Advertisement
/* Using scanf() function to read a string in C */
#include<stdio.h>
int main()
{
char season[20];
printf("Enter your favorite season : ");
scanf("%s", season);
printf("Your favorite season is : %s", season);
return 0;
}
Enter your favorite season : Autumn
Your favorite season is : Autumn
/* scanf() function cannot read multi-word strings in C */
#include<stdio.h>
int main()
{
char country[20];
printf("Enter a country you want to visit : ");
scanf("%s", country); /*scanf() function can't read multi-words*/
printf("The country you want to visit is : %s", country);
return 0;
}
Enter a country you want to visit : Czech Republic
The country you want to visit is : Czech
Advertisement
/* Using gets() function to read a string in C */
#include<stdio.h>
int main()
{
char country[20];
printf("Enter a country you want to visit : ");
gets(country);
printf("The country you want to visit is : %s", country);
return 0;
}
Enter a country you want to visit : Czech Republic
The country you want to visit is : Czech Republic
/* Using puts() function to display a string*/
#include<stdio.h>
int main()
{
char country[20];
printf("Enter a country you want to visit : ");
gets(country);
printf("The country you want to visit is : ");
puts(country);
return 0;
}
Enter a country you want to visit : New Zealand
The country you want to visit is : New Zealand
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement