• Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

getchar

somboku

New Coder
Hi guys,

I'm confusing about getchar() while reading Ritchie Kerninghans book.

while this one loops as my 2.4Ghz can
Ex1
c = getchar(); while( c != EOF ) { ++g; if ((g % 1000000000) == 0) { printf("%lld\n",g); } if(g > 10000000000) break; }

but this. just waits and prints input for input on every [ENTER]:

Ex2
while ((c = getchar()) != EOF){ ++f; printf("c:%c.",c); if ( f > 1000) break; //putchar(c); }

shouldn't be getchar() outside a while just a "oneShot" and inside like
in the second example loop as the first one?
And why dosnt Ex1 starts print immidiately but right after hitting [ENTER]?

thanks.
S.
 

cbreemer

King Coder
Both code samples work exactly as they should. Ex 1 reads the keyboard once, then executes a loop that prints some stuff and ends when the counter reaches max. Testing c for EOF is bogus here, as c never changes (and anyway, your keyboard will never return EOF, except maybe when it breaks) . Ex 2 starts a loop requiring you to hit the keyboard with every iteration. What exactly do you not understand about it ?
Your question
shouldn't be getchar() outside a while just a "oneShot" and inside like
in the second example loop as the first one?
does not make any sense to me. Please state what exactly you expect, and/or want to achieve.
 

somboku

New Coder
Both code samples work exactly as they should. Ex 1 reads the keyboard once, then executes a loop that prints some stuff and ends when the counter reaches max. Testing c for EOF is bogus here, as c never changes (and anyway, your keyboard will never return EOF, except maybe when it breaks) . Ex 2 starts a loop requiring you to hit the keyboard with every iteration. What exactly do you not understand about it ?
Your question

does not make any sense to me. Please state what exactly you expect, and/or want to achieve.
Well, thanks for the answer.

I read your answer a few times and still ... yes... but why is this like..
And I wanted to answer your question and dubble check with the origin..

The following happend: I am reading THAT book from Ritchi Kerninghahn 2nd Edition in
in order to learn C. And messing around with that getChar() -creating own loops etc...
This is the original loop in the book:
... int c; c = getchar(); while (c != EOF){ putchar(c); c = getchar(); }

after a while that "c = getchar()" within the loop was not there. And I tried to understand
my "broken" snippet example. :/

Now, I am fine thanks to you and I can turn to page 32. :)

Thx.
 
Top