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()

Apurva Nigam said:   1 decade ago
If (A) is the answer then how can
http://www.indiabix.com/c-programming/input-output/discussion-446
be true,where it is giving an infinite loop as output??


Post your comments here:

Your comments will be displayed after verification.