Q. What is the output of the following C program?
Code:
#include <stdio.h>
int main()
{
char c;
int i = 0;
FILE *file;
// write to the text file
file = fopen("test.txt", "w+");
fprintf(file, "%c", 'x');
fprintf(file, "%c", -1);
fprintf(file, "%c", 'y');
fclose(file);
// read from the text file
file = fopen("test.txt", "r");
while ((c = fgetc(file)) != -1)
printf("%c", c);
return 0;
}
β
Correct Answer: (A)
Display x
Explanation: The output is as follows:
$gcc prog3.c
$ a.out
x