Examples of Strings in C

For Complete YouTube Video: Click Here

In this class, we will understand Examples of Strings in C.

We have already discussed strings.

Examples of Strings in C

We will try to understand the concepts of strings with some examples.

Example 1

The image below is the program for ‘example 1’ on strings.

Examples of Strings in C Example 1
Examples of Strings in C Example 1

In the above image, we have defined a string variable, and within that one, we have given \0 as one of the characters.

Now, we will think that \0 given in the string is part of the string and will be counted in the length of the ‘string.’

Based on that assumption, we will consider the length of the string as 16.

But the \0 is always considered as the end of the string even though it is there in the definition of the string.

Example 2

The image below is the program for ‘example 2’ on strings.

Examples of Strings in C Example 2
Examples of Strings in C Example 2

In the above program, we have declared an array of strings.

We will generally go wrong by thinking that the lines marked in red are swapping the names[3] and names[4].

But the program will generate an error.

The error is that we cannot assign an array to another array.