Question discussion

What will be the output of the code if input.txt contains 42?

359038Medium DSSSB TGT Computer Science 0 replies 0 views
0
What will be the output of the code if input.txt contains 42?
#include <stdio.h>
int main() {
    FILE *file = fopen("input.txt", "r");
    int value;
    fscanf(file, "%d", &value);
    fseek(file, 0, SEEK_SET);
    fscanf(file, "%d", &value);
    fclose(file);
    printf("%d\n", value);
    return 0;
}
  • AUndefined behavior
  • B42
  • CEOF
  • DIt depends on the system
Correct answer: B

Explanation

fseek(file, 0, SEEK_SET) rewinds to the start, so the second fscanf reads 42 again and the output is 42.

Discussion 0

Nobody has replied yet. Start the discussion!