C Input/Output - Tricky MCQ
Tricky Questions on Input/Output Functions
What is the return value of printf() function?
printf() returns the number of characters successfully written to the output stream (excluding the null terminator). On failure, it returns a negative value. This is useful for checking if output was successful and for formatting.
What happens if format specifier doesn't match argument type in printf()?
When format specifiers don't match argument types in printf(), it leads to undefined behavior. The compiler may not catch it (especially with variables), and you'll get garbage output. This is because printf() uses varargs and relies on the format string to interpret arguments correctly.
What is the difference between puts() and printf() for string output?
puts(str) writes the string str followed by a newline character to stdout. printf("%s", str) only writes the string without automatically adding a newline. puts() is generally faster for simple string output as it doesn't parse format strings.
What does scanf() return?
scanf() returns the number of input items successfully matched and assigned. This is useful for error checking. If input fails before any assignment, it returns EOF. If there's a matching failure, it returns the number of items successfully read before the failure.
Why is gets() dangerous and deprecated?
gets() reads input until newline or EOF, with no way to limit input size. If input exceeds buffer size, it causes buffer overflow (writing beyond allocated memory), leading to crashes or security vulnerabilities. It was removed from C11. Use fgets() instead.
What is the difference between %i and %d in printf/scanf?
In printf(), %i and %d are identical - both print signed decimal integers. In scanf(), %d reads only decimal numbers, while %i interprets input based on prefix: decimal (no prefix), octal (0 prefix), or hexadecimal (0x or 0X prefix).
What does %n format specifier do in printf()?
The %n format specifier is unique - it doesn't print anything. Instead, it stores the number of characters that have been printed by printf() up to that point into an int pointer argument. Useful for formatting or progress tracking.
How to flush output buffer in C?
Standard output is typically line-buffered when connected to terminal. fflush(stdout) explicitly flushes the buffer. Printing a newline (\n) also flushes the buffer when output is line-buffered. For immediate output without newline, use fflush(stdout).
What happens if you don't use & with variables in scanf()?
scanf() needs memory addresses (& operator) to store input values. Without &, it treats the variable's value as an address, leading to writing input to a random memory location → undefined behavior (crash, corruption, etc.). Exception: arrays (array name decays to pointer).
What is the difference between fprintf() and printf()?
printf() always writes to stdout. fprintf() writes to any FILE* stream (files, stderr, stdout, etc.). printf(format, ...) is equivalent to fprintf(stdout, format, ...). Use fprintf(stderr, ...) for error messages.
What does the * modifier do in printf format specifiers?
In printf() format strings, * allows dynamic width/precision from an argument instead of hardcoded values. The argument must be an int. Useful when width/precision isn't known at compile time.
What's wrong with: scanf("%s", &name); where name is char array?
For arrays, name decays to &name[0] (pointer to first element). &name gives the address of the array itself (same value but different type: char(*)[100] vs char*). It usually works but gives a type mismatch warning. Best practice: use name without &.
What is the purpose of fpurge()/fflush() on input streams?
fflush() on output streams writes buffered data. On input streams, behavior is implementation-defined. fpurge() (BSD) or __fpurge() (Glibc) discards unread input. Standard portable way: read and discard with a loop. Used to clear leftover input (like newline after scanf()).
What is the difference between %f, %e, and %g for floating point?
%f prints in decimal notation (e.g., 123.456000). %e prints in scientific notation (e.g., 1.234560e+02). %g uses either %f or %e, whichever is more compact (removes trailing zeros). %a (C99) prints hexadecimal floating point.
How to read/write binary data in C?
For binary I/O: fread() and fwrite() (standard library) read/write raw bytes. read() and write() (POSIX system calls) also work. Open files in binary mode ("rb", "wb") to avoid newline translation. Text functions like fprintf() convert data to text representation.