Strings in c language with examples

Preview
You must first complete Display output in C – printf() function before viewing this Lesson

Strings in c language with examples

A string is a group of one or more characters enclosed within double quotes (“).

A string can alphabets, digits , special characters and blank spaces.

A string is always terminated with a special character called null character represented as ‘\0’.

Example:  “Lovejot” , “2019” , “Rs4000”,  “ladderpython@gmail.com” 

1. What is a  string variable?

An array of char type is known as a string variable.

Syntax for declaring a string variable is

char var_name[Size];

char is the data type specifying that a string variable is being declared.

var_name refers the name of string variable specified by the programmer. The name of variable must follow all the rules of valid identifier of C language.

[ ] Square brackets actually specify that it is a string variable.

Size specifies the maximum number of characters which can be stored in the string variable.

Example: char name[20];

In this example, name is a string variable which can contain maximum 19 characters as one character should always be a null character.

2. Initialization of a string variable

There are two ways to initialize a string varible:

a. The first way to initialize a string variable is

char var_name[Size]={List of characters};

char is the data type specifying that a string variable is being declared.

var_name refers the name of string variable specified by the programmer. The name of variable must follow all the rules of valid identifier of C language.

[ ] Square brackets actually specify that it is a string variable

Size specifies the maximum number of characters which can be stored in the string variable. During initialization, Size is optional. If we don’t specify any size, compiler will automatically calculate the size depending upon the number of characters stored in the string variable.

List of characters specifies various characters which are to be stored into string variable. The list of characters must be enclosed between braces.

Example

char studentname[5]={‘A’,’m’,’i’,’t’};

or

char studentname[]={‘A’,’m’,’i’,’t’};

In examples above, string variable studentname has been initialized with the value “Amit”.

Program to initialize a string variable.
#include<stdio.h>
int main()
{
char studentname[]={‘A’,’m’,’i’,’t’};
printf(“\nStudentname=%s”,studentname);
return(0);
}
Output
Studentname=Amit

b. The second way to initialize a string variable is

char var_name[Size]=”Value”;

char specifies that we are declaring a string variable.

var_name refers to the string variable defined by the programmer. The string variable name should follow all the rules of a valid identifier of C Language.

[ ] Square brackets actually specify that it is a string variable.

Size specifies the maximum number of characters which can be stored in string variable. Size is optional.

If we don’t specify any size, compiler will automatically calculate the size depending upon the number of characters stored in the string variable.

Value specifies any string value which we want to store in string variable. The value to be stored in string variable must be enclosed within double quotes.

Example         

char studentname[5]=”Amit”;

or

char studentname[]=”Amit”;

In examples above, string variable studentname has been initialized with the value “Amit”.

Program to initialize a string variable.
#include<stdio.h>
int main()
{
char studentname[]=” Amit”;
printf(“\nStudentname=%s”,studentname);
return(0);
}
Output
Studentname=Amit

3. Reading and displaying a string variable

We can read and display a string variable just as we read a normal variable of C Language.

Functions scanf() and gets() can be used to read a string variable.

Formatted output function printf() and unformatted function puts() can be used to display a string variable.

Program to read and display a string variable.
#include<stdio.h>
int main()
{
char studentname[20];
printf(“Enter name of student=”);
gets(studentname);
printf(“\nStudentname=%s”,studentname);
return(0);
}
Output
Enter name of student=Amit
Studentname=Amit

Lesson tags: display string in c, initialicze a string variable in, read string value in c, string in c
Back to: C Programming Language