PDA

View Full Version : quick c question


dmm1285
June 3rd, 2008, 02:41 AM
Is there any case where this:

int ch;
while((ch = getchar()) != '\n')
putchar(ch);

would act differently then:

int ch = 0;
while(ch != '\n')
{
ch = getchar();
putchar(ch);
}

jpkotta
June 3rd, 2008, 03:13 AM
They act differently when the character read by getchr() is '\n'.

dmm1285
June 3rd, 2008, 04:51 AM
I see what you mean. I understand why that idiom is so used now. I was trying to think of alternate way besides the in line assignment without repeating myself.

Joeb454
June 3rd, 2008, 08:20 PM
I'm not 100% sure (not written C for a while), but would it be while (ch != '\0') //basically, NULL

//rest of code