LearnFrenzy
Sign in Take a free test

C Input / Output · Predict the Output · Question 10

fseek with a negative offset from SEEK_END – output Answer, explanation & discussion

  • Output
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 10 Back to Exercise

What is the output of the following C program?

main.c
#include <stdio.h>

int main(void) {
    FILE *fp = tmpfile();
    if (fp == NULL)
        return 1;
    fputs("Learn by doing", fp);
    fseek(fp, -5L, SEEK_END);
    char word[8];
    fscanf(fp, "%7s", word);
    printf("%s %ld\n", word, ftell(fp));
    fclose(fp);
    return 0;
}
  • A doing 14 Correct
  • B doing 9
  • C by 8
  • D Learn 5
Correct Answer: Option A

The text is 14 bytes long. Five bytes before the end is "doing"; after reading it the position is at the end, 14.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links