gets_s(3)

gets_s3

Get a line from a stream

Part of fgets(3).

Usage

#include <stdio.h>

char *

fgets(char * restrict str, int size, FILE * restrict stream);

#define __STDC_WANT_LIB_EXT1__ 1

char *

gets_s(char *str, rsize_t size);

Library

Standard C Library (libc, -lc)

Overview

The fgets() function reads at most one less than the number of characters specified by size from the given stream and stores them in the string str. Reading stops when a newline character is found, at end-of-file or error. The newline, if any, is retained. If any characters are read and there is no error, a ‘\0’ character is appended to end the string.

The gets_s() function is equivalent to fgets() with a stream of stdin, except that the newline character (if any) is not stored in the string.

The gets() function was unsafe and is no longer available.

Return values

Upon successful completion, fgets() and gets_s() return a pointer to the string. If end-of-file occurs before any characters are read, they return NULL and the buffer contents remain unchanged. If an error occurs, they return NULL and the buffer contents are indeterminate. The fgets() and gets_s() functions do not distinguish between end-of-file and error, and callers must use feof(3) and ferror(3) to determine which occurred.

Errors

[EBADF]

The given stream is not a readable stream.

The function fgets() may also fail and set errno for any of the errors specified for the routines fflush(3), fstat(2), read(2), or malloc(3).

The function gets_s() may also fail and set errno for any of the errors specified for the routine getchar(3).

Releases

23 releases since FreeBSD 11.2-RELEASE; the text changed 14 times.

  1. 11.2
  2. 11.3
  3. 11.4
  4. 12.0
  5. 12.1
  6. 12.2
  7. 12.3
  8. 12.4
  9. 13.0–13.1
  10. 13.2
  11. 13.3–13.4
  12. 13.5
  13. 14.0–14.2
  14. 14.3–14.5
  15. 15.0–16.0

Standards

The fgets() function conforms to ISO/IEC 9899:1999 (“ISO C99”). gets_s() conforms to ISO/IEC 9899:2011 (“ISO C11”) K.3.7.4.1. gets() has been removed from ISO/IEC 9899:2011 (“ISO C11”).

History

The functions fgets() and gets() first appeared in Version 7 AT&T UNIX.

From base 16.0-CURRENT-20261027-61c70dc16a79 in FreeBSD 16.0-CURRENT (2026-10-27). Parsed by carta from the page's mdoc source.