C Standard Library

C <stdio.h> - gets() Function



The C <stdio.h> gets() function reads characters from the standard input stdin and stores them as a C string into str until a newline character is found or end-of-file is reached.

The newline character, if found, is not copied into str. A terminating null character is automatically appended after the characters copied to str.

Syntax

char * gets ( char * str );

Parameters

str Specify character string to be written.

Return Value

  • On success, str is returned, On failure, null pointer is returned.
  • If the failure has been caused due to end-of-file condition, additionally sets the end-of-file indicator feof() on stdin.
  • If error occurs due to some other reason, additionally sets the error indicator ferror() on stdin.

Example:

The example below shows the usage of gets() function.

#include <stdio.h>
 
int main (){

  char name[256];

  //taking input from keyboard
  printf("Enter full name: ");
  gets(name);

  //printing the result
  printf("Your name is: %s\n", name);

  return 0;
}

If the following input is entered:

John Smith

The output will be:

Enter full name: John Smith 
Your name is: John Smith 

❮ C <stdio.h> Library

5