String Literals in C
String Literals in C
A string literal in C is a sequence of characters enclosed within double quotes (" "). It represents a
constant sequence of characters in the source code and is used to initialize string variables or
In C programming, string literals are not just sequences of characters, but they also come with a
special memory representation due to the null-terminator (\0) that indicates the end of the string.
String literals are widely used in C programs for handling text, printing outputs, and initializing
variables.
A string literal is written as a sequence of characters enclosed by double quotes. For example:
String literals are automatically terminated with the null character '\0'. This null character is essential
For example, the string literal "Hello" is represented internally as: {'H', 'e', 'l', 'l', 'o', '\0'}
1. **Immutability**: String literals are immutable, meaning they cannot be modified directly once
declared.
2. **Null Terminator (`\0`)**: Every string literal in C is automatically null-terminated, which marks the
4. **Size**: The size of a string literal is the number of characters plus one for the null terminator
#include <stdio.h>
int main() {
printf("%s\n", greeting);
return 0;
In this example, the string literal "Hello, World!" is stored in the `greeting[]` array, and the null
case is 13.
In C, trying to modify a string literal directly results in undefined behavior. String literals are typically
Example:
Conclusion
String literals in C are a fundamental feature for representing text. They are automatically
null-terminated and stored in memory in a read-only section to prevent modification during runtime.
Since they are immutable, string literals should not be modified directly, and this behavior ensures