C Strings
Strings
Strings are actually one-dimensional array, used for storing text/characters of characters terminated by a null character '\0'. Thus a null-terminated string contains the characters that comprise the string followed by a null.
Declaration of Strings in C
String declaration is as simple as the declaration of a one-dimensional array. There are two ways to declare a string in C:-
- By char array
- By string literal
string declaration
Initialization of a string in C
Strings can be initialized in different ways. Below are some examples of string initialization.
Access Strings
Since strings are actually arrays in C, you can access a string by referring to its index number inside square brackets [].
Modify Strings
To change the value of a specific character in a string, refer to the index number, and use single quotes:
Read a string in C:
Use functions like scanf(), fgets(), gets() to read a string from the user.
Another Way Of Creating Strings
In the examples above, we used a "string literal" to create a string variable. This is the easiest way to create a string in C.