Test your understanding of C file handling with these 13 multiple-choice questions — fopen modes, reading and writing files, fseek, and EOF handling. Every question has the answer and a short explanation hidden below it, and the trickier ones link to a full walkthrough with compilable code.
These questions come from our free Android app, C Programming Quiz — 150+ questions across 9 categories with score tracking, if you prefer to practise on your phone.
How to use this page: answer each question yourself before tapping Show answer. If you get one wrong, follow the linked explanation — knowing why the wrong options are wrong is what interviews test.
Question 1 of 13 (Easy)
Which function opens a file in C?
- open()
- fopen()
- file_open()
- read()
Show answer
Answer: B — fopen()
fopen() opens a file and returns a FILE pointer. It is the standard C way to open files.
Question 2 of 13 (Easy)
What does fopen return on failure?
- 0
- NULL
- -1
- EOF
Show answer
Answer: B — NULL
fopen returns NULL if it cannot open the file (e.g., file not found, permission denied). Always check the return value.
Question 3 of 13 (Easy)
Which function closes an open file?
- close()
- fclose()
- end()
- fend()
Show answer
Answer: B — fclose()
fclose() flushes the buffer and closes the file. Forgetting to call it can cause data loss or resource leaks.
Question 4 of 13 (Medium)
What mode string opens a file for writing, creating it if necessary and truncating it if it exists?
- "r"
- "w"
- "a"
- "rw"
Show answer
Answer: B — "w"
"w" opens a file for writing. If it exists, its contents are truncated. If not, it is created. "a" appends. "r" is read-only.
Question 5 of 13 (Medium)
What does fprintf do?
- Reads formatted input from a file
- Writes formatted output to a file
- Flushes file output
- Returns the size of a file
Show answer
Answer: B — Writes formatted output to a file
fprintf works like printf but writes to a FILE* instead of stdout. First argument is the file pointer.
Question 6 of 13 (Medium)
What is EOF in C?
- A special file that marks the end of all files
- A macro (typically -1) returned by functions to signal end of file or error
- A file extension
- A null character
Show answer
Answer: B — A macro (typically -1) returned by functions to signal end of file or error
EOF is a macro defined in <stdio.h>, usually -1. Functions like fgetc return EOF to indicate no more data is available or an error occurred.
Question 7 of 13 (Hard)
What does fseek(fp, 0, SEEK_END) do?
- Rewinds the file to the beginning
- Moves the file position indicator to the end of the file
- Closes the file
- Returns the file size
Show answer
Answer: B — Moves the file position indicator to the end of the file
fseek moves the file position indicator. SEEK_END as the base moves relative to the file's end; an offset of 0 positions at the very end. Combined with ftell, this is how you measure a file's size.
Question 8 of 13 (Hard)
What is the difference between text mode and binary mode in fopen?
- No difference on Linux; on Windows, text mode translates \n to \r\n
- Binary mode is always faster
- Text mode supports Unicode; binary mode does not
- Binary mode cannot read text files
Show answer
Answer: A — No difference on Linux; on Windows, text mode translates \n to \r\n
On Linux/macOS, text and binary mode behave identically. On Windows, text mode performs newline translation (\n ↔ \r\n). Use binary mode ("rb"/"wb") when you need raw bytes.
Question 9 of 13 (Medium)
Which function reads a line from a file into a buffer?
- fscanf
- fgets
- fread
- getline
Show answer
Answer: B — fgets
fgets reads up to n-1 characters from a FILE* into a buffer, stopping at '\n' or EOF. It is the standard safe line-reading function in C.
Question 10 of 13 (Hard)
What does ftell(fp) return?
- The file size in bytes
- The current position of the file position indicator
- The number of bytes read so far
- The file descriptor number
Show answer
Answer: B — The current position of the file position indicator
ftell returns the current value of the file position indicator as a long. Combined with fseek(fp, 0, SEEK_END), this is the standard way to determine file size.
Question 11 of 13 (Medium)
What is the purpose of fflush(stdout)?
- Closes stdout
- Forces any buffered output to be written immediately
- Resets the output format
- Reads pending input from stdin
Show answer
Answer: B — Forces any buffered output to be written immediately
stdout is often line-buffered or fully-buffered. fflush forces the buffer to be written to the underlying output device immediately.
Question 12 of 13 (Easy)
Which standard stream represents error output in C?
- stdin
- stdout
- stderr
- errout
Show answer
Answer: C — stderr
stderr is the standard error stream. It is often unbuffered so error messages appear immediately, even if stdout is redirected.
Question 13 of 13 (Medium)
Which function reads a single character from a file?
- getc(fp)
- readc(fp)
- fscanchar(fp)
- inchar(fp)
Show answer
Answer: A — getc(fp)
getc(fp) (or fgetc) reads one character from the file pointer fp. It returns the character as an unsigned char cast to int, or EOF on failure.
How Did You Score?
All 13 correct means this topic is interview-ready — try the harder timed rounds in the C Programming Quiz app. Missed a few? The guides below cover everything these questions test.
Keep Practising
- File Handling in C — Complete Guide
- C Programs List — 130+ Examples
- C Aptitude Questions and Answers
- All quiz question walkthroughs
More C Quizzes
- C Basics Quiz — 28 Questions with Answers
- C Operators Quiz — 20 Questions with Answers
- C Memory Management Quiz — 19 Questions with Answers
- C Pointers Quiz — 18 Questions with Answers
- C Arrays and Strings Quiz — 18 Questions with Answers
- C Functions Quiz — 15 Questions with Answers
- C Structs and Unions Quiz — 13 Questions with Answers
- C Preprocessor Quiz — 11 Questions with Answers