stdio.h(7POSIX)

πŸ’– PROLOG πŸ’–

Okay bestie, real talk before we get into it: this manual page is part of the POSIX Programmer's Manual. The Linux implementation of this interface may differ (consult the corresponding Linux manual page for details of Linux behavior), or the interface may not be implemented on Linux. So like… check your local manpage too, no cap.


✨ NAME ✨

stdio.h β€” standard buffered input/output πŸ’…

That's it. That's the name. We're talking the header that runs basically every C program's whole communication-with-the-world situation. Iconic behavior honestly.


🌸 SYNOPSIS 🌸

#include <stdio.h>

One line. That's the whole incantation. πŸͺ„


πŸ¦‹ DESCRIPTION πŸ¦‹

So here's the vibe: some of the functionality described on this reference page extends the ISO C standard. If your app wants those extra symbols to be visible in this header, it shall define the appropriate feature test macro (see the System Interfaces volume of POSIX.1‐2017, Section 2.2, The Compilation Environment). No macro, no symbol, bestie. That's the deal.

πŸ’œ The typedefs, aka the data types

The <stdio.h> header shall define the following data types through typedef:

  • FILE β€” a structure containing information about a file. (she's the moment, she's the main character, everyone's holding a pointer to her)
  • fpos_t β€” a non-array type containing all information needed to specify uniquely every position within a file.
  • off_t β€” as described in <sys/types.h>.
  • size_t β€” as described in <stddef.h>.
  • ssize_t β€” as described in <sys/types.h>.
  • va_list β€” as described in <stdarg.h>.

πŸŽ€ The macros that expand to integer constant expressions

The <stdio.h> header shall define the following macros which shall expand to integer constant expressions:

  • BUFSIZ β€” size of <stdio.h> buffers. This shall expand to a positive value.
  • L_ctermid β€” maximum size of character array to hold ctermid() output.
  • L_tmpnam β€” maximum size of character array to hold tmpnam() output.

πŸ’€ The macros with distinct values (buffering era)

The <stdio.h> header shall define the following macros which shall expand to integer constant expressions with distinct values:

  • _IOFBF β€” input/output fully buffered.
  • _IOLBF β€” input/output line buffered.
  • _IONBF β€” input/output unbuffered.

⭐ The macros with distinct values (seeking era)

The <stdio.h> header shall define the following macros which shall expand to integer constant expressions with distinct values:

  • SEEK_CUR β€” seek relative to current position.
  • SEEK_END β€” seek relative to end-of-file.
  • SEEK_SET β€” seek relative to start-of-file.

πŸ”₯ The macros that are implementation limits

The <stdio.h> header shall define the following macros which shall expand to integer constant expressions denoting implementation limits:

  • {FILENAME_MAX} β€” maximum size in bytes of the longest pathname that the implementation guarantees can be opened.
  • {FOPEN_MAX} β€” number of streams which the implementation guarantees can be open simultaneously. The value is at least eight.
  • {TMP_MAX} β€” minimum number of unique filenames generated by tmpnam(). Maximum number of times an application can call tmpnam() reliably. The value of {TMP_MAX} is at least 25.
    • On XSI-conformant systems, the value of {TMP_MAX} is at least 10000. glow up fr fr

🧚 The sad one (it's negative, sorry)

The <stdio.h> header shall define the following macro which shall expand to an integer constant expression with type int and a negative value:

  • EOF β€” end-of-file return value. (she's negative but she's valid πŸ’…)

🌈 NULL and the string constant

The <stdio.h> header shall define NULL as described in <stddef.h>.

The <stdio.h> header shall define the following macro which shall expand to a string constant:

  • P_tmpdir β€” default directory prefix for tempnam().

πŸ‘‘ The standard streams

The <stdio.h> header shall define the following macros which shall expand to expressions of type "pointer to FILE" that point to the FILE objects associated, respectively, with the standard error, input, and output streams:

  • stderr β€” standard error output stream.
  • stdin β€” standard input stream.
  • stdout β€” standard output stream.

πŸ“ The functions

The following shall be declared as functions and may also be defined as macros. Function prototypes shall be provided.

void     clearerr(FILE *);
char    *ctermid(char *);
int      dprintf(int, const char *restrict, ...)
int      fclose(FILE *);
FILE    *fdopen(int, const char *);
int      feof(FILE *);
int      ferror(FILE *);
int      fflush(FILE *);
int      fgetc(FILE *);
int      fgetpos(FILE *restrict, fpos_t *restrict);
char    *fgets(char *restrict, int, FILE *restrict);
int      fileno(FILE *);
void     flockfile(FILE *);
FILE    *fmemopen(void *restrict, size_t, const char *restrict);
FILE    *fopen(const char *restrict, const char *restrict);
int      fprintf(FILE *restrict, const char *restrict, ...);
int      fputc(int, FILE *);
int      fputs(const char *restrict, FILE *restrict);
size_t   fread(void *restrict, size_t, size_t, FILE *restrict);
FILE    *freopen(const char *restrict, const char *restrict,
             FILE *restrict);
int      fscanf(FILE *restrict, const char *restrict, ...);
int      fseek(FILE *, long, int);
int      fseeko(FILE *, off_t, int);
int      fsetpos(FILE *, const fpos_t *);
long     ftell(FILE *);
off_t    ftello(FILE *);
int      ftrylockfile(FILE *);
void     funlockfile(FILE *);
size_t   fwrite(const void *restrict, size_t, size_t, FILE *restrict);
int      getc(FILE *);
int      getchar(void);
int      getc_unlocked(FILE *);
int      getchar_unlocked(void);
ssize_t  getdelim(char **restrict, size_t *restrict, int,
             FILE *restrict);
ssize_t  getline(char **restrict, size_t *restrict, FILE *restrict);
char    *gets(char *);
FILE    *open_memstream(char **, size_t *);
int      pclose(FILE *);
void     perror(const char *);
FILE    *popen(const char *, const char *);
int      printf(const char *restrict, ...);
int      putc(int, FILE *);
int      putchar(int);
int      putc_unlocked(int, FILE *);
int      putchar_unlocked(int);
int      puts(const char *);
int      remove(const char *);
int      rename(const char *, const char *);
int      renameat(int, const char *, int, const char *);
void     rewind(FILE *);
int      scanf(const char *restrict, ...);
void     setbuf(FILE *restrict, char *restrict);
int      setvbuf(FILE *restrict, char *restrict, int, size_t);
int      snprintf(char *restrict, size_t, const char *restrict, ...);
int      sprintf(char *restrict, const char *restrict, ...);
int      sscanf(const char *restrict, const char *restrict, ...);
char    *tempnam(const char *, const char *);
FILE    *tmpfile(void);
char    *tmpnam(char *);
int      ungetc(int, FILE *);
int      vdprintf(int, const char *restrict, va_list);
int      vfprintf(FILE *restrict, const char *restrict, va_list);
int      vfscanf(FILE *restrict, const char *restrict, va_list);
int      vprintf(const char *restrict, va_list);
int      vscanf(const char *restrict, va_list);
int      vsnprintf(char *restrict, size_t, const char *restrict,
             va_list);
int      vsprintf(char *restrict, const char *restrict, va_list);
int      vsscanf(const char *restrict, const char *restrict, va_list);

Sixty-something prototypes. She ate and left no crumbs. 🍽️✨

Also: inclusion of the <stdio.h> header may also make visible all symbols from <stddef.h>.

The following sections are informative. (translation: the spec-y part is over, now we're just chatting)


🌟 APPLICATION USAGE 🌟

Since standard I/O streams may use an underlying file descriptor to access the file associated with a stream, application developers need to be aware that {FOPEN_MAX} streams may not be available if file descriptors are being used to access files that are not associated with streams.

real talk: your file descriptors and your streams are sharing the same budget, bestie. Don't max out one and then act surprised when the other one ghosts you. 😭

πŸ’… RATIONALE πŸ’…

There is a conflict between the ISO C standard and the POSIX definition of the {TMP_MAX} macro that is addressed by ISO/IEC 9899:1999 standard, Defect Report 336. The POSIX standard is in alignment with the public record of the response to the Defect Report. This change has not yet been published as part of the ISO C standard.

So like… they beefed, and POSIX is on the record about it. The drama is documented. πŸ“œ


🫢 FUTURE DIRECTIONS 🫢

None.

nothing to see here. she's done evolving. periodt.


πŸ” SEE ALSO πŸ”

  • <stdarg.h>
  • <stddef.h>
  • <sys_types.h>

The System Interfaces volume of POSIX.1‐2017, Section 2.2, The Compilation Environment, clearerr(), ctermid(), fclose(), fdopen(), feof(), ferror(), fflush(), fgetc(), fgetpos(), fgets(), fileno(), flockfile(), fmemopen(), fopen(), fprintf(), fputc(), fputs(), fread(), freopen(), fscanf(), fseek(), fsetpos(), ftell(), fwrite(), getc(), getchar(), getc_unlocked(), getdelim(), getopt(), gets(), open_memstream(), pclose(), perror(), popen(), putc(), putchar(), puts(), remove(), rename(), rewind(), setbuf(), setvbuf(), stdin, system(), tempnam(), tmpfile(), tmpnam(), ungetc(), vfprintf(), vfscanf()

That's a whole friend group right there. Go say hi. πŸ¦‹


Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1-2017, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html.

Any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. To report such errors, see https://www.kernel.org/doc/man-pages/reporting_bugs.html.


okay that's the whole page, and if you made it this far β€” congrats, you now know more about stdio.h than 99% of people who use it every single day. πŸ’€πŸ’–

this one came from IEEE/The Open Group, dated 2017, and it's filed under stdio.h(7POSIX) in the POSIX Programmer's Manual. πŸŽ€ byeee, go write some C 🫢✨