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

String is defined as collection of multiple characters within inverted commas(“).

Example:  “Lovejot” , “2019” 

1. String variable

Character type array is known as string variable.

Example: char sname[20];



2. Initialization of a string variable

We can intialize string variable as

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

Example: 

char name[6]={‘r’,’o’,’h’,’i’,’t’};

OR

char var[Size]=”Value”;

Example         

char name[6]=”rohit”;

Program 
#include<stdio.h>
int main()
{
char name[]=”Rohit”;
printf(“%s”,studentname);
return(0);
}
Output
Rohit



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.

Program 
#include<stdio.h>
int main()
{
char name[20];
printf(“Enter your name =”);
gets(name);
printf(“\nname=%s”,name);
return(0);
}

Best Books of Computer Science




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