Read: String in C - Part 1
In the last tutorial I gave you an overview of the strings in C language. I told you about the basic programs to print the elements inside a string. In today’s tutorial I will tell you about the best way to print the string on the screen. I will also tell you about the limitation of scanf() function with strings. So lets get started.
Output
TheCrazyProgrammer
Above program is self explanatory. I have only used %s format specifier to print the name string.
Output
Explanation
As you can see I have used scanf() function two times. First time I have asked the user to enter only one word. So scanf() function is able to perform that task easily. But in the second time I have asked the user to enter at least two words. But in that case printf() has only printed “I”. Well the reason behind it is that, scanf() function can only take single word. It closes the string when it founds any blank space. To overcome this problem we have to use another function.
Output
In the above program I have replaced scanf() function with gets function. As you can see now everything is working fine.
Output
Hello World!!!
In the last tutorial I gave you an overview of the strings in C language. I told you about the basic programs to print the elements inside a string. In today’s tutorial I will tell you about the best way to print the string on the screen. I will also tell you about the limitation of scanf() function with strings. So lets get started.
String in C
Text manipulation is one of the most frequently used function in any language. In the last program I have printed the string elements by accessing them one by one. However in serious programing it is very difficult to run loops when you have to print some string of characters on screen. To overcome this problem Dennis Ritchie introduced %s format specifier. It turns out to be very handy while printing the string on the go. Lets make one program to understand it.#include<stdio.h>
void main()
{
char name[]="TheCrazyProgrammer";
printf("%s",name);
}
Output
TheCrazyProgrammer
Above program is self explanatory. I have only used %s format specifier to print the name string.
Reading String from User
So far I told you about printing the output on the screen. However taking input from the user to a string is equally important too. To perform this task, first answer that will hit your mind is scanf() function. Yes scanf() can be used to take the input. But it is not perfect for this task. Lets understand it with one program#include<stdio.h>
void main( )
{
char name[100];
printf("Enter one word
");
scanf("%s",name);
printf("%s
",name);
printf("
Enter at least two words
");
scanf("%s",name);
printf("%s",name);
}
Output
As you can see I have used scanf() function two times. First time I have asked the user to enter only one word. So scanf() function is able to perform that task easily. But in the second time I have asked the user to enter at least two words. But in that case printf() has only printed “I”. Well the reason behind it is that, scanf() function can only take single word. It closes the string when it founds any blank space. To overcome this problem we have to use another function.
gets() function in C
This function is used to take the input from the user to the string. It overcomes the problem of storing only single word. With the usage of this function user can store any number of words or even sentences. Lets understand it with one program.#include<stdio.h>
void main( )
{
char name[100];
printf("Enter at least two words
");
gets(name);
printf("Now its working fine: %s",name);
}
Output
puts() function in C
As its name suggest puts() function is a counter part of gets(). It is used to print one string at a time on the screen. Remember we can only use one string at a time to print it on the screen. On the other hand printf() function can be used to print any number of strings. Lets make one simple program to understand its working.#include<stdio.h>
void main()
{
char name[100]="Hello World!!!";
puts(name);
}
Output
Hello World!!!