C Programming Tricky Questions

Tricky Questions on printf() and scanf() Statements

What is the return value of printf() function?
printf() returns the number of characters successfully printed (excluding the null terminator). On error, it returns a negative value. Example: printf("Hello") returns 5.
Tricky Point: Many programmers think printf() returns void, but it actually returns an int. This is often used in tricky interview questions.
What happens if you use %d to print a float value?
It leads to undefined behavior. The program may crash, print garbage value, or give unpredictable results. Always use correct format specifiers: %f for float, %lf for double.
float f = 3.14; printf("%d", f); // WRONG: undefined behavior printf("%f", f); // CORRECT: prints 3.140000
What is the difference between %i and %d in printf()?
In printf(), %i and %d are identical - both print signed decimal integers. The difference matters only in scanf() where %i can read octal (0 prefix) and hexadecimal (0x prefix) numbers.
What does scanf("%d%d", &a, &b); expect as input?
It expects two integers separated by whitespace (space, tab, or newline). scanf() automatically skips whitespace before numeric inputs. Example inputs: "10 20" or "10\\n20".
What happens if you forget & (address-of) operator in scanf()?
It leads to undefined behavior, usually a segmentation fault. scanf() needs memory addresses to store values. Without &, it tries to write to the value itself as an address.
Warning: This is a common beginner mistake that can crash the program. Modern compilers may give warnings for this.
What is the difference between %s and %c in scanf()?
%s reads a string (skips leading whitespace, reads until whitespace). %c reads a single character (including whitespace). To skip whitespace with %c, use " %c" (note the space).
What does printf("%*d", width, num); do?
The * allows specifying field width at runtime. First argument becomes width, second becomes the value. Example: printf("%*d", 5, 10); prints "   10" (10 right-aligned in 5 spaces).
Why does scanf("%d", &ch); with char variable cause problems?
scanf() writes sizeof(int) bytes (typically 4) into memory, but char variable has only 1 byte. This causes buffer overflow, corrupting adjacent memory. Always match variable type with format specifier.
What is the return value of scanf()?
scanf() returns the number of successfully read items. Returns EOF on end-of-file or error. Example: scanf("%d %d", &a, &b) returns 2 if both integers read successfully.
What does printf("%d", printf("Hello")); print?
It prints "Hello5". Inner printf("Hello") prints "Hello" and returns 5 (characters printed). Outer printf("%d", 5) then prints "5".
Tricky Point: This demonstrates that printf() can be used as an argument to another printf() since it returns a value.
Why should you avoid using gets() instead of fgets() or scanf() with limit?
gets() doesn't check buffer boundaries, leading to buffer overflow vulnerabilities. It's deprecated and removed from C11. Always use fgets() or scanf("%ns", str) where n is buffer size-1.
What is the difference between %f, %e, and %g for floating point?
%f: decimal notation (3.140000). %e: scientific notation (3.140000e+00). %g: uses %e or %f, whichever is shorter (3.14). %g removes trailing zeros.
What happens with scanf("%[^\\n]", str);?
It reads a string until newline (excluding newline). The [^\\n] is a scanset that reads all characters except newline. Similar to gets() but safer if you specify buffer size: scanf("%99[^\\n]", str).
What does printf("%p", &var); print?
It prints the memory address of variable in hexadecimal format. %p is for printing pointers. The format is implementation-defined but typically hexadecimal with 0x prefix.
Why does scanf("%d", &num); leave newline in input buffer?
scanf("%d") reads the integer but leaves the newline (from pressing Enter) in the buffer. This can cause issues with subsequent scanf("%c") which reads the leftover newline.
scanf("%d", &num); // User types: 10<Enter> // Input buffer contains: '1','0','\n' // scanf reads '1','0', leaves '\n' scanf("%c", &ch); // ch gets '\n', not next character!
What is the purpose of %n format specifier?
%n doesn't print anything. It stores the number of characters printed so far into the corresponding argument (pointer to int). Useful for formatting and alignment.
int count; printf("Hello%nWorld", &count); // After this, count = 5 (characters before %n)
What does printf("%#x", num); do?
# flag adds prefix: for %x adds "0x", for %o adds "0", for %e, %f, %g forces decimal point. printf("%#x", 255) prints "0xff".
What is the difference between %hd and %d?
%hd is for short int (half the size of int). %d is for int. Using wrong specifier causes undefined behavior. Similarly: %ld for long, %lld for long long.
What happens with printf("%.2f", 3.14159);?
It prints "3.14". The .2 specifies precision: 2 digits after decimal point. For floating point, precision controls decimal places. For strings, precision controls maximum characters to print.
Why is scanf("%s", str) dangerous?
It reads until whitespace but doesn't limit input size, causing buffer overflow if input exceeds array size. Safer alternatives: scanf("%99s", str) (reads max 99 chars) or fgets(str, 100, stdin).
What does printf("%-10d", num); do?
- flag left-justifies output within field width. printf("%-10d", 123) prints "123       " (123 followed by 7 spaces, total width 10).
What is the issue with scanf("%d\n", &num);?
The \n in format string requires actual newline in input. User must press Enter twice. This confuses users and is usually a mistake. Never put \n at end of scanf() format string.
What does printf("%04d", 12); print?
It prints "0012". 0 flag pads with leading zeros instead of spaces. 4 is field width. Useful for fixed-width numeric output like timestamps: "01:05" not "1:5".
How to clear input buffer after scanf()?
Use while((getchar()) != '\n'); to consume leftover characters. Or use scanf("%*[^\\n]"); scanf("%*c"); to discard until newline and discard newline. Needed when mixing scanf() with gets()/fgets().
What is the difference between puts() and printf()?
puts(str) prints string followed by newline, doesn't support formatting. printf() supports formatting, doesn't automatically add newline. puts() is faster for simple string output.
puts("Hello"); // Prints "Hello\n" printf("Hello"); // Prints "Hello" (no newline) printf("Hello\n"); // Same as puts("Hello")
Note: These tricky questions test deep understanding of printf() and scanf() behavior, including format specifiers, return values, buffer handling, and common pitfalls. Mastering these concepts is crucial for writing robust C programs.
Previous printf() & scanf() Functions Next