0
0
Cprogramming~5 mins

Character arrays - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a character array in C?
A character array in C is a sequence of characters stored in contiguous memory locations. It is often used to store strings, which are sequences of characters ending with a special null character '\0'.
Click to reveal answer
beginner
How do you declare a character array to hold the word "hello"?
You can declare it like this: <br>char word[6] = {'h', 'e', 'l', 'l', 'o', '\0'};<br>or simply: <br>char word[] = "hello";<br>The extra space is for the null character '\0' that marks the end of the string.
Click to reveal answer
beginner
Why do character arrays used as strings in C end with a '\0' character?
The '\0' character, called the null terminator, tells functions where the string ends. Without it, functions like printf or strlen wouldn't know where the string stops and might read garbage data or cause errors.
Click to reveal answer
beginner
How can you change the first character of a character array named 'name' to 'J'?
You can assign it directly like this: <br>name[0] = 'J';<br>This changes the first character stored in the array to 'J'.
Click to reveal answer
intermediate
What happens if you forget to add the '\0' at the end of a character array used as a string?
If the '\0' is missing, functions that expect a string will keep reading memory past the array until they find a '\0'. This can cause unexpected behavior, crashes, or security issues.
Click to reveal answer
Which of the following correctly declares a character array to store the string "cat"?
Achar animal[3] = {'c', 'a', 't', '\0'};
Bchar animal[3] = "cat";
Cchar animal[] = {'c', 'a', 't'};
Dchar animal[4] = "cat";
What does the '\0' character represent in a character array?
AThe end of the string
BA tab character
CA newline character
DA space character
How do you access the third character in a character array named 'word'?
Aword[3]
Bword[2]
Cword(3)
Dword{2}
What will happen if you print a character array without a '\0' terminator using printf with %s?
AIt prints the array correctly
BIt prints nothing
CIt may print extra garbage characters or crash
DIt prints only the first character
Which of these is a valid way to initialize a character array with the string "dog"?
Achar pet[] = "dog";
Bchar pet[3] = "dog";
Cchar pet[2] = {'d', 'o', 'g', '\0'};
Dchar pet = "dog";
Explain what a character array is and how it is used to store strings in C.
Think about how words are stored as letters in a row with a special end marker.
You got /4 concepts.
    Describe why the null terminator '\0' is important in character arrays used as strings.
    Consider what happens if you don't tell where the string ends.
    You got /4 concepts.