31

OS: Debian 9 (Linux 4.9)

Compiler: GCC 8.2

Currently I am including <stddef.h> (where size_t is defined) and <stdint.h> (where most integral types are defined), but I still don't have ssize_t.

Where is it defined?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • 4
    #include – bruno Mar 15 '19 at 20:38
  • 4
    `#include ` or `#include ` – bruno Mar 15 '19 at 20:44
  • 2
    Posix [``](https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/sys_types.h.html) – pmg Mar 15 '19 at 20:48
  • 5
    *+1* for asking a basic question that does not appear to have been asked before. Or I could not find it using two search engines. – jww Mar 16 '19 at 00:36
  • 2
    Note that even if you include ``, the type might not be defined if you specify a strict C standard with GCC (`-std=c99`) rather than the GNU variant (`-std=gnu99`). You then need to enable the POSIX extensions, probably with `#define _XOPEN_SOURCE 700` (the number's easier to remember than `#define _POSIX_C_SOURCE 200809L` which is a valid alternative; there are subtle differences between X/Open and POSIX, but they're minimal and seldom relevant). The `#define` must appear before any system header is included. It could be specified on the command line as `-D_XOPEN_SOURCE=700`. – Jonathan Leffler Jan 23 '21 at 12:34

2 Answers2

24

ssize_t is defined in sys/types.h.

Per the POSIX documentation:

NAME

sys/types.h - data types

SYNOPSIS

#include <sys/types.h>

DESCRIPTION

The header shall define at least the following types:

...

ssize_t

    Used for a count of bytes or an error indication.

Andrew Henle
  • 32,625
  • 3
  • 24
  • 56
6

Since version 5.9, the Linux man-pages document system data types, so that you can find this information easily in a centralized manner.

Just type man ssize_t:

ssize_t(3type)      Linux Programmer’s Manual     ssize_t(3type)

NAME
       ssize_t - count of bytes or an error indication

LIBRARY
       Standard C library (libc)

SYNOPSIS
       #include <sys/types.h>

       typedef /* ... */ ssize_t;

DESCRIPTION
       Used  for  a  count of bytes or an error indication.  Ac‐
       cording to POSIX, it shall be a signed integer type capa‐
       ble  of  storing  values  at  least  in  the  range  [-1,
       SSIZE_MAX],  and  the implementation shall support one or
       more programming environments where the width of  ssize_t
       is no greater than the width of the type long.

       Glibc  and  most  other  implementations provide a length
       modifier for ssize_t for the printf(3) and  the  scanf(3)
       families  of functions, which is z; resulting commonly in
       %zd or %zi for printing ssize_t values.  Although z works
       for ssize_t on most implementations, portable POSIX  pro‐
       grams  should  avoid  using it—for example, by converting
       the value to intmax_t and using its length modifier (j).

VERSIONS
       <aio.h>, <mqueue.h>,  and  <sys/socket.h>  define  ssize_t
       since POSIX.1‐2008.

CONFORMING TO
       POSIX.1‐2001 and later.

NOTES
       The  following  headers  also provide this type: <aio.h>,
       <monetary.h>,   <mqueue.h>,    <stdio.h>,    <sys/msg.h>,
       <sys/socket.h>, <sys/uio.h>, and <unistd.h>.

SEE ALSO
       read(2),   readlink(2),   readv(2),   recv(2),   send(2),
       write(2), ptrdiff_t(3type), size_t(3type)

Linux                      2022‐06‐17             ssize_t(3type)

If you just want ssize_t, you should include <sys/types.h>, which is its canonical header, and probably the lightest one that provides ssize_t. However, it is provided by any of the headers documented, so if you happen to also need a definition in one of those other headers, you can include that other header only.

  • 1
    I'm on Ubuntu 20.04. `man ssize_t` just shows the following for me: `No manual entry for ssize_t`. – Gabriel Staples May 25 '21 at 07:30
  • @GabrielStaples Ubuntu 20.04 has `man-pages-5.05`, per what I read in . You can easily check your version by reading the COLOPHON of `man intro` (or any of the manual pages provided by the `man-pages` project (which on Ubuntu are in packages `manpages` and `manpages-dev`)). – alx - recommends codidact May 25 '21 at 07:35
  • After looking up what [colophon](https://www.wordreference.com/definition/colophon) means, since that word is too big for my vocabulary, I searched the `man intro` page and see my version at the very bottom. Thank you. `COLOPHON This page is part of release 5.05 of the Linux man-pages project.` So, you are correct. Ubuntu 20.04 has man pages version 5.05. – Gabriel Staples May 25 '21 at 07:40
  • 2
    If you're interested in upgrading your manual pages (which is a quite safe and reversible operation), it's trivial to do it from source: `git clone git://git.kernel.org/pub/scm/docs/man-pages/man-pages.git && cd man-pages && make install`. You can `make uninstall` to revert the installation and go back to normal (your system manual pages are not overwritten, as it uses `` for the installation). Just follow the README. – alx - recommends codidact May 25 '21 at 07:44