gets and puts in c

scanf( ) is not capable of receiving multi-word strings. Therefore names such as ‘Debashish Roy’ would be unacceptable. The way to get around this limitation is by using the function gets( ). The usage of functions gets( ) and its counterpart puts( )

main( )
{
char name[25] ;
printf ( “Enter your full name ” ) ;
gets ( name ) ;
puts ( “Hello!” ) ;
puts ( name ) ;
}

Enter your name Debashish Roy
Hello!
Debashish Roy

The program and the output are self-explanatory except for the fact that, puts( ) can display only one string at a time (hence the use of two puts( ) in the program above). Also, on displaying a string, unlike printf( ), puts( ) places the cursor on the next line. Though gets( ) is capable of receiving only one string at a time, the plus point with gets( ) is that it can receive a multi-word string.

If we are prepared to take the trouble we can make scanf( ) accept multi-word strings by writing it in this manner:

char name[25] ;
printf ( “Enter your full name ” ) ;
scanf ( “%[^\n]s”, name ) ;

Leave a Comment