C Programming - Input / Output - Discussion
Discussion Forum : Input / Output - Point Out Errors (Q.No. 5)
5.
Point out the error/warning in the program?
#include<stdio.h>
int main()
{
unsigned char ch;
FILE *fp;
fp=fopen("trial", "r");
while((ch = getc(fp))!=EOF)
printf("%c", ch);
fclose(fp);
return 0;
}
Answer: Option
Explanation:
Here, EOF is -1. As 'ch' is declared as unsigned char it cannot deal with any negative value.
Discussion:
12 comments Page 2 of 2.
Renonsz said:
9 years ago
EOF is promoted to 255.
So A) cannot be right, it runs without error and print out the content of the file.
So A) cannot be right, it runs without error and print out the content of the file.
Robert Azo Echima said:
4 weeks ago
@All.
The real issue here isn’t the declaration itself, but how it's used in the while loop.
Declaring ch as an unsigned char is syntactically valid, but it introduces a logical bug when paired with getc (fp) like this: while ((ch = getc(fp))!= EOF).
The real issue here isn’t the declaration itself, but how it's used in the while loop.
Declaring ch as an unsigned char is syntactically valid, but it introduces a logical bug when paired with getc (fp) like this: while ((ch = getc(fp))!= EOF).
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers