C Programming - Input / Output - Discussion
Discussion Forum : Input / Output - Find Output of Program (Q.No. 9)
9.
What will be the output of the program ?
#include<stdio.h>
int main()
{
FILE *ptr;
char i;
ptr = fopen("myfile.c", "r");
while((i=fgetc(ptr))!=NULL)
printf("%c", i);
return 0;
}
Answer: Option
Explanation:
The program will generate infinite loop. When an EOF is encountered fgetc() returns EOF. Instead of checking the condition for EOF we have checked it for NULL. so the program will generate infinite loop.
Discussion:
13 comments Page 2 of 2.
Onurag said:
8 years ago
If Null comes before some text and Eof I think It will execute upto Null.
"I Want to run" "I want" will get printed.
"I Want to run" "I want" will get printed.
Rohan said:
7 years ago
FILE *ptr;
Step 1: A pointer ptr is created for FILE
char i;
Step 2: A variable i is declared as character...
ptr = fopen("myfile.c", "r");
Step 3: ptr is used to store the content of "myfile.c" which is opened in readable mode..
while((i=fgetc(ptr))!=NULL)
printf("%c", i);
Step 4: (i) while loop is used , when an EOF is encountered fgetc() returns EOF.
(ii) Instead of checking the condition for EOF we have checked it for NULL.
Ans ->> [c] infinite loop.
Step 1: A pointer ptr is created for FILE
char i;
Step 2: A variable i is declared as character...
ptr = fopen("myfile.c", "r");
Step 3: ptr is used to store the content of "myfile.c" which is opened in readable mode..
while((i=fgetc(ptr))!=NULL)
printf("%c", i);
Step 4: (i) while loop is used , when an EOF is encountered fgetc() returns EOF.
(ii) Instead of checking the condition for EOF we have checked it for NULL.
Ans ->> [c] infinite loop.
Utkarsh said:
6 years ago
What if actually have a NULL character in our file somewhere in between? Please tell me.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers