Declare and Initialize a Character Array (String) in C

In C, a character array is used to store strings, which are sequences of characters terminated by a null character '\0'. You can declare and initialize a string using various methods, such as specifying characters individually, using string literals, or dynamically allocating memory.

In this tutorial, we will explore different ways to declare and initialize character arrays in C with practical examples.


Examples of Declaring and Initializing a Character Array

1. Declaring and Initializing a Character Array Using a String Literal

In this example, we declare a character array and initialize it using a string literal. A string literal automatically includes the null character '\0' at the end.

main.c

</>
Copy
#include <stdio.h>

int main() {
    // Declaring and initializing a string using a string literal
    char greeting[] = "Hello, World!";

    // Printing the string
    printf("%s\n", greeting);

    return 0;
}

Explanation:

  1. We declare a character array greeting and initialize it using a string literal "Hello, World!".
  2. The compiler automatically adds the null terminator '\0' at the end of the string.
  3. We use printf() with the format specifier %s to print the string.

Output:

Hello, World!

2. Declaring and Initializing a Character Array Using Individual Characters

In this example, we declare a character array and manually assign each character, including the null terminator.

main.c

</>
Copy
#include <stdio.h>

int main() {
    // Declaring a character array and initializing with individual characters
    char name[] = {'C', 'o', 'd', 'e', 'r', '\0'};

    // Printing the string
    printf("%s\n", name);

    return 0;
}

Explanation:

  1. We declare a character array name with individual character assignments.
  2. We explicitly add the null character '\0' at the end to mark the end of the string.
  3. We print the string using printf() with the format specifier %s.

Output:

Coder

3. Declaring a Fixed-Size Character Array and Assigning a String

In this example, we declare a fixed-size character array and assign a string using strcpy() from the string.h library.

main.c

</>
Copy
#include <stdio.h>
#include <string.h>

int main() {
    // Declaring a fixed-size character array
    char city[20];

    // Copying a string into the character array
    strcpy(city, "New York");

    // Printing the string
    printf("%s\n", city);

    return 0;
}

Explanation:

  1. We declare a fixed-size character array city with space for up to 20 characters.
  2. We use the strcpy() function from string.h to copy the string “New York” into the array.
  3. The null character '\0' is automatically added by strcpy().
  4. We print the string using printf().

Output:

New York

Conclusion

In this tutorial, we explored different ways to declare and initialize character arrays (strings) in C:

  1. Using a string literal: The easiest way to initialize a string.
  2. Assigning individual characters: Requires explicitly adding the null terminator.
  3. Using a fixed-size array and strcpy(): Useful when the string may change dynamically.