17 Oct Strings in C
An array of characters in C is called a String. The null character also has a role in the C language. A string is terminated by a null character i.e., \0. In this lesson, we will learn how to work with Strings in C programming.
Before moving further, we’ve prepared a video tutorial to understand what strings are in C:
Video: Strings in C
Video: Strings in C (Hindi)
Let’s first see what we meant by strings. The following is a string,
Lionel
We will now learn how to declare and initialize the above string in C programming,
char celeb[] = "Lionel";
We haven’t added the null character above, since the compiler adds it on its own. In addition, we haven’t added size, since it is optional. Let’s see the entire example,
#include <stdio.h>
void main () {
char celeb[] = "Lionel";
printf("Name of celebrity: %s\n", celeb );
getch();
}
The following is the output,

You can also declare and initialize the above string like the following, with extra space for the null character and size in square brackets,
char celeb[7] = {'L', 'i', 'o', 'n', 'e', 'l','\0'};
Above, we added an extra size for the array to hold a null character.
Now let’s see another example of a string,
Studyopedia
The declaration and initialization of the above string,
char website[] = {'S', 't', 'u', 'd', 'y', 'o', 'p', 'e', 'd', 'i', 'a','\0'};
You may have noticed the following in the above declaration,
- We have not added the size, since it is optional.
- A null character is added at the end.
Let’s see the entire example,
#include <stdio.h>
void main () {
char website[] = {'S', 't', 'u', 'd', 'y', 'o', 'p', 'e', 'd', 'i', 'a','\0'};
printf("Learning website for free tutorials: %s\n", website );
getch();
}
The following is the output,

The array looks like the following with characters,

In this lesson, we learned how to declare and initialize Strings in C programming with some examples.
If you liked the tutorial, spread the word and share the link and our website Studyopedia with others:
For Videos, Join Our YouTube Channel: Join Now
Read More:
No Comments