gets( ) and puts( ) in C

gets( ) receives a string from the keyboard. Why is it needed? Because scanf( ) function has some limitations while receiving string of characters, as the following example illustrates

main( ) 
{ 
 char name[50] ; 
 printf ( "\nEnter name " ) ; 
 scanf ( "%s", name ) ; 
 printf ( "%s", name ) ; 

}

And here is the output…
Enter name Jonty Rhodes
Jonty

The puts( ) function works exactly opposite to gets( ) function. It outputs a string to the screen.

Here is a program which illustrates the usage of these functions:

main( ) 
{ 
 char footballer[40] ; 
 puts ( "Enter name" ) ; 
 gets ( footballer ) ; /* sends base address of array */ 
 puts ( "Happy footballing!" ) ; 
 puts ( footballer ) ; 
}

Following is the sample output:
Enter name

Jonty Rhodes
Happy footballing!
Jonty Rhodes

Leave a Comment