In this lesson, we will talk about C strings. Using strings, we can store alphabets, sentences, and paragraphs. For example, suppose you want to receive the information of different people and store it in the database with a form on the software page. We use strings to store such information. Now that you are familiar with the usage of strings, We can go to strings in C language to check their principles and rules.
Before illustrating the details and syntax of strings in C language, let’s get acquainted with basic concepts such as characters and strings.
We all send a lot of messages on our smartphones each day. When we press a button on the phone screen, the exclusive code of the button that corresponds to the character is sent to the display, and the display shows it. (For example, pressing the ‘a’ button will send the code 97 to the display.)
You may ask, where did 97 come from? The answer should be found in the table of ASCII codes. In fact, all uppercase and lowercase the alphabet letters, numbers, symbols, etc., are one character and need a standard code called ASCII code to be viewed on screens. In the table of ASCII codes, a specific code is considered for each letter or symbol.
Characters are put together to form words, sentences, paragraphs, and texts. In fact, a sequence of characters is created, which is called a string. For example, consider the string “Hello” which consists of five characters ‘H’, ‘e’, ‘l’, ‘l’, and ‘o’.
In C language, strings can be stored, retrieved, and manipulated. For this purpose, arrays should be used. You might be wondering why an array is used to store strings. This is because, in C language, a new data type is not defined for storing strings. The string is the same array which its cells are filled with ASCII codes, and the last cell has a value of zero.
As mentioned, a string is an array of characters. To define a string, you should defining an array, with the difference that the type of string can only be char. In C language, strings must be placed between a pair of double quotes, ie,”-“, but characters are placed between a pair of single quotes, i.e.’-‘
When a letter or character is placed between single quotes, for the compiler, it means the ASCII code of that letter or character. Also, when a word or a sentence is placed between double quotes, for each of its characters, their equivalent ASCII code is extracted. Pay attention to an example of string definition and initialization:
unsigned char str[6] = {'H', 'e', 'l', 'l', 'o', '\0'};
An array of type character named str is used to store the word “Hello” with the length of 6.
Why do you think a string with length 5 is not used? (You can answer this question by reading the rest of the lesson )
In case of storing a string with a length exceeding the limit defined for the array, because this issue is not controlled by the C compiler, the storage operation is performed, the additional characters are also recorded with the end of the string in bytes of memory that are outside the range of the array. But no guarantee to access characters stored outside the length of the array.
When initializing a string you can leave the length of string empty. Look at the below:
unsigned char str[] = "Hello";
Pay attention to the following example:
char str[] = {"hello\n"};
At this line, the only new thing is the control character \n. The existence of this character means that if we want to print this string, after printing, the cursor will be moved to the next line, and the display operation will continue from the new line.
If we want to define a string that holds the names of n students, what should we do? In such cases, we must define an array of strings. In the example below, we have defined an array to store the names of 2 students, each of which is 15 characters long:
char name [2][15] = {"student1", "student2"};