C Reference ยท stdio.h
C getline() function (POSIX)
Reference for POSIX getline(): read a whole line of any length into a growing heap buffer, handle the newline, reuse the buffer and free it correctly.
getline() reads one whole line from a stream into a buffer that it allocates and grows for you. Unlike fgets(), you never have to guess a maximum line length: the function reallocates the buffer until the line fits.
It is a POSIX function (and part of POSIX.1-2008), not part of ISO C, so it is declared in <stdio.h> only when POSIX features are requested. Under -std=c23 with GCC you must define _POSIX_C_SOURCE before including any header. It is available on Linux and macOS; Windows with MSVC does not provide it.
#define _POSIX_C_SOURCE 200809L #include <stdio.h> ssize_t getline(char **restrict lineptr, size_t *restrict n, FILE *restrict stream);
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Syntax
ssize_t getline(char **restrict lineptr, size_t *restrict n, FILE *restrict stream);
| Name | Description |
|---|---|
lineptr |
Address of a char *. It must hold NULL or a pointer from malloc() (not an array on the stack). The function may replace it with a bigger block. |
n |
Address of a size_t holding the current size of that block (0 when *lineptr is NULL). Updated when the block grows. |
stream |
An open input stream. |
Returns: the number of characters read, including the newline if there was one but not the terminating null character. Returns -1 at end-of-file (when nothing was read) or on error.
Errors / undefined behavior: on failure errno is set (EINVAL for bad arguments, ENOMEM when allocation fails, or the stream's own error). Even on failure *lineptr may point to a block that you must free(). Passing a pointer that was not allocated with malloc(), or a *n larger than the real block, is undefined behavior.
Usage notes
- The newline is kept. As with
fgets(), strip it withline[strcspn(line, "\n")] = '\0';or by checkingline[length - 1]. The last line of a file may have no newline. - Reuse the buffer. Declare
lineandcapacityonce outside the loop and free once afterwards. Allocating inside the loop causes a leak on every iteration. - Always
free(line), even when the very first call returns-1; the function might have allocated memory before failing. - Embedded null characters are stored faithfully. Because the return value is the true length,
getline()is the right tool for input that may contain'\0'(wherestrlen()would be wrong). - Unbounded memory. A hostile input with no newline makes the buffer grow until memory runs out. For untrusted input, enforce your own maximum length, or use
fgets()with a fixed buffer. ssize_tis a signed type from<sys/types.h>(included by<stdio.h>when POSIX is enabled). Do not store the result insize_t, or-1becomes a huge number.- Related function:
getdelim()does the same with a delimiter of your choice instead of'\n'. - Portability: C23 and earlier standards do not include
getline(). For code that must also build with MSVC, write a small wrapper aroundfgets()andrealloc().
Related
fgets,fgetcandfputc,realloc,free- Lessons: User input, Text files, Dynamic memory