C strings

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.

Basics of strings

Before illustrating the details and syntax of strings in C language, let’s get acquainted with basic concepts such as characters and strings.

What is a character?

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.

What is a string?

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.

String definition in C language

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 )

String rules in C language

  1. The C language compiler uses a special character called null to determine the end of a string, which is characterized by (‘\0’) (a byte with numerical value zero). That is, the last member of the string ends with a null character or zero.
  2. To store a word with a length of n characters, an array of type char with n+1 cells is needed. Therefore, in a char array with a length of 50, for example, strings with a maximum length of 49 can be stored.
  3. In such an array with a length of 50, if strings shorter than this length are stored, The null character or ‘0’ is placed immediately after the last character of the string.

Unauthorized length

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.

C Strings Initialization

When initializing a string you can leave the length of string empty. Look at the below:

unsigned char str[] = "Hello";
  1. Between [ ] is empty. In this case, an unbounded array is generated, which determines the number of its members depending on the input elements. Here, the ASCII code of each character is stored in the array’s cells and zero is placed at the end.
  2. If you have noticed, there are two ways to assign values ​​to strings. As in the first example, the ASCII code of each character can be given to the string, or a string can be stored completely in an array.
  3. Strings are initialized this way only at definition time. But this is not allowed in the middle of the program.
  4. In the text of a string, in addition to printable characters, control characters such as \n and \t can be added, and if the character ” itself is needed, it should be written as \.”

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.

Array of strings

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"};
Was this helpful?
[0]
Scroll to Top
Scroll to Top