Newbie C question...
  • Priest of the Order of the Butterfly
    Priest of the Order of the Butterfly
    ausPPC
    Posts: 543 from 2007/8/6
    From: Pending...
    I don't understand why the second scanf() doesn't wait for input. I've compiled with gcc 2.95.3 and 4.4.4 just to see if that would make a difference but it doesn't. The final printf() just outputs -

    You entered

    and then the program returns to the command line.

    Code:
    #include <stdio.h>

    main()
    {
    char a;

    printf("Pausing... Please enter any letter or number > ");
    scanf("%c", &a);
    printf("You entered %cn", a);
    printf("Pausing... Please enter any letter or number > ");
    scanf("%c", &a);
    printf("You entered %cn", a);
    }


    umm... Also don't know why the backslashes before the n character in the 2nd and 4th printf() calls aren't being displayed above...

    [ Edited by ausPPC on 2010/12/12 12:25 ]
    PPC assembly ain't so bad... ;)
  • »12.12.10 - 02:19
    Profile Visit Website
  • Priest of the Order of the Butterfly
    Priest of the Order of the Butterfly
    ausPPC
    Posts: 543 from 2007/8/6
    From: Pending...
    I got a different but still confusing result from vbcc - the compiled program waits for input at the second scanf() but the final printf() still outputs as above. There also seems to be an additional return code (blank line) in the output. Any takers?

    In all cases the compile command has been -

    gcc -o ram:ctest ctest.c

    (with vc instead of gcc when using the vbcc compiler).
    PPC assembly ain't so bad... ;)
  • »12.12.10 - 06:34
    Profile Visit Website
  • Priest of the Order of the Butterfly
    Priest of the Order of the Butterfly
    eliot
    Posts: 564 from 2004/4/15
    Here the corrected code, shich should make it clear:

    #include <stdio.h>

    int main(void)
    {
    char a;

    printf("Pausing... Please enter any letter or number > ");
    scanf("%c", &a);
    printf("You entered %c\n", a);

    if (getchar() == '\n')
    printf("bingo, again!\n");

    printf("Pausing... Please enter any letter or number > ");
    scanf("%c", &a);
    printf("You entered %c\n", a);

    return 0;
    }

    To pint Newline to stdout, it should be "\n".
    There is a newline in stdin, so second scanf
    just reads it.
    regards
    eliot
  • »12.12.10 - 09:01
    Profile
  • Priest of the Order of the Butterfly
    Priest of the Order of the Butterfly
    ausPPC
    Posts: 543 from 2007/8/6
    From: Pending...
    Aha... The particular way I'm using scanf() only removes a single character from the user's input when there is always going to be a minimum of two user input characters - the second one probably being a return character which is then unintentionally picked by the second scanf().
    PPC assembly ain't so bad... ;)
  • »12.12.10 - 10:47
    Profile Visit Website
  • Priest of the Order of the Butterfly
    Priest of the Order of the Butterfly
    eliot
    Posts: 564 from 2004/4/15
    Yes, that's it.
    You should buy a good book about c.

    [ Edited by eliot on 2010/12/12 14:19 ]
    regards
    eliot
  • »12.12.10 - 14:19
    Profile
  • Priest of the Order of the Butterfly
    Priest of the Order of the Butterfly
    ausPPC
    Posts: 543 from 2007/8/6
    From: Pending...
    Actually, I've got a venerable copy of 'A Book on C' by Al Kelley and Ira Pohl. It's venerable because it's just the 2nd edition from 1989.

    At the moment, my only interest in C is trying to integrate it with assembly and it remains to be seen if I'm going to win that battle... ;)
    PPC assembly ain't so bad... ;)
  • »12.12.10 - 20:07
    Profile Visit Website
  • Priest of the Order of the Butterfly
    Priest of the Order of the Butterfly
    ausPPC
    Posts: 543 from 2007/8/6
    From: Pending...
    I give up... How does a C program read and write to allocated memory?
    PPC assembly ain't so bad... ;)
  • »22.02.11 - 06:38
    Profile Visit Website
  • MorphOS Developer
    itix
    Posts: 1516 from 2003/2/24
    From: Finland
    Quote:


    I give up... How does a C program read and write to allocated memory?



    struct Something
    {
    ULONG foo;
    ULONG bar;
    };

    struct Something *s = AllocMem(sizeof(*s), MEMF_ANY);

    if (s) s->foo = 1;

    You can also define it as an array (a byte array for example).
    1 + 1 = 3 with very large values of 1
  • »22.02.11 - 06:57
    Profile
  • Priest of the Order of the Butterfly
    Priest of the Order of the Butterfly
    ausPPC
    Posts: 543 from 2007/8/6
    From: Pending...
    Thank you! btw itix, have you returned from where ever you were? I'm still interested in any light you can shed on the use of InternalLoadSeg()...
    PPC assembly ain't so bad... ;)
  • »22.02.11 - 07:41
    Profile Visit Website
  • MorphOS Developer
    itix
    Posts: 1516 from 2003/2/24
    From: Finland
    I am still here in New Zealand, I had to extend my staying again... luckily that earthquake didn't strike here in North island where I am living.

    I'll be back on the next week, I hope... definitely before end of March when my visa expires.
    1 + 1 = 3 with very large values of 1
  • »22.02.11 - 09:05
    Profile
  • Yokemate of Keyboards
    Yokemate of Keyboards
    amigadave
    Posts: 2794 from 2006/3/21
    From: Northern Calif...
    My Sister lives not far from Aukland in the North Island, so it is good to hear that the earthquake did not have any effect there.

    Really sad that so many people were killed in that quake, but it was very strong.

    I don't know why you are in New Zealand, but it is one of the most beautiful countries I have ever visited and all the people I met while vacationing there were very friendly. I have even considered moving there to live permanently now that I am retired, but don't know if my retirement income is high enough to move there.
    MorphOS - The best Next Gen Amiga choice.
  • »22.02.11 - 16:52
    Profile
  • MorphOS Developer
    itix
    Posts: 1516 from 2003/2/24
    From: Finland
    This time the earthquake was near the city centre. I have been reading horror stories how fallen bricks hit people... but rescue is well organized here, just like the whole country.

    I am here working at the local paper mill but the nature here is really beautiful. Auckland is too big city for my taste but I love Bay of Plenty area where I am currently. Prices here are normal European level and people are friendly. Summer is not too hot. It is almost +25C every day yet it rarely goes down to +20 or up to +30.

    Downside is that NZ is pretty far away from everything. Even just Australia is 1500km away. NZ is not large in area but by a car you wont get far in one day. There arent many cities either. On the otherhand I have been at vulcanoes like Whakaari and caveing in Waikino. Lot of fun stuff to do if you like the nature.
    1 + 1 = 3 with very large values of 1
  • »23.02.11 - 06:43
    Profile
  • Priest of the Order of the Butterfly
    Priest of the Order of the Butterfly
    ausPPC
    Posts: 543 from 2007/8/6
    From: Pending...
    I've now gone through more trial and error experiments than I care to admit...

    Allocating memory and writing the address and size of the previous allocation to the new block was easy enough. Trying to access that data and use it to free each allocated block has proven to be <insert obscene yet colourful expression here>.

    Edit: I tried a different approach using casting and pointers with subscripting and it seems like I'm on the right track now.

    [ Edited by ausPPC on 2011/2/23 19:04 ]
    PPC assembly ain't so bad... ;)
  • »23.02.11 - 07:08
    Profile Visit Website
  • Yokemate of Keyboards
    Yokemate of Keyboards
    amigadave
    Posts: 2794 from 2006/3/21
    From: Northern Calif...
    Quote:


    itix wrote:
    This time the earthquake was near the city centre. I have been reading horror stories how fallen bricks hit people... but rescue is well organized here, just like the whole country.

    I am here working at the local paper mill but the nature here is really beautiful. Auckland is too big city for my taste but I love Bay of Plenty area where I am currently. Prices here are normal European level and people are friendly. Summer is not too hot. It is almost +25C every day yet it rarely goes down to +20 or up to +30.

    Downside is that NZ is pretty far away from everything. Even just Australia is 1500km away. NZ is not large in area but by a car you wont get far in one day. There arent many cities either. On the otherhand I have been at vulcanoes like Whakaari and caveing in Waikino. Lot of fun stuff to do if you like the nature.




    Yes, I agree that living in Aukland would not be my choice if I lived in NZ. My sister lives in Whangaparoa (sp?) and looks out on the bay on one side of her house and if she stands in just the right place on the other side of the house can see the ocean on the other side of the peninsula she lives on. I stayed near there for a few days and then rented a car and drove down to Wellington with several stops along the way. Then we flew back to Aukland where my sister picked us up again for a couple more days before we came home. That was probably the best 10 days I have ever had on any vacation. NZ is the most beautiful country I have ever visited and the people are amazingly friendly. Great Thai restaurant too. I'll go back some day to visit again, or possibly move there permanently. Caving at Wataimo was a blast and I was very proud that my wife did it without hesitation, or complaint.
    MorphOS - The best Next Gen Amiga choice.
  • »23.02.11 - 21:17
    Profile
  • MorphOS Developer
    itix
    Posts: 1516 from 2003/2/24
    From: Finland
    You mean Waitomo glowworm caves? It sounds like fun place. It seems it is my last weekend here in New Zealand now and I have been driving everywhere except in the west. Already have driven over 5000 km with my rental car, I wonder if I can make it 6000 km :)

    Oh, and we are hopelessly OT :P
    1 + 1 = 3 with very large values of 1
  • »25.02.11 - 10:47
    Profile
  • jPV
  • Yokemate of Keyboards
    Yokemate of Keyboards
    jPV
    Posts: 2027 from 2003/2/24
    From: po-RNO
    Quote:


    itix wrote:

    Oh, and we are hopelessly OT :P


    I love to read it anyway ;) It's always been my dream to visit NZ.. I don't know if it happens ever. Maybe you should write travel story with pics too (I guess it could be included in one certain mag if it gets released some day) :)
  • »25.02.11 - 15:36
    Profile Visit Website
  • Yokemate of Keyboards
    Yokemate of Keyboards
    amigadave
    Posts: 2794 from 2006/3/21
    From: Northern Calif...
    Quote:


    itix wrote:
    You mean Waitomo glowworm caves? It sounds like fun place. It seems it is my last weekend here in New Zealand now and I have been driving everywhere except in the west. Already have driven over 5000 km with my rental car, I wonder if I can make it 6000 km :)

    Oh, and we are hopelessly OT :P


    Yes, that's the name of the caves we went to. The guided tour was very entertaining, as the guides took advantage of scaring the women in our group at a couple of points as they glided down into unknown darkness on zip lines, but it was a good thing that my wife did not hear the part of the guides speech about there being eels in the water in the cave, or our tour would have stopped right then and there. Even with wetsuits, it was hard to believe that my wife would jump into that water in a dark cave. She missed her tire tube and went completely under the water and came up sputtering and grabbing for anyone, and anything she could get her hands on. Fun memories.

    Very off topic, but have a safe journey home and for everyone else, if you have a chance to visit NZ for a vacation, jump at the opportunity. It is one of the best vacation countries in the World.
    MorphOS - The best Next Gen Amiga choice.
  • »26.02.11 - 04:46
    Profile