---
name: "fgets"
section: "3"
platform: "linux"
package: "man-pages"
version: "6.19-1"
release: "Arch Linux 2026-11-01"
platforms: ["linux", "freebsd", "macos"]
url: "https://new.carta.tech/fgets"
---

# fgets(3)

FILE get line-string

## NAME

fgets - FILE get line-string

## LIBRARY

Standard C library (*libc*, *-lc*)

## SYNOPSIS

```
#include <stdio.h>
```

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

## DESCRIPTION

**fgets**() reads in at most one less than *size* characters from *stream* and stores them into the buffer pointed to by *s*. Reading stops after an **EOF** or a newline. If a newline is read, it is stored into the buffer. A terminating null byte ('\\0') is stored after the last character in the buffer.

## RETURN VALUE

**fgets**() returns *s* on success.

When end of file occurs while no characters have been read, NULL is returned.

On error, NULL is returned, and *errno* is set to indicate the error.

## ERRORS

See [**fgetc**(3)](/fgetc/3.md).

## ATTRIBUTES

For an explanation of the terms used in this section, see [**attributes**(7)](/attributes/7.md).

| | | |
| --- | --- | --- |
| **Interface** | **Attribute** | **Value** |
| **fgets**() | Thread safety | MT-Safe |

## STANDARDS

C11, POSIX.1-2008.

## HISTORY

POSIX.1-2001, C89.

## SEE ALSO

[**ferror**(3)](/ferror/3.md), [**fgetc**(3)](/fgetc/3.md), [**fgetws**(3)](/fgetws/3.md), [**fopen**(3)](/fopen/3.md), [**fread**(3)](/fread/3.md), [**getline**(3)](/getline/3.md), [**unlocked\_stdio**(3)](/unlocked_stdio/3.md)
