gets : <stdio.h> library function

The function char *gets(char *str); reads characters from the standard input stream (stdin) and stores it as a null terminated string into str until a newline character or the end-of-file is reached, whichever comes first. A null character('\0') is automatically added after the characters copied to str.

Function prototype of gets

char *gets(char *str);
  • str : This is the pointer to an character array where the characters copied from stdio stream is stored.

Return value of gets

On Success, this function returns the str pointer after copying the character read from the stdio stream otherwise returns NULL in case of error or If end of file is reached before reading any characters.

C program to show the use of gets function

The following program shows the use of gets function to read a string from user and print it back on screen.

#include <stdio.h>

int main(){
   char string[50];

   printf("Enter your name\n");

   printf("You name is %s", string);

Program Output
Enter your name
Jack Dawson
You name is Jack Dawson