What function is commonly used in C to read a string from the user?
The scanf function is commonly used to read strings from the user in C, often with the format specifier %s.
Click to reveal answer
beginner
How do you print a string stored in a variable in C?
Use the printf function with the format specifier %s to print a string variable, for example: printf("%s", str);
Click to reveal answer
intermediate
What is a safe way to read a string input to avoid buffer overflow?
Use fgets instead of scanf to read strings safely by specifying the maximum number of characters to read, e.g., fgets(str, sizeof(str), stdin);
Click to reveal answer
intermediate
What happens if you use scanf("%s", str); and the input is longer than the buffer?
It causes a buffer overflow, which can overwrite memory and lead to crashes or security issues because scanf does not limit input length.
Click to reveal answer
beginner
How do you declare a string variable in C?
A string in C is an array of characters ending with a null character \0. For example: char str[100]; declares a string that can hold up to 99 characters plus the null terminator.
Click to reveal answer
Which function is safer to read a string input in C?
Afgets
Bscanf with %s
Cgets
Dprintf
✗ Incorrect
fgets is safer because it limits the number of characters read, preventing buffer overflow. gets is unsafe and deprecated. scanf with %s does not limit input size.
What format specifier is used to print a string in C?
A%d
B%s
C%f
D%c
✗ Incorrect
The %s format specifier is used with printf to print strings.
What does the null character \0 signify in a C string?
ASpace character
BStart of the string
CEnd of the string
DNew line
✗ Incorrect
The null character \0 marks the end of a string in C.
Which of these is a correct way to declare a string that can hold 50 characters?
Achar str;
Bint str[50];
Cstring str;
Dchar str[51];
✗ Incorrect
Strings in C are arrays of characters, so char str[51]; declares a string with space for 50 characters plus the null terminator.
What is the risk of using scanf("%s", str); without limiting input size?
ABuffer overflow
BSyntax error
CNo risk
DMemory leak
✗ Incorrect
Using scanf without limiting input size can cause buffer overflow, which may crash the program or cause security issues.
Explain how to safely read and print a string in C.
Think about how to avoid buffer overflow and how strings end in C.
You got /4 concepts.
Describe the difference between using scanf and fgets for string input in C.
Consider safety and how input is stored.
You got /4 concepts.
Practice
(1/5)
1. Which function is used to read a single word string from the user in C?
easy
A. fgets(str, size, stdin);
B. printf("%s", str);
C. gets(str);
D. scanf("%s", str);
Solution
Step 1: Understand string input functions
scanf with "%s" reads a single word string until whitespace.
Step 2: Identify correct function for single word input
printf is for output, gets is unsafe, fgets reads whole line including spaces.
Final Answer:
scanf("%s", str); -> Option D
Quick Check:
Single word input = scanf("%s", str); [OK]
Hint: Use scanf with %s for single word input [OK]
Common Mistakes:
Using printf instead of scanf for input
Using fgets expecting single word input
Using gets which is unsafe
2. Which of the following is the correct syntax to print a string variable str in C?
easy
A. printf("%d", str);
B. printf("%s", str);
C. scanf("%s", str);
D. gets(str);
Solution
Step 1: Identify the print format specifier for strings
"%s" is used in printf to print strings.
Step 2: Match correct function and format
printf with "%d" is for integers, scanf is for input, gets reads input.
Final Answer:
printf("%s", str); -> Option B
Quick Check:
Print string = printf("%s", str); [OK]
Hint: Use printf with %s to print strings [OK]
Common Mistakes:
Using %d to print strings
Using scanf instead of printf for output
Using gets for printing
3. What will be the output of the following C code?