0
0
Cprogramming~20 mins

String input and output in C - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
String Input and Output Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this C code snippet?
Consider the following C program. What will it print when run?
C
#include <stdio.h>

int main() {
    char str[10];
    scanf("%s", str);
    printf("Hello, %s!", str);
    return 0;
}
ACompilation error
BHello, !
CHello, world!\n
DHello, world!
Attempts:
2 left
💡 Hint
Think about what scanf with %s reads and how printf prints it.
Predict Output
intermediate
2:00remaining
What does this code print?
What is the output of this C program?
C
#include <stdio.h>

int main() {
    char s[6] = "Hello";
    printf("%s\n", s);
    return 0;
}
AHello
BHello\n
CHello\0
DCompilation error
Attempts:
2 left
💡 Hint
Remember how strings are stored and printed in C.
Predict Output
advanced
2:00remaining
What is the output of this code with fgets?
What will this C program print if the user inputs: Hello World\n?
C
#include <stdio.h>

int main() {
    char buffer[6];
    fgets(buffer, 6, stdin);
    printf("%s", buffer);
    return 0;
}
AHello World
BHello
CHello\n
DHello\0
Attempts:
2 left
💡 Hint
fgets reads up to size-1 characters including newline if present.
Predict Output
advanced
2:00remaining
What error does this code cause?
What happens when this C code runs?
C
#include <stdio.h>

int main() {
    char str[5];
    scanf("%s", str);
    printf("%s", str);
    return 0;
}
ARuntime error: segmentation fault always
BCompilation error due to missing semicolon
CBuffer overflow if input longer than 4 characters
DPrints input safely
Attempts:
2 left
💡 Hint
Think about how scanf reads strings and array size.
🧠 Conceptual
expert
2:00remaining
How many characters are read by fgets in this code?
Given this code, how many characters maximum will fgets read from input?
C
#include <stdio.h>

int main() {
    char input[10];
    fgets(input, sizeof(input), stdin);
    return 0;
}
A9 characters plus the null terminator
B10 characters plus the null terminator
CExactly 10 characters, no null terminator
D8 characters plus the null terminator
Attempts:
2 left
💡 Hint
Remember how fgets uses the size parameter.