Monday, 15 April 2013

c - datatype of variable used for holding input of getchar(),as char instead of int -



c - datatype of variable used for holding input of getchar(),as char instead of int -

#include<stdio.h> int main() { int c; while((c=getchar())!=eof) putchar(c); putchar(c); //2nd putchar getch(); }

in above code book 'c ritchie n kernighan', reason declaring c int variable given :

so c may able hold code of eof outside char codes char datatype can accommodate.

but when declare c char, , input eof(ctrl-z + enter),that value(which -1 on computer)does go c , end exiting while up, way happens if declare c int. why need declare c int, when char works fine?? or missing here?

one more thing, in sec putchar statement, should print lastly value of c (ie. -1) after having received eof doesn't. why?

whether char signed or unsigned implementation-defined.

if char unsigned, no char value can equal eof, loop never exit.

if char signed, eof equal c, happens on machine. problem is, means, valid char equal eof, causing loop exit early.

c getchar kernighan-and-ritchie

No comments:

Post a Comment