Bird
Raised Fist0
Cprogramming~10 mins

Character arrays - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Character arrays
Declare char array
↓
Assign characters to array elements
↓
Add null terminator '\0'
↓
Use array as string
↓
Print or manipulate characters
↓
End
This flow shows how a character array is declared, filled with characters, terminated with a null character, and then used as a string.
Execution Sample
C
char name[6] = {'H', 'e', 'l', 'l', 'o', '\0'};
printf("%s\n", name);
This code creates a character array holding the word 'Hello' and prints it as a string.
Execution Table
StepActionArray ContentNull Terminator PresentOutput
1Declare array 'name' of size 6['?', '?', '?', '?', '?', '?']No
2Assign 'H' to name[0]['H', '?', '?', '?', '?', '?']No
3Assign 'e' to name[1]['H', 'e', '?', '?', '?', '?']No
4Assign 'l' to name[2]['H', 'e', 'l', '?', '?', '?']No
5Assign 'l' to name[3]['H', 'e', 'l', 'l', '?', '?']No
6Assign 'o' to name[4]['H', 'e', 'l', 'l', 'o', '?']No
7Assign '\0' to name[5]['H', 'e', 'l', 'l', 'o', '\0']Yes
8Print name with %s['H', 'e', 'l', 'l', 'o', '\0']YesHello
9End of program['H', 'e', 'l', 'l', 'o', '\0']Yes
💡 Program ends after printing the string 'Hello' stored in the character array.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5After Step 6After Step 7Final
name['?', '?', '?', '?', '?', '?']['H', '?', '?', '?', '?', '?']['H', 'e', '?', '?', '?', '?']['H', 'e', 'l', '?', '?', '?']['H', 'e', 'l', 'l', '?', '?']['H', 'e', 'l', 'l', 'o', '?']['H', 'e', 'l', 'l', 'o', '\0']['H', 'e', 'l', 'l', 'o', '\0']
Key Moments - 3 Insights
Why do we need the '\0' character at the end of the array?
The '\0' marks the end of the string in C. Without it, functions like printf don't know where the string ends and may print garbage or cause errors. See step 7 and 8 in the execution_table.
Can we print the array without the '\0' terminator?
No, printing with %s expects a null-terminated string. Without '\0', printf will continue reading memory beyond the array, causing undefined behavior. This is why step 7 is crucial.
Why do we declare the array size as 6 for the word 'Hello'?
Because 'Hello' has 5 letters plus 1 for the '\0' terminator. The array must have space for all characters plus the null character. This is shown in step 1.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 6. What is the content of name[4]?
A'o'
B'l'
C'\0'
D'e'
💡 Hint
Check the 'Array Content' column at step 6 in the execution_table.
At which step does the null terminator '\0' get assigned to the array?
AStep 5
BStep 8
CStep 7
DStep 6
💡 Hint
Look for the step where 'Null Terminator Present' changes to 'Yes' in the execution_table.
If we forget to add '\0' at the end, what will happen when printing the array?
AIt prints the string correctly
BIt may print extra garbage characters or crash
CIt prints only the first character
DIt prints nothing
💡 Hint
Refer to the key_moments explanation about the importance of '\0' and step 8 in the execution_table.
Concept Snapshot
Character arrays hold sequences of characters.
Always end with '\0' to mark string end.
Declare size to fit all chars + '\0'.
Use %s in printf to print as string.
Without '\0', printing causes errors.
Full Transcript
This lesson shows how to create and use character arrays in C. We start by declaring an array of characters with enough space for the word plus a null terminator. Then, we assign each character to the array elements one by one. The null terminator '\0' is added at the end to mark the string's end. When printing with printf and %s, the function reads characters until it finds '\0'. Without this terminator, printing can cause errors or garbage output. The execution table traces each step, showing how the array fills up and when the null terminator is added. Key moments clarify why the null terminator is essential and how array size relates to string length.

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