gets() and puts() Function in C Examples


The gets() allows the user to enter the space-separated strings. It returns the string entered by the user.


  • gets() function is used to scan a line of text from a standard input device.
  • This function will be terminated by a new line character.
  • The new line character won’t be included as part of the string. The string may include white space characters.
  • Syntax :char *gets(char *s);
  • This function is declared in the header file stdio.h.
  • It takes a single argument. The argument must be a data item representing a string. On successful completion, shall return a pointer to string s.


  • The C library function int puts(const char *str) writes a string to stdout up to but not including the null character.A newline character is appended to the output.
  • Example: int puts(char const*str)
  • Parameters: str-this is the C string to be written
  • Return value: If successful, non-negative value is returned. On error, the function returns EOF.

Example 1:

 void main ()  
    char s[30];  
    printf("Enter the string? ");  
    printf("You entered %s",s);  


 Enter the string? 
 aimtocode is the best
 You entered aimtocode is the best

Example 2:

 #include <stdio.h>
 #include <conio.h>

 void main( ) {

 char name[20];

 printf( "Enter your name : ");

 printf( "Your name is : ");

 getch ();


 Enter your name :
 Your name is : Prayag