Bird
Raised Fist0
Cprogramming~5 mins

Character arrays - Cheat Sheet & Quick Revision

Choose your learning style10 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:
char word[6] = {'h', 'e', 'l', 'l', 'o', '\0'};
or simply:
char word[] = "hello";
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:
name[0] = 'J';
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.

      Practice

      (1/5)
      1. What is a character array in C used for?
      easy
      A. To store a sequence of characters, like a word or sentence
      B. To store only numbers
      C. To perform mathematical calculations
      D. To create a list of integers

      Solution

      1. Step 1: Understand character arrays

        A character array in C is a collection of characters stored in contiguous memory locations.
      2. Step 2: Identify its use

        It is mainly used to store text like words or sentences, not numbers or calculations.
      3. Final Answer:

        To store a sequence of characters, like a word or sentence -> Option A
      4. Quick Check:

        Character array = text storage [OK]
      Hint: Character arrays hold text, not numbers or math [OK]
      Common Mistakes:
      • Thinking character arrays store numbers only
      • Confusing character arrays with integer arrays
      • Assuming character arrays perform calculations
      2. Which of the following is the correct way to declare a character array to hold the word "hello" in C?
      easy
      A. char word[6] = "hello";
      B. char word[5] = {'h', 'e', 'l', 'l', 'o'};
      C. char word[] = {'h', 'e', 'l', 'l', 'o', '\0'};
      D. char word[5] = "hello";

      Solution

      1. Step 1: Understand string termination in C

        Strings in C end with a special character '\0' to mark the end.
      2. Step 2: Check array size and initialization

        char word[6] = "hello"; declares an array of size 6 (5 letters + '\0') and initializes it with "hello" correctly.
      3. Final Answer:

        char word[6] = "hello"; -> Option A
      4. Quick Check:

        String needs space for '\0' [OK]
      Hint: Remember to include space for '\0' in string arrays [OK]
      Common Mistakes:
      • Not allocating space for the null terminator '\0'
      • Using single quotes for strings
      • Declaring array size too small
      3. What will be the output of the following C code?
      char str[] = "cat";
      printf("%c", str[3]);
      medium
      A. t
      B. 0 (null character, no visible output)
      C. c
      D. Compilation error

      Solution

      1. Step 1: Understand string indexing

        String "cat" has characters at indices 0:'c', 1:'a', 2:'t', and index 3 is the null terminator '\0'.
      2. Step 2: Print character at index 3

        Printing str[3] prints the null character which is invisible, so no visible output appears.
      3. Final Answer:

        0 (null character, no visible output) -> Option B
      4. Quick Check:

        str[3] = '\0' (null) [OK]
      Hint: Remember strings end with '\0' at last index [OK]
      Common Mistakes:
      • Expecting 't' at index 3 instead of '\0'
      • Thinking it causes an error
      • Confusing index positions
      4. Identify the error in this code snippet:
      char name[5];
      strcpy(name, "hello world");
      medium
      A. No error, code works fine
      B. Incorrect use of strcpy function
      C. Missing #include <string.h>
      D. Array size is too small for the string

      Solution

      1. Step 1: Check array size vs string length

        "hello world" has 11 characters plus '\0' = 12 total, but array size is only 5.
      2. Step 2: Understand consequences

        Copying this string will overflow the array, causing undefined behavior.
      3. Final Answer:

        Array size is too small for the string -> Option D
      4. Quick Check:

        Array must fit string + '\0' [OK]
      Hint: Array size must be >= string length + 1 [OK]
      Common Mistakes:
      • Ignoring array size limits
      • Assuming strcpy checks size
      • Forgetting null terminator space
      5. Given the code below, what will be the output?
      char arr[10] = "abc";
      arr[3] = 'd';
      arr[4] = '\0';
      printf("%s", arr);
      hard
      A. abc\0d
      B. abc
      C. abcd
      D. Runtime error

      Solution

      1. Step 1: Analyze initial array content

        arr is initialized with "abc" which is 'a','b','c','\0' plus extra space.
      2. Step 2: Modify characters and null terminator

        arr[3] = 'd' adds 'd' after 'c', then arr[4] = '\0' sets new string end after 'd'.
      3. Step 3: Print the string

        printf prints characters until '\0', so output is "abcd".
      4. Final Answer:

        abcd -> Option C
      5. Quick Check:

        String ends at arr[4] '\0' [OK]
      Hint: Null terminator controls string end in arrays [OK]
      Common Mistakes:
      • Ignoring manual null terminator placement
      • Expecting original string without changes
      • Confusing character and string printing
      Recommended

      Ready to prove it?

      Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

      10 questions~5 mineasy hard