A String is nothing but a collection of Characters.
Syntax :-
data type variable name[size]
char String[size];
char String[size];
Character data type
|
String data type
|
char
a=’x’;
|
char
a[size];
char
a[20];
|
Ex:-
char name[10];
0
|
1
|
2
|
3
|
4
|
5
|
6
|
7
|
8
|
9
|
P
|
R
|
A
|
S
|
A
|
D
|
\0
|
\0
|
\0
|
\0
|
0
|
1
|
2
|
3
|
4
|
5
|
6
|
7
|
8
|
9
|
In the above example we can store only 9 characters remaining one character is Null characters'\0'.
Data type | Format specifier |
Character data type | %C |
String data type | %S |
* Write a Program to Print your Name with out String.
#include<stdio.h>
#include<conio.h>
void
main()
{
char
a=’p’,b=’r’,c=’a’,d=’s’,e=’a’,f=’d’;
clrscr();
printf(“%c%c%c%c%c%c”,a,b,c,d,e,f);
getch();
}
|
Output:-
prasad |
* Write a Program to Print your Name using one variable.
#include<stdio.h>
#include<conio.h>
void main()
{
char name;
clrscr();
printf("enter your name:");
scanf("%c",&name);
printf("your name is :%c",name);
getch();
}
|
Output:-
Enter your name: prasad
Your name is: p
|
To the above program we gave a name,in that only one Character will be print.
Because Variable can store only one Character.
If we want to store number of Characters in one Variable we have to create String.
* Write a Program to Print your Name using String.
#include<stdio.h>
#include<conio.h>
void main()
{
char name[20];
clrscr();
printf("enter your name");
scanf("%s",&name);
printf("your name is :%s",name);
getch();
}
|
Output:-
enter your
name: prasad
your name
is: prasad
|
Post a Comment