EOF

Mar 30, 2015 at 8:35am
Hello,

I need to verify that the expression getchar() ! = EOF is 0 or 1.
My current code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <stdio.h>



int main (int argc, char *argv[])
{
   
int c;

while (( c= getchar()) != EOF) {

 printf("%d ", c != EOF);
 putchar(c);

 break;  
 }

 printf("\n%d\n", c != EOF);

}



When typing some characters on terminal window getting strange results:

characters
1 c
1
Mar 30, 2015 at 8:42am
You are unconditionally breaking out of the loop on line 15; is that really what you want?
Mar 30, 2015 at 9:18am

The break statement will stop the execution of the innermost loop and start executing the next line of code after the block, so it would indeed not work here.

Still, when I leave it out I get something like this:

98980980
1 91 81 91 81 01 91 81 01


Where did the 0 go?
Mar 30, 2015 at 12:34pm
I dont think there is a missing zero, i think there is an extra "1"

1-9
1-8
1-9
1-8
1-0
1-9
1-8
1-0
1-?
Mar 31, 2015 at 6:21am
You'll want to print a newline at the end of the printing within the loop to get something like Jaybob66 posted.

All the characters input are accounted for; the last 1 is just the program coming around the loop again and waiting for you to input the next character.
Topic archived. No new replies allowed.