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
#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:
- We declare a character array
char str[6]
with 6 elements. - The string is initialized manually with each character, including the null character (
\0
). - 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
#include <stdio.h>
int main() {
// Declare and initialize string using string literal
char str[] = "Hello";
// Print the string
printf("%s", str);
return 0;
}
Explanation:
- We declare a character array
char str[]
and initialize it using a string literal. - The compiler automatically determines the size and appends the null character (
\0
) at the end. - 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
#include <stdio.h>
int main() {
// Declare and initialize string using pointer
char *str = "Hello";
// Print the string
printf("%s", str);
return 0;
}
Explanation:
- We declare a pointer
char *str
and assign it a string literal. - The string is stored in read-only memory, and modifying it may lead to undefined behavior.
- 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
#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:
- We declare a character array
char str[20]
to store the user input. - The
scanf()
function reads a word from the user input but stops at whitespace. - 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:
- Using a character array with explicit initialization.
- Using a string literal in a character array.
- Using a pointer to a string literal.
- Taking string input from the user with
scanf()
.