A string in C is an array of characters ending with a special character called the null terminator '\0' which marks the end of the string.
Click to reveal answer
beginner
Why do C strings end with a null terminator '\0'?
The null terminator '\0' tells functions where the string ends in memory since C does not store string length separately.
Click to reveal answer
beginner
How is a string stored in memory in C?
A string is stored as a sequence of characters in consecutive memory locations, followed by a null terminator '\0'.
Click to reveal answer
intermediate
What happens if a string in C does not have a null terminator?
Without a null terminator, functions that process strings may read beyond the intended end, causing undefined behavior or errors.
Click to reveal answer
beginner
How can you declare and initialize a string in C?
You can declare a string as a char array with initialization like: char str[] = "hello"; which automatically adds the null terminator.
Click to reveal answer
What character marks the end of a string in C?
A' ' (space)
B'\n' (newline)
C'\0' (null terminator)
D'\t' (tab)
✗ Incorrect
The null terminator '\0' marks the end of a string in C.
How is a string stored in memory in C?
AAs a sequence of characters in contiguous memory with a null terminator
BAs a number representing the string length
CAs a linked list of characters
DAs a pointer to a string object
✗ Incorrect
Strings in C are stored as contiguous characters followed by a null terminator.
What happens if a C string is missing the null terminator?
AFunctions may read beyond the string causing errors
BThe string is treated as empty
CThe string length is calculated correctly anyway
DThe string is automatically fixed by the compiler
✗ Incorrect
Without a null terminator, string functions may read memory beyond the string causing undefined behavior.
Which of these is a correct way to declare a string in C?
Achar str = "hello";
Bchar str[] = "hello";
Cstring str = "hello";
Dchar str[5] = 'hello';
✗ Incorrect
char str[] = "hello"; declares a char array initialized with the string and null terminator.
What is the size in memory of the string "cat" in C?
ADepends on the system
B3 bytes
C5 bytes
D4 bytes
✗ Incorrect
The string "cat" has 3 characters plus 1 null terminator, so 4 bytes total.
Explain how strings are represented and stored in memory in C.
Think about how C knows where the string ends.
You got /4 concepts.
Describe the importance of the null terminator in C strings and what can go wrong if it is missing.
Consider how string functions find the string length.
You got /3 concepts.
Practice
(1/5)
1. Which of the following correctly describes how strings are stored in C?
easy
A. As an array of characters ending with a null character '\0'.
B. As a linked list of characters.
C. As a single integer representing the string length.
D. As a floating-point number encoding characters.
Solution
Step 1: Understand C string storage
In C, strings are stored as arrays of characters with a special null character '\0' marking the end.
Step 2: Compare options with this knowledge
Only As an array of characters ending with a null character '\0'. correctly states this; others describe different data structures or types.
Final Answer:
As an array of characters ending with a null character '\0'. -> Option A
Quick Check:
C strings end with '\0' = A [OK]
Hint: Remember C strings always end with '\0' to mark their end [OK]
Common Mistakes:
Thinking strings are linked lists in C
Assuming strings store length explicitly
Confusing string storage with other data types
2. Which of the following is the correct way to declare and initialize a string literal in C?
easy
A. char str = "hello";
B. char str[5] = 'hello';
C. char str[] = "hello";
D. char str[] = {'h','e','l','l','o'};
Solution
Step 1: Check string declaration syntax
In C, to store a string literal, use an array of chars with double quotes and square brackets.
Step 2: Evaluate each option
A uses char (single char) with double quotes (wrong). B uses single quotes for multiple chars (wrong). D misses null terminator '\0'. C correctly declares and initializes with null terminator.
Final Answer:
char str[] = "hello"; -> Option C
Quick Check:
Use char array with double quotes = C [OK]
Hint: Use char array with double quotes for string literals [OK]
Common Mistakes:
Using single quotes for strings
Not including null terminator
Declaring char variable instead of array
3. What will be the output of the following C code?
char str[] = "abc"; printf("%c", str[3]);
medium
A. c
B. \0 (null character, prints nothing visible)
C. a
D. Runtime error
Solution
Step 1: Understand string indexing and null terminator
The string "abc" is stored as {'a','b','c','\0'}. Index 3 is the null character '\0'.
Step 2: Determine printf output for str[3]
Printing '%c' with '\0' prints nothing visible (null character), no error occurs.
Final Answer:
\0 (null character, prints nothing visible) -> Option B
Quick Check:
Index 3 is '\0' = A [OK]
Hint: Remember string ends with '\0' at last index + 1 [OK]
Common Mistakes:
Assuming str[3] is 'a' or 'c'
Expecting runtime error for accessing null char
Confusing null char with printable character
4. Identify the error in the following C code snippet:
char str[5] = "hello"; str[5] = '!';
medium
A. Array index out of bounds when assigning str[5].
B. Incorrect use of single quotes for string.
C. Missing null terminator in string initialization.
D. No error, code runs fine.
Solution
Step 1: Analyze array size and indexing
Array str has size 5, valid indices are 0 to 4. Accessing str[5] is out of bounds.
Step 2: Check string initialization and assignment
"hello" fits exactly 5 chars including '\0'? No, 'hello' needs 6 chars including '\0'. But main error is str[5] assignment which is invalid index.
Final Answer:
Array index out of bounds when assigning str[5]. -> Option A