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;
}
Error: in unsigned char declaration
Error: while statement
No error
It prints all characters in file "trial"
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.

Dileep kumar Kotha said:   1 decade ago
Option B is also correct, since you used getc() instead of fgetc()

Maithili said:   9 years ago
How here EOF is -1?


Post your comments here:

Your comments will be displayed after verification.