We use string input and output to read words or sentences from the user and show text on the screen.
String input and output in C
Introduction
Syntax
C
char str[SIZE]; // To read a string scanf("%s", str); // To print a string printf("%s", str);
Note: scanf("%s", str); reads a word (no spaces).
To read a full line with spaces, use fgets(str, SIZE, stdin); instead.
Examples
C
char name[20]; scanf("%s", name); printf("Hello %s!", name);
C
char sentence[100]; fgets(sentence, 100, stdin); printf("You typed: %s", sentence);
Sample Program
This program asks the user to enter their name, reads it as a word, and then greets them by name.
C
#include <stdio.h> int main() { char name[30]; printf("Enter your name: "); scanf("%s", name); printf("Hello, %s!\n", name); return 0; }
Important Notes
Remember that scanf("%s", str); stops reading at the first space.
Use fgets() if you want to read sentences with spaces.
Always make sure the array size is big enough to hold the input plus the ending \0 character.
Summary
Use scanf("%s", str); to read a single word string.
Use printf("%s", str); to print a string.
Use fgets() to read full lines with spaces.
Practice
1. Which function is used to read a single word string from the user in C?
easy
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 DQuick 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
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 BQuick 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?
If the user inputs:
#include <stdio.h>
int main() {
char str[20];
scanf("%s", str);
printf("%s", str);
return 0;
}If the user inputs:
Hello Worldmedium
Solution
Step 1: Understand scanf with %s behavior
scanf("%s", str) reads input until first whitespace, so it reads only "Hello".Step 2: Output printed by printf
printf("%s", str) prints the stored string, which is "Hello".Final Answer:
Hello -> Option AQuick Check:
scanf with %s stops at space = Hello [OK]
Hint: scanf %s reads only until space, not full line [OK]
Common Mistakes:
- Expecting scanf %s to read full line with spaces
- Confusing scanf with fgets
- Assuming output includes 'World'
4. Identify the error in the following code snippet for reading a full line including spaces:
#include <stdio.h>
int main() {
char str[50];
scanf("%s", str);
printf("%s", str);
return 0;
}medium
Solution
Step 1: Analyze scanf with %s for input
scanf("%s", str) stops reading input at first whitespace, so it cannot read full line with spaces.Step 2: Check other parts of code
printf("%s", str) correctly prints string, array size is sufficient, main has return type int.Final Answer:
scanf("%s", str) reads only until first space, not full line -> Option CQuick Check:
scanf %s stops at space, so not full line input [OK]
Hint: Use fgets to read full line with spaces [OK]
Common Mistakes:
- Assuming scanf %s reads full line
- Thinking printf can't print strings
- Confusing array size issues
5. How can you correctly read a full line of text including spaces into a string
str of size 100 in C?hard
Solution
Step 1: Understand functions for reading strings with spaces
scanf("%s", str) reads only until space, gets is unsafe and deprecated.Step 2: Identify safe function to read full line
fgets(str, 100, stdin) reads up to 99 characters including spaces safely.Final Answer:
fgets(str, 100, stdin); -> Option AQuick Check:
Use fgets for full line input with spaces [OK]
Hint: Use fgets to read lines with spaces safely [OK]
Common Mistakes:
- Using scanf %s for full line input
- Using gets despite safety issues
- Confusing printf with input functions
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
