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 1 of 2.

Ronali said:   1 decade ago
Explanation is not clear, actually it gives segmentation fault but why?
(1)

Robert said:   9 years ago
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.
(1)

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??

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

Parin said:   1 decade ago
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...

Aakash said:   1 decade ago
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:   1 decade ago
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:   9 years ago
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:   9 years ago
"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:   9 years ago
How here EOF is -1?


Post your comments here:

Your comments will be displayed after verification.