Format
#include <stdio.h> char *gets(char *buffer);
Language Level: ANSI, POSIX, XPG4
gets reads a line from the standard input stream stdin and stores
it in buffer. The line consists of all characters up
to and including the first new-line character (\n) or EOF. gets
then replaces the new-line character, if read, with a null
character (\0) before returning the line.
Return Value
If successful, gets returns its argument. A NULL pointer
return value indicates an error or an end-of-file condition with
no characters read. Use ferror or feof to determine which of
these conditions occurred. If there is an error, the value stored
in buffer is undefined. If an end-of-file condition
occurs, buffer is not changed.
Example
This example gets a line of input from stdin.
#include <stdio.h>
#define MAX_LINE 100
int main(void)
{
char line[MAX_LINE];
char *result;
printf("Please enter a string;\n");
if ((result = gets(line)) != NULL)
{
if (ferror(stdin))
perror("Error");
printf("Input line : %s\n", result);
}
/***************************************************
For the following input:
This is a test for function gets.
The output should be:
Input line : This is a test for function gets.
***************************************************/
}
![]()
![]()
_cgets -- Read String of Characters from
Keyboard
fgets -- Read a String
feof -- Test End-of-File Indicator
ferror -- Test for Read/Write Errors
fputs -- Write String
getc - getchar -- Read a Character
puts -- Write a String
<stdio.h>