Bird
Raised Fist0
Cprogramming~10 mins

String input and output in C - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - String input and output
Start Program
↓
Declare char array
↓
Input string using scanf
↓
Store input in array
↓
Print string using printf
↓
End Program
The program declares a character array, reads a string from the user, stores it, then prints it back.
Execution Sample
C
#include <stdio.h>
int main() {
  char name[20];
  scanf("%19s", name);
  printf("Hello, %s!\n", name);
  return 0;
}
This program reads a string input from the user and prints a greeting with that string.
Execution Table
StepActionInput/ValueVariable StateOutput
1Declare char array 'name' of size 20-name = uninitialized-
2Read string input with scanfUser types: Alicename = "Alice"-
3Print greeting with printf-name = "Alice"Hello, Alice!
4Return 0 and end program---
💡 Program ends after printing the greeting.
Variable Tracker
VariableStartAfter InputFinal
nameuninitialized"Alice""Alice"
Key Moments - 3 Insights
Why do we use %19s in scanf instead of just %s?
Using %19s limits input to 19 characters plus the null terminator, preventing buffer overflow as shown in step 2 of the execution_table.
What happens if the user types a string longer than 19 characters?
scanf stops reading after 19 characters, so the extra characters remain in the input buffer, preventing overflow and keeping 'name' safe as seen in step 2.
Why does printf use %s to print the string?
The %s format tells printf to print characters from the array until it finds the null terminator, which is how strings are stored in C, demonstrated in step 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'name' after step 2?
Auninitialized
B"Alice"
C"Hello"
Dempty string
💡 Hint
Check the 'Variable State' column in row for step 2.
At which step does the program output the greeting?
AStep 3
BStep 2
CStep 1
DStep 4
💡 Hint
Look at the 'Output' column in the execution_table.
If the user inputs a string longer than 19 characters, what prevents buffer overflow?
AUsing %s in scanf
BUsing printf to print the string
CUsing %19s in scanf
DDeclaring name as char array
💡 Hint
Refer to the key_moments explanation about scanf format specifier.
Concept Snapshot
String input and output in C:
- Declare char array to hold string
- Use scanf("%19s", array) to safely read input
- Use printf("%s", array) to print string
- Strings end with null character '\0'
- Limit input size to avoid overflow
Full Transcript
This program shows how to read a string from the user and print it in C. First, a character array named 'name' is declared to hold the input. Then, scanf with %19s reads up to 19 characters safely, storing them in 'name'. After input, printf prints a greeting including the string. The program ends after printing. Using %19s prevents buffer overflow by limiting input size. The string is printed until the null terminator is found. This simple flow helps beginners understand string input and output in C.

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

  1. Step 1: Understand string input functions

    scanf with "%s" reads a single word string until whitespace.
  2. Step 2: Identify correct function for single word input

    printf is for output, gets is unsafe, fgets reads whole line including spaces.
  3. Final Answer:

    scanf("%s", str); -> Option D
  4. 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

  1. Step 1: Identify the print format specifier for strings

    "%s" is used in printf to print strings.
  2. Step 2: Match correct function and format

    printf with "%d" is for integers, scanf is for input, gets reads input.
  3. Final Answer:

    printf("%s", str); -> Option B
  4. 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?
#include <stdio.h>
int main() {
    char str[20];
    scanf("%s", str);
    printf("%s", str);
    return 0;
}

If the user inputs: Hello World
medium
A. Hello
B. Hello World
C. World
D. Compilation error

Solution

  1. Step 1: Understand scanf with %s behavior

    scanf("%s", str) reads input until first whitespace, so it reads only "Hello".
  2. Step 2: Output printed by printf

    printf("%s", str) prints the stored string, which is "Hello".
  3. Final Answer:

    Hello -> Option A
  4. Quick 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
A. char array size is too large
B. printf("%s", str) cannot print strings
C. scanf("%s", str) reads only until first space, not full line
D. Missing return type for main

Solution

  1. 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.
  2. Step 2: Check other parts of code

    printf("%s", str) correctly prints string, array size is sufficient, main has return type int.
  3. Final Answer:

    scanf("%s", str) reads only until first space, not full line -> Option C
  4. Quick 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
A. fgets(str, 100, stdin);
B. scanf("%s", str);
C. printf("%s", str);
D. gets(str);

Solution

  1. Step 1: Understand functions for reading strings with spaces

    scanf("%s", str) reads only until space, gets is unsafe and deprecated.
  2. Step 2: Identify safe function to read full line

    fgets(str, 100, stdin) reads up to 99 characters including spaces safely.
  3. Final Answer:

    fgets(str, 100, stdin); -> Option A
  4. Quick 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 strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard