String in C language is the array of characters terminated by a null character ‘\0’. String is similar to the array and its data type is specific to char.
Declaration of Strings
Declaring a string is similar to declaring a one-dimensional array but the data type must be char.
Syntax for declaration of strings
char string_name[ length ];
Here string_name is the name of the string and length is the size of the string including null character.
Example
char myString[10];
Initialization of strings
Initialization of a string is also similar to initializing an array.
Syntax for initializing a string
There are three different ways through which we can initialize a string.
Method 1:
char name[11] = {'O', 'j', 'h', 'a', 'B', 'i', 'k', 'a', 's', 'h', '\0'};
Method 2:
char name[] = {'O', 'j', 'h', 'a', 'B', 'i', 'k', 'a', 's', 'h', '\0'};
Method 3:
char name[11] = "OjhaBikash";
Method 4:
char name[] = "OjhaBikash";
Program to print a string
//A program to print a string
#include<stdio.h>
int main()
{
char name[] = "OjhaBikash";
printf("%s\n",name);
return 0;
}
Output
OjhaBikash