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 1 of 2.
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).
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).
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.
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.
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).
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.
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.
The pointed answer to the question is ambiguous.
(1)
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...
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.
But if you observed, it first prints the content of the file and then garbage values because of infinite loop.
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??
http://www.indiabix.com/c-programming/input-output/discussion-446
be true,where it is giving an infinite loop as output??
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.
Ronali said:
1 decade ago
Explanation is not clear, actually it gives segmentation fault but why?
(1)
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers