In this article, you will learn the basics of character array initialization in C. You will see different ways to declare and initialize character arrays. It also covers how character arrays store strings and the importance of the null terminator (\0).
What is a Character Array in C?
A character array is an array where each element holds a char type. It’s commonly used to store strings (a sequence of characters ending with a null character ‘\0’).
Syntax:
Here’s the basic syntax to declare a character array:
char arrayName[size];
Where:
- char is the data type.
- arrayName is the name of the array.
- size is the number of characters the array can hold.
Methods of Initializing Character Arrays
1. Using String Literals
You can initialize a character array directly with a string literal.​
char str[] = "Sanfoundry";
Here, str is automatically sized to accommodate the string and the null terminator.​
2. Specifying Size Explicitly
You can also define the size explicitly:
char str[11] = "Sanfoundry";
- You must include space for the null character (‘\0’).
- If you write char str[10] = “Sanfoundry”; → Error or warning: too many initializers.
3. Initializing with Individual Characters
Another approach is to initialize each character individually.​
char str[] = { 'S', 'a', 'n', 'f', 'o', 'u', 'n', 'd', 'r', 'y', '\0' };
This method gives you control over each character, including the null terminator.​
4. Partial Initialization
If you provide fewer initializers than the array size, the remaining elements are set to zero.​
char str[15] = "Sanfoundry";
In this case, “Sanfoundry” is 11 bytes (10 chars + \0). Remaining 4 bytes (str[11] to str[14]) are zero-initialized.
Initializing Character Arrays in Structures
When dealing with structures, you can initialize character arrays as follows:
#include <stdio.h> #include <string.h> struct Student { char fullName[50]; }; int main() { struct Student s1 = { "Rahul" }; struct Student s2; strcpy(s2.fullName, "Priya"); printf("Student 1: %s\n", s1.fullName); printf("Student 2: %s\n", s2.fullName); return 0; }
Output:
Student 1: Rahul Student 2: Priya
This program defines a Student structure with a fullName array. The first student is initialized with the name “Rahul,” and the second student is assigned the name “Priya” using strcpy(). It then prints both names.
Practical Examples
Example 1: Simple Character Array Initialization
#include <stdio.h> int main() { char portal[] = "Sanfoundry"; printf("Portal: %s\n", portal); return 0; }
Output:
Portal: Sanfoundry
This program defines a character array portal and initializes it with the string “Sanfoundry.” It then prints the string using the printf() function.
Example 2: Manual Character Initialization
#include <stdio.h> int main() { char keyword[9] = { 'T', 'u', 't', 'o', 'r', 'i', 'a', 'l', '\0' }; printf("Keyword: %s\n", keyword); return 0; }
Output (may vary):
Keyword: Tutorial
This program defines a character array keyword and fills it with the word “Tutorial” followed by a null terminator (\0). The program then prints the word “Tutorial” using the printf() function.
Example 3: Character Array Without Null Terminator
#include <stdio.h> int main() { char course[6] = { 'C', 'o', 'd', 'e', 'B', 'y' }; // No null terminator printf("Course: %s\n", course); return 0; }
Output (may vary):
Course: CodeBy
This program defines a character array course and fills it with the letters “CodeBy”. However, since there is no null terminator (\0), it may lead to undefined behavior when printing the string. The program attempts to print the course array, but it may result in garbage values after the intended characters.
Common Pitfalls
- Omitting the Null Terminator: Forgetting ‘\0’ can lead to undefined behavior when treating the array as a string.​
- Insufficient Array Size: Ensure the array is large enough to hold the string and the null terminator.​​
- Modifying String Literals: String literals are typically stored in read-only memory; modifying them can cause errors.​
Sanfoundry Global Education & Learning Series – 1000 C Tutorials.
- Watch Advanced C Programming Videos
- Practice Computer Science MCQs
- Apply for C Internship
- Practice BCA MCQs
- Check Computer Science Books