Declare and Initialize a String in C

In C, a string is declared as an array of characters terminated by a null character (\0). We can initialize a string using character arrays or string literals. Strings in C do not have a built-in data type like in other languages, so they are represented using character arrays or pointers to characters.


Examples of Declaring and Initializing Strings

1. Declaring and Initializing a String Using Character Array

In this example, we declare and initialize a string using a character array with a predefined size.

main.c

</>
Copy
#include <stdio.h>

int main() {
    // Declare and initialize string using character array
    char str[6] = {'H', 'e', 'l', 'l', 'o', '\0'};

    // Print the string
    printf("%s", str);

    return 0;
}

Explanation:

  1. We declare a character array char str[6] with 6 elements.
  2. The string is initialized manually with each character, including the null character (\0).
  3. The printf() function is used to print the string. %s is used as a format specifier for strings.

Output:

Hello

2. Declaring and Initializing a String Using String Literal

In this example, we declare and initialize a string using a string literal without specifying the size.

main.c

</>
Copy
#include <stdio.h>

int main() {
    // Declare and initialize string using string literal
    char str[] = "Hello";

    // Print the string
    printf("%s", str);

    return 0;
}

Explanation:

  1. We declare a character array char str[] and initialize it using a string literal.
  2. The compiler automatically determines the size and appends the null character (\0) at the end.
  3. The printf() function prints the string using the format specifier %s.

Output:

Hello

3. Declaring and Initializing a String Using a Pointer

In this example, we declare and initialize a string using a pointer to a character.

main.c

</>
Copy
#include <stdio.h>

int main() {
    // Declare and initialize string using pointer
    char *str = "Hello";

    // Print the string
    printf("%s", str);

    return 0;
}

Explanation:

  1. We declare a pointer char *str and assign it a string literal.
  2. The string is stored in read-only memory, and modifying it may lead to undefined behavior.
  3. The printf() function is used to print the string.

Output:

Hello

4. Taking String Input Using scanf()

In this example, we take a string input from the user using scanf().

main.c

</>
Copy
#include <stdio.h>

int main() {
    // Declare string with a fixed size
    char str[20];

    // Take user input
    printf("Enter a string: ");
    scanf("%s", str);

    // Print the string
    printf("You entered: %s", str);

    return 0;
}

Explanation:

  1. We declare a character array char str[20] to store the user input.
  2. The scanf() function reads a word from the user input but stops at whitespace.
  3. The printf() function prints the entered string.

Output:

Enter a string: Hello
You entered: Hello

Conclusion

In this tutorial, we covered different ways to declare and initialize strings in C:

  1. Using a character array with explicit initialization.
  2. Using a string literal in a character array.
  3. Using a pointer to a string literal.
  4. Taking string input from the user with scanf().