fgets(3)

๐Ÿ’– fgets(3) โ€” the "get a whole line, bestie" situation ๐Ÿ’–

okay so real talk: this is fgets from the Standard C library (libc, -lc), and it's literally the moment your girl stops nibbling characters one at a time and just gets a line. FILE get line-string. that's the whole name. iconic. ๐Ÿ’…


โœจ SYNOPSIS โœจ

#include <stdio.h>

char *fgets(int size;
            char s[restrict size], int size, FILE *restrict stream);

like yes that's the signature exactly as it is, i'm not touching it, my hands are OFF it ๐Ÿซถ


๐ŸŒธ DESCRIPTION ๐ŸŒธ

okay bestie listen. fgets() reads in at most one less than size characters from stream, and it stashes them into the buffer that s points at. the <hotpink>one less than</hotpink> part is NOT a typo, it's for the null byte, we'll get there.

the reading stops after:

  • an EOF, or
  • a newline ๐ŸŒˆ

and here's the slay: if a newline is read, it gets stored into the buffer. it's not thrown away. it's coming with us. periodt.

then, after the last character in the buffer, a terminating null byte ('\0') is stored. that's your little bow on top ๐ŸŽ€


๐Ÿ’ซ RETURN VALUE ๐Ÿ’ซ

  • on success, fgets() returns s. like, it hands you back your own buffer. cute.
  • when end of file happens and no characters have been read, NULL is returned. no vibes. empty. ๐Ÿ˜ญ
  • on error, NULL is returned and errno is set to tell you what went down.
so if you get NULL, you gotta check: was it EOF or was it an actual error? don't assume, bestie, go look at errno ๐Ÿ’€

๐Ÿ”ฅ ERRORS ๐Ÿ”ฅ

see fgetc(3). that's where the error lore lives, we don't duplicate it here, iykyk.


๐Ÿงš ATTRIBUTES ๐Ÿงš

for what all these terms even mean, see attributes(7). the table said:

  • fgets() โ€” Thread safety โ€” MT-Safe โœจ
translation: she's MT-Safe. multiple threads can vibe with her at once, no cap. โญ

๐Ÿ‘‘ STANDARDS ๐Ÿ‘‘

C11, POSIX.1-2008.


๐Ÿฆ‹ HISTORY ๐Ÿฆ‹

POSIX.1-2001, C89. she's been here a minute, she's a veteran, she ate. ๐Ÿ‘‘


๐Ÿ’œ SEE ALSO ๐Ÿ’œ


and that's the whole page, bestie!! ๐Ÿ’– this was Linux man-pages 6.20, dated 2026-08-19, and yes it's the fgets(3) page, go read your file, go touch some bytes, go be the main character of your own stream ๐Ÿซถโœจ๐ŸŒธ

๐Ÿ“ฆ where this page comes from
  • arch rolling: man-pages 6.20-1 ยท BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
  • nixpkgs unstable: man-pages 6.20
  • ๐Ÿ“œ the page's license: Linux-man-pages-copyleft
  • ๐Ÿ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms