Sail E0 Webinar
Question
Comment on the output of this C code?
int main()
{
char c;
int i = 0;
FILE *file;
file = fopen("test.txt", "w+");
fprintf(file, "%c", 'a');
fprintf(file, "%c", -1);
fprintf(file, "%c", 'b');
fclose(file);
file = fopen("test.txt", "r");
while ((c = fgetc(file)) != -1)
printf("%c", c);
return 0;
}
Options:
A .  a
B .  Infinite loop
C .  Depends on what fgetc returns
D .  Depends on the compiler
Answer: Option A


None.
Output:
$ cc pgm3.c
$ a.out
a



Was this answer helpful ?
Next Question

Submit Solution

Your email address will not be published. Required fields are marked *

Latest Videos

Latest Test Papers