Hi, i have a problem using getchar:
It prints "hello" twice, because i think it sees the '\n' character.Code:#include <stdio.h> int main () { int c; char a = 'a'; while ( (c = getchar()) != a ) { printf("hello"); } }
??
Hi, i have a problem using getchar:
It prints "hello" twice, because i think it sees the '\n' character.Code:#include <stdio.h> int main () { int c; char a = 'a'; while ( (c = getchar()) != a ) { printf("hello"); } }
??
So:
if(c != '\n')
needs to be inserted above the printf() statement, doesn't it?
when you execute your program, a prompt will invite you to enter some charachers and the program will calculate the number of char you have entred until you enter the char 'a' and he will dispaly 'N' * hello times, with N = the number of char you entred.
So you can see 'a' in your program as a terminating char of the execution.
Regards.