By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,192 Members | 1,276 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,192 IT Pros & Developers. It's quick & easy.

question about "while"

P: n/a
Hi Group,
When I run this:

# include <stdio.h>
int main(){
int c=0;
while ( (c=getchar()) != EOF && c != ' ' && c != '\t' )
printf("foo");
if (c == '8')
putchar(c);
else if (c=='\n')
printf( " It's a new line");
return(0);
}

and the stdin is: 'b" i get "foofoo" as the output.

8 or a new line feed gives "8" and "It's a new line" as expected.

My question is this. How exactly is the while statement evaluated if c
is neither a space or a tab.

Thank you.

Apr 11 '06 #1
Share this Question
Share on Google+
9 Replies


P: n/a
When c is neither a space or a tab, this program will always be in the
while loop. when c is a space or a tab, no output will display.

1. c = getchar()
2. compare to EOF ,' ' , and '\t'
3. compare return false --> quit while loop
compare return true --> back to 1

Apr 11 '06 #2

P: n/a

LZXIA wrote:
When c is neither a space or a tab, this program will always be in the
while loop. when c is a space or a tab, no output will display.

1. c = getchar()
2. compare to EOF ,' ' , and '\t'
3. compare return false --> quit while loop
compare return true --> back to 1

Hi,
If I understand you correctly, why does it only seem to print "foofoo"
if is not "EOF ,' ' , and '\t' " as opposed to getting stuck in the
loop with endless foos?

Thanks

Apr 11 '06 #3

P: n/a
I am not good at english, and I can't understand your words, but I can
give you a example

Now most keyboard is designed to be like this:
1. It has a buffer to restore input, and the buffer size is limit such
as 80 characters.
2. When the buffer is full, or it get a '\n', it will send the
characters to where them should go.

So when you type "bacd" and did not hit the [CR], nothing will
happen.But if CR is pressed, the funtion getchar() will work.
It get the characters from the buffer one by one. after it has got the
'd', it will get a '\n' . so you will get 5 foo. Then getchar()
waiting for you input again.

I hope I have explained clearly.

Apr 11 '06 #4

P: n/a

LZXIA wrote:
I am not good at english,
No...it was I not understanding well.
So when you type "bacd" ....... get a '\n' . so you will get 5 foo.


And that is exactly what happened.

I hope I have explained clearly.

Yes...thank you.

Apr 11 '06 #5

P: n/a
becos the function getchar() waits for input (its a function which
blocks) so soon after while condition is evaluated (and is false i.e no
\n or ' ' or '\t' ) it waits for next input ().

Apr 11 '06 #6

P: n/a
becos the function getchar() waits for input (its a function which
blocks) so soon after while condition is evaluated (and is false i.e no
\n or ' ' or '\t' ) it waits for next input ().

Apr 11 '06 #7

P: n/a
white.crow wrote:
becos the function getchar() waits for input (its a function which
blocks) so soon after while condition is evaluated (and is false i.e
no \n or ' ' or '\t' ) it waits for next input ().


Please read in the information in my .sig below.

Brian

--
Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded
header.
Apr 11 '06 #8

P: n/a
LZXIA wrote:
I am not good at english, and I can't understand your words, but I can
give you a example


Please read in the information in my .sig below.

Brian
--
Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded
header.
Apr 11 '06 #9

P: n/a
morpheus wrote:
Hi Group,
When I run this:

# include <stdio.h>
int main(){
int c=0;
while ( (c=getchar()) != EOF && c != ' ' && c != '\t' )
printf("foo");
if (c == '8')
putchar(c);
else if (c=='\n')
printf( " It's a new line");
return(0);
}

and the stdin is: 'b" i get "foofoo" as the output.

8 or a new line feed gives "8" and "It's a new line" as expected.

My question is this. How exactly is the while statement evaluated if c
is neither a space or a tab.

Thank you.


From MSDN Library:

while ( expression ) statement

The expression must have arithmetic or pointer type. Execution proceeds
as follows:

1. The expression is evaluated.

2. If expression is initially false, the body of the while statement is
never executed, and control passes from the while statement to the
next statement in the program.

If expression is true (nonzero), the body of the statement is executed
and the process is repeated beginning at step 1.

Apr 12 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.