What is input validation in C?
A
Checking if the user input matches expected format or type
D
Removing input from memory
Analysis & Theory
Input validation ensures the user's input meets expected criteria before being used.
Which function is safer for reading strings in C?
Analysis & Theory
`fgets()` is safer because it allows specifying the maximum number of characters to read.
Why is `gets()` considered dangerous?
B
It causes buffer overflows
C
It doesn't limit input size
Analysis & Theory
`gets()` does not check input size and is removed from the C11 standard due to security issues.
What does `scanf("%d", &x)` return?
B
The number of items successfully read
Analysis & Theory
`scanf()` returns the number of input items successfully matched and assigned.
How can you check if the user entered an integer using `scanf()`?
B
Check if scanf("%d", &x) == 1
Analysis & Theory
`scanf()` returns 1 if the integer input is successful, otherwise 0 or EOF.
Which input function allows detection of leftover characters (like `\n`)?
Analysis & Theory
`fgets()` reads until newline or size limit, allowing detection and cleanup of newline characters.
How can you clear the input buffer in C?
Analysis & Theory
Reading characters in a loop with `getchar()` clears leftover input, especially after invalid `scanf()`.
What type of input causes `scanf("%d", &x)` to fail?
Analysis & Theory
Letters (e.g., 'abc') don't match `%d` format and cause `scanf()` to fail.
Which method is **most secure** for numeric input validation?
C
fgets() with `sscanf()` and checks
Analysis & Theory
`fgets()` reads the full line, and `sscanf()` parses it safely with error checking.
What is a good practice when asking for user input?
A
Assume user enters correct data
B
Validate and sanitize input
Analysis & Theory
Always validate and sanitize user input to prevent crashes or incorrect behavior.