Challenge - 5 Problems
Character Arrays Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of character array initialization
What is the output of this C program?
C
#include <stdio.h> int main() { char arr[] = {'H', 'e', 'l', 'l', 'o'}; printf("%s", arr); return 0; }
Attempts:
2 left
💡 Hint
Remember how strings are represented in C and what %s expects.
✗ Incorrect
The array arr is not null-terminated, so printf with %s reads beyond the array until it finds a null character, causing undefined or garbage output.
❓ Predict Output
intermediate2:00remaining
Length of character array with null terminator
What will be the output of this program?
C
#include <stdio.h> #include <string.h> int main() { char arr[] = "Hello"; printf("%zu", strlen(arr)); return 0; }
Attempts:
2 left
💡 Hint
strlen counts characters until the null terminator, but does not include it.
✗ Incorrect
The string "Hello" is stored with a null terminator, so strlen returns 5, the number of visible characters.
🔧 Debug
advanced2:00remaining
Identify the error in character array assignment
What error will this code produce?
C
#include <stdio.h> int main() { char arr[5]; arr = "Hello"; printf("%s", arr); return 0; }
Attempts:
2 left
💡 Hint
Think about how arrays and pointers work in C.
✗ Incorrect
Arrays cannot be assigned to after declaration. The line arr = "Hello" is invalid and causes a compilation error.
📝 Syntax
advanced2:00remaining
Correct way to declare and initialize a character array
Which option correctly declares and initializes a character array to hold the string "Code" by explicitly including the null terminator?
Attempts:
2 left
💡 Hint
Remember the null terminator is needed for strings in C.
✗ Incorrect
Option C correctly allocates space for 5 characters including the null terminator and initializes the array properly.
🚀 Application
expert2:00remaining
Number of elements in a character array
Given the code below, what is the value of variable count after execution?
C
#include <stdio.h> int main() { char arr[] = "Programming"; int count = sizeof(arr) / sizeof(arr[0]); printf("%d", count); return 0; }
Attempts:
2 left
💡 Hint
Remember that string literals include the null terminator in arrays.
✗ Incorrect
The string "Programming" has 11 characters plus 1 null terminator, so the array size is 12 elements.