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?
ssize_t is defined in sys/types.h.
NAME
sys/types.h - data types
SYNOPSIS
#include <sys/types.h>DESCRIPTION
The header shall define at least the following types:
...
ssize_tUsed for a count of bytes or an error indication.
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.
man ssize_t just shows the following for me: No manual entry for ssize_t.man-pages-5.05, per what I read in <pkgs.org/search/?q=manpages>. 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)).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.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 </usr/local/share/man/> for the installation). Just follow the README.
#include <unistd.h>or#include <sys/types.h><sys/types.h><sys/types.h>, 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 200809Lwhich is a valid alternative; there are subtle differences between X/Open and POSIX, but they're minimal and seldom relevant). The#definemust appear before any system header is included. It could be specified on the command line as-D_XOPEN_SOURCE=700.