String in C language is an array of characters that is terminated by \0 (null character).
There are two ways to declare string in c language.
- By char array
- By string literal
Let's see the example of declaring string by char array in C language.
While declaring string, size is not mandatory. So you can write the above code as given below:
You can also define string by string literal in C language. For example:
In such case, '\0' will be appended at the end of string by the compiler.
Difference between char array and string literal
The only difference is that string literal cannot be changed whereas string declared by char array can be changed.
String Example in C
Let's see a simple example to declare and print string. The '%s' is used to print string in c language.