Which function is commonly used to take basic input in C?
A
printf() B
cin C
scanf() D
input()
Analysis & Theory
`scanf()` is used to take input from the user in C.
What is the correct syntax to read an integer from the user?
A
scanf("%d", number); B
scanf("%d", &number); C
scanf("%i", number); D
read(number);
Analysis & Theory
You must pass the address of the variable using `&`, e.g., `scanf("%d", &number);`
Which function is used to read a string with spaces?
A
scanf("%s", str); B
gets(str); C
fgets(str, size, stdin); D
Both 2 and 3
Analysis & Theory
`gets()` and `fgets()` both read strings with spaces. However, `gets()` is unsafe and deprecated.
Why is `gets()` considered unsafe?
A
It doesn’t read spaces B
It does not check buffer overflow C
It doesn’t work on strings D
It terminates the program
Analysis & Theory
`gets()` can cause buffer overflows because it doesn’t check input length.
What is the format specifier for reading a float using `scanf()`?
A
%d B
%f C
%lf D
%c
Analysis & Theory
`%f` is the correct format specifier for reading a float in `scanf()`.
Which function is safer for reading strings in C?
A
scanf("%s", str); B
gets(str); C
fgets(str, sizeof(str), stdin); D
scanf("%[^
]", str);
Analysis & Theory
`fgets()` is safer as it limits the number of characters to prevent buffer overflows.
What does `scanf("%c", &ch);` read?
A
An integer B
A floating number C
A character D
A string
Analysis & Theory
`%c` reads a single character input from the user.
Which format specifier is used to read a double value using `scanf()`?
A
%f B
%lf C
%d D
%c
Analysis & Theory
To read a double value, use `%lf` in `scanf()`.
What happens if you enter more characters than expected in `scanf("%s", str);`?
A
It wraps to the next line B
It gives an error C
It may cause a buffer overflow D
It truncates safely
Analysis & Theory
`scanf("%s")` has no limit and can overflow the buffer.
What is the correct way to read a full line of text safely?
A
gets(str); B
scanf("%s", str); C
fgets(str, 100, stdin); D
input(str);
Analysis & Theory
`fgets()` allows safe reading of a full line of text including spaces.