Character Array in C



In C, characters and strings are stored differently. A character array stores a sequence of characters, like text. We use single quotes (' ') to define a single character (e.g., 'A') and double quotes (" ") to define a string or a character array (e.g., "A", "Hello").

The main difference is that a single character is stored as its ASCII value, while a string is stored as a sequence of characters ending with a null terminator (\0). In this chapter, we will see single and double quoted character arrays.

Understanding Chracter Arrays in C

A character array is a collection of characters stored in consecutive memory locations, which means each character occupies the next memory slot one after the another. In C, we use character arrays to represent strings, which are enclosed in double quotes and a null terminator (\0) is added at the end of each string to mark its end.

Example of a Character Array

In this example, we declare a character array str[] with 6 elements and print it using %s. Each character is stored one after the other in memory, so the program prints them one by one until it reaches the null terminator \0.

#include <stdio.h>

int main() {
    char str[] = "Hello";  // Creating a character array of 6 chracters
    printf("%s", str);     // Printing the string
    return 0;
}

Below you can see the output of the above program, which displays the string stored in the character array.

Hello

Single Quotes in C

Single quotes (' ') in C are used to represent single character. We can also use them to create a character array by listing characters one by one inside braces {}. Each character is stored internally as its ASCII value.

For example, 'A' represents the character A, but in memory it is stored as the number 65, which is the ASCII value of A.

Note that we cannot use single quotes to store multiple characters together like 'Hi' because that will cause an error.

Example of Single Quoted Character Array

Below is an example showing a character array using single quotes. Here, we declare a character array arr to store the characters 'H', 'i', and '!'. We then print each character one by one in a loop.

#include <stdio.h>

int main() {
    char arr[] = {'H', 'i', '!'};  // Creating a single-quoted character array
    int i;
    
    for(i = 0; i < 3; i++) {
        printf("%c ", arr[i]);  // Prints each character
    }
    
    return 0;
}

Below is the output of the above program, which displays each character of the array one by one.

H i !

Double Quotes in C

Double quotes (" ") in C are used to represent string literals, which are stored as character arrays. A string in C always ends with a null character (\0) so that the program knows where the string ends.

For example, "A" may look like a single character, but in memory, it is stored as two characters: 'A' followed by '\0'.

Note: Unlike single quotes, double quotes can store multiple characters as a string.

Example of Double Quoted Character Array

Below is an example showing the use of double quotes. Here, we define a string str with the value "Hi". So, "Hi" is stored as three characters: 'H', 'i', and the null terminator '\0'. That is why the size of the array becomes 3.

#include <stdio.h>

int main() {
    // storing the string "Hi" in a character array (includes '\0' automatically)
    char str[] = "Hi"; 
    printf("String: %s\n", str);   
    printf("Size of str: %lu\n", sizeof(str));  
    return 0;
}

Following is the output of the above program, which displays the given string and also shows the size of the array (including the null terminator) −

String: Hi
Size of str: 3

Common Mistakes

When working with characters and strings in C, it's easy to get confused when to use single quotes (' ') and double quotes (" "). Here are two common mistakes to avoid −

  • Using double quotes for a single character −
char c = "A";   // Wrong: "A" is a string (character array), not a single character
char c = 'A';   // Correct: 'A' is a single character

Always use single quotes when you want to store just one character.

  • Using single quotes for strings
char str[] = 'Hello';   // Wrong: single quotes can only hold one character
char str[] = "Hello";   // Correct: double quotes are used for strings

Always use double quotes when you want to store multiple characters (a string).

Difference between Single and Double Quotes in C

The following table shows the key differences between single quotes and double quotes in C −

Feature Single Quotes (' ') Double Quotes (" ")
Represents A single character A sequence of characters (string)
Example 'A' "A"
Data Type char (or integer constant) char[] (character array)
Memory Size 1 byte (for a single character) Number of characters + 1 (for \0)
Null Terminator Not added Automatically added at the end
Usage Individual letters, digits, or symbols Words, sentences, or multiple characters
Stored As ASCII value of the character Array of ASCII values with \0 at the end

Conclusion

In this chapter, we learned that single quotes are used to store a single character as its ASCII value, while double quotes store multiple characters as a string ending with a null terminator. We also looked at their differences in character arrays.

Advertisements