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

how can i come out of the loop : while(getchar()!=EOF);

P: 2
Hello everyone..
I am doing C Programming in Ubuntu G++ compiler

in one program i need to come out out the code


#include<stdio.h>
char c;
while( (c=getchar())!= EOF)
printf("something\n");
printf("end of file\n");


while running this program, which word to type to get out of while loop.
Thanks
Aug 4 '07 #1
Share this Question
Share on Google+
5 Replies


Meetee
Expert Mod 100+
P: 931
Hello everyone..
I am doing C Programming in Ubuntu G++ compiler

in one program i need to come out out the code


#include<stdio.h>
char c;
while( (c=getchar())!= EOF)
printf("something\n");
printf("end of file\n");


while running this program, which word to type to get out of while loop.
Thanks
Hi,

You can compare (c=getchar()) with some character like
while( (c=getchar())!= '\n')
If you press ENTER in this case, it will come out of while loop.

Regards
Aug 4 '07 #2

Expert 10K+
P: 11,448
Hi,

You can compare (c=getchar()) with some character like
while( (c=getchar())!= '\n')
If you press ENTER in this case, it will come out of while loop.

Regards
True, but the OP's problem is that 'c' is a char in his program. If he'd change
it to an 'int' type the test for EOF would work as expected. The -1 value (which
is the EOF) value is truncated to 8 bits for the char and most likely his char
type is unsigned, so that -1 value comes out again as 255 which definitely isn't
equal to -1. Making 'c' an int eliminates the truncation.

kind regards,

Jos
Aug 4 '07 #3

P: 2
Thanks.
I came to know the last day one more fact that in linux Ctrl+D also work as EOF.
Aug 5 '07 #4

P: 1
Hello everyone..
I am doing C Programming in Ubuntu G++ compiler

in one program i need to come out out the code


#include<stdio.h>
char c;
while( (c=getchar())!= EOF)
printf("something\n");
printf("end of file\n");


while running this program, which word to type to get out of while loop.
Thanks
i think break statment na or not my friend, good prog u wrote
Aug 5 '07 #5

P: 1
@amit1702
hey this does work but when we press it twice else it only work for exiting while loop and not main. But the solution is really gr8 thanks a lot. could you also tell the alternative for <conio.h> available in linux.
Mar 1 '09 #6

Post your reply

Sign in to post your reply or Sign up for a free account.