📖 Scenario: You are creating a simple program that stores and displays a name using character arrays in C. This is like writing a name on a piece of paper and then reading it out loud.
🎯 Goal: Build a program that stores a name in a character array, counts the number of characters, and then prints the name and its length.
📋 What You'll Learn
Create a character array with a specific name
Create an integer variable to store the length of the name
Use a loop to count the characters in the array
Print the name and the length
💡 Why This Matters
🌍 Real World
Character arrays are used to store text like names, messages, or any words in many programs.
💼 Career
Understanding how to work with strings and arrays is essential for programming in C, especially in systems programming and embedded software.
Progress0 / 4 steps
1
Create a character array with the name
Create a character array called name and initialize it with the string "Alice".
C
Hint
Use double quotes to create a string in C, like char name[] = "Alice";.
2
Create an integer variable for length
Create an integer variable called length and set it to 0.
C
Hint
Use int length = 0; to create the variable.
3
Count the characters in the array
Use a for loop with an integer variable i starting at 0 to count characters in name until you find the null character '\0'. Increase length by 1 for each character.
C
Hint
Use name[i] != '\0' to check for the end of the string.
4
Print the name and its length
Use printf to print the text Name: followed by the name array, then print Length: followed by the length variable on the next line.
C
Hint
Use printf("Name: %s\n", name); and printf("Length: %d\n", 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
Step 1: Understand character arrays
A character array in C is a collection of characters stored in contiguous memory locations.
Step 2: Identify its use
It is mainly used to store text like words or sentences, not numbers or calculations.
Final Answer:
To store a sequence of characters, like a word or sentence -> Option A
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
Step 1: Understand string termination in C
Strings in C end with a special character '\0' to mark the end.
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.
Final Answer:
char word[6] = "hello"; -> Option A
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
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'.
Step 2: Print character at index 3
Printing str[3] prints the null character which is invisible, so no visible output appears.
Final Answer:
0 (null character, no visible output) -> Option B
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
Step 1: Check array size vs string length
"hello world" has 11 characters plus '\0' = 12 total, but array size is only 5.
Step 2: Understand consequences
Copying this string will overflow the array, causing undefined behavior.
Final Answer:
Array size is too small for the string -> Option D
Quick Check:
Array must fit string + '\0' [OK]
Hint: Array size must be >= string length + 1 [OK]