C Programming - Input / Output - Discussion


Point out the error/warning in the program?


int main()
    unsigned char ch;
    FILE *fp;
    fp=fopen("trial", "r");
    while((ch = getc(fp))!=EOF)
        printf("%c", ch);
    return 0;

[A]. Error: in unsigned char declaration
[B]. Error: while statement
[C]. No error
[D]. It prints all characters in file "trial"

Answer: Option A


Here, EOF is -1. As 'ch' is declared as unsigned char it cannot deal with any negative value.

Apurva Nigam said: (Jun 28, 2011)  
If (A) is the answer then how can
be true,where it is giving an infinite loop as output??

Dileep Kumar Kotha said: (Aug 20, 2011)  
Option B is also correct, since you used getc() instead of fgetc()

Parin said: (Dec 15, 2011)  
I exactly had a same question as Apurva Nigam has..!!!.. That's why I just entered to discussion page.. but what i saw was that the question is already posted...

Ronali said: (Jun 6, 2013)  
Explanation is not clear, actually it gives segmentation fault but why?

Aakash said: (Aug 17, 2013)  
Hey @Ronali trial file is not available thats why its giving segmentation fault make one file trial.txt and then execute the program it will give a as answer but path of trial should be specified in while.

Manish.Sargaiyan said: (Sep 15, 2013)  
Option C should be the answer.

Because option A is wrong there is no error with unsigned char it will just make the loop infinite since EOF=-1 will not be check by it.

Option B is also wrong there is no error in while loop, we can use getc() as well as fgetc() both will work.

Option D is not correct because it won't print all characters because there is infinite loop.

SO there is NO ERROR in program so option C is correct.

Shreejeet said: (Jul 9, 2016)  
Yeah, You are right @Manish option should be C.

But if you observed, it first prints the content of the file and then garbage values because of infinite loop.

Robert said: (Aug 21, 2016)  
It just prints an infinite loop since we need type char "variable" to be declared, not unsigned char "variable".

The pointed answer to the question is ambiguous.

Robert said: (Aug 21, 2016)  
"EOF is a macro which expands to an integer constant expression with type int and an implementation dependent negative value but is very commonly -1. '\0' is a char with value 0 in C++ and an int with the value 0 in C".

In the very first moment when you declare the type of the variable as "unsigned char", you assume will always have a value >= 0. So the condition will always be fulfilled.

This will lead us to an infinite loop.

The answer pointed to this question is a little ambiguous. I would choose instead.

Answer: Infinite loop (with argumentation).

Maithili said: (Sep 14, 2016)  
How here EOF is -1?

Renonsz said: (Jan 11, 2017)  
EOF is promoted to 255.

So A) cannot be right, it runs without error and print out the content of the file.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.