Test Your Skill on Input / Output in C Language
In this article, let's test — or analyse — your skill on how input and output are used in the C programming language. We will study some MCQ (Multiple-Choice Questions) and FAQ (Frequently Asked Questions) covering the standard I/O library functions.
📚 What you'll practice on this page:
- The difference between formatted and unformatted I/O functions
- The return types of
getchar(), putchar(), putc() and why they are int
- Which functions read from
stdin and which write to stdout
- Why
print() is not a C function
- When to use
printf/scanf and when to prefer getchar/putchar
Every interactive C program uses at least one input function and at least one output function. The C standard library provides two families: the formatted family (printf, scanf) and the unformatted family (getchar, putchar, gets, puts, getc, putc). The six questions below test whether you can tell them apart, and whether you know the return types — which matter far more than beginners expect.
MCQ — Input / Output in C
1. Which one of the following is the odd one out?
- a.
printf()
- b.
putchar()
- c.
scanf()
- d.
puts()
Answer: c. scanf()
printf(), putchar() and puts() all write text to the console. scanf() is the odd one because it reads from the console and stores the input in a variable.
2. What is the return type of getchar()?
- a.
char
- b.
int
- c.
char*
- d. Reading a character does not require a return type
- e. Return type depends on the compiler
- f.
EOF
- g. All of the above
Answer: b and f — int, returning EOF on error.
getchar() reads a character from standard input (stdin, i.e. the keyboard) and returns it as an int, or returns EOF if an error or end-of-file occurs. Returning int rather than char is essential: it lets getchar() return all 256 possible character values plus a distinct EOF value, which char alone could not represent.
3. Which one of the following are formatted input/output functions?
- a.
printf() and scanf()
- b.
printf() and print()
- c.
getchar() and putchar()
- d.
gets() and puts()
- e.
getc() and putc()
Answer: a. printf() and scanf()
In the list given above, only printf() and scanf() belong to the category of formatted I/O functions — they take a format string ("%d", "%lf", …) that tells them how to convert the data. All the other functions listed are unformatted: they read or write raw characters or strings without any conversion specification.
4. Which one of the following functions reads a character from standard input (the keyboard)?
- a.
printf()
- b.
print()
- c.
getchar()
- d.
puts()
- e.
putc()
Answer: c. getchar()
getchar() reads a single character from the standard input stream (stdin) — regardless of what that character is — and returns it to the program. The other functions in the list write to output: printf and puts to stdout, putc to any output stream. print() is not a C function at all.
5. Which one of the following functions writes a character to any output stream and returns an int?
- a.
printf()
- b.
print()
- c.
getchar()
- d.
puts()
- e.
putc()
Answer: e. putc()
The return type of putc() is int. It returns the character written, cast from unsigned char to int, or EOF on error. Because EOF has the value -1 and cannot be represented as an unsigned char, the return type must be int — that is how putc() can signal both success and failure through a single return value.
6. Which one of the following is not a standard formatted input or output function in C language?
- a.
printf()
- b.
print()
- c.
getchar()
- d.
puts()
- e.
putc()
Answer: b. print()
The print() function does not exist in the C programming language. All the other entries in the list are inbuilt functions available in the C standard library — printf, getchar, puts and putc.
Frequently Asked Questions
❓ Quick Viva Questions & Answers
Q1. What is the difference between formatted and unformatted I/O?
Formatted I/O functions (printf, scanf) take a format string that specifies how each value should be converted between its binary representation and text. Unformatted I/O functions (getchar, putchar, gets, puts, getc, putc) read or write raw characters or strings without any conversion specifier.
Q2. Why is the return type of getchar() an int and not a char?
Because getchar() must be able to return all 256 possible byte values and a distinct EOF indicator. A char cannot represent both — especially if char is signed, one valid character value would collide with EOF (-1). Returning int gives 257 distinct values to work with, so EOF is unambiguous.
Q3. What is EOF, and what value does it have?
EOF stands for End Of File. It is a macro defined in <stdio.h>, usually as #define EOF (-1). Input functions return EOF when they reach the end of the stream or encounter an error. On the keyboard, Ctrl+D (Linux/macOS) or Ctrl+Z followed by Enter (Windows) signals end-of-file.
Q4. What is the difference between putchar() and putc()?
putchar(c) writes the character c to stdout and is usually implemented as a macro. putc(c, stream) writes c to any output stream (a file, for example) and is a genuine function. Both return an int — the character written on success, or EOF on failure.
Q5. Is print() really not a C function?
Correct — there is no standard C function named print(). Many other languages (Python, JavaScript, Go) have a print builtin, which is why beginners sometimes carry the name into C. In C, the output function is printf().
Q6. Which I/O function should I prefer for reading a single character?
Use getchar() when you want a quick, unformatted read from stdin. Use scanf(" %c", &ch) when you need formatted reading (and remember the leading space in the format string to skip whitespace — otherwise scanf will happily consume leftover newlines from previous input).
Q7. Why does the C standard keep both formatted and unformatted functions?
Efficiency and control. Unformatted functions are lighter — a single character read or write needs no format parsing. Formatted functions are convenient but carry overhead. A high-performance parser might use getchar() in a loop, while an ordinary application would use printf/scanf for readability.
Previous Topic: >> Simple C Programs
||
Next Topic: >> Arithmetic Operators