question about "while"

M

morpheus

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.
 
L

LZXIA

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
 
M

morpheus

LZXIA said:
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
 
L

LZXIA

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.
 
W

white.crow

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 ().
 
W

white.crow

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 ().
 
D

Default User

white.crow said:
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
 
D

Default User

LZXIA said:
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
 
S

seeker

morpheus said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,150
Latest member
MakersCBDReviews
Top