Preview
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