0
0
Cprogramming~10 mins

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

Choose your learning style9 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.