Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where is ssize_t defined in Linux?

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?

like image 415
alx Avatar asked Mar 15 '19 20:03

alx


People also ask

What data type is Ssize_t?

The datatype size_t is unsigned integral type. It represents the size of any object in bytes and returned by sizeof operator. It is used for array indexing and counting.

Is Ssize_t signed?

In short, ssize_t is the same as size_t , but is a signed type - read ssize_t as “signed size_t ”. ssize_t is able to represent the number -1 , which is returned by several system calls and library functions as a way to indicate error. For example, the read and write system calls: #include <sys/types.

What is Off_t?

off_t is normally defined as a signed, 32-bit integer. In the programming environment which enables large files, off_t is defined to be a signed, 64-bit integer. offset_t. 64-bit file offset, measured in bytes from the beginning of a file or device.


2 Answers

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.

like image 101
Andrew Henle Avatar answered Sep 21 '22 14:09

Andrew Henle


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
              Include:  <sys/types.h>.   Alternatively, <aio.h>, <monetary.h>,
              <mqueue.h>, <stdio.h>, <sys/msg.h>, <sys/socket.h>, <sys/uio.h>,
              or <unistd.h>.

              Used  for a count of bytes or an error indication.  According to
              POSIX, it shall be a signed integer type capable of storing val-
              ues  at  least in the range [-1, SSIZE_MAX], and the implementa-
              tion 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 func-
              tions, which is z; resulting commonly in %zd or %zi for printing
              ssize_t  values.  Although z works for ssize_t on most implemen-
              tations, portable POSIX programs should avoid using it--for  ex-
              ample,  by converting the value to intmax_t and using its length
              modifier (j).

              Conforming to: POSIX.1-2001 and later.

              See also:  read(2),  readlink(2),  readv(2),  recv(2),  send(2),
              write(2)

              See also the ptrdiff_t and size_t types in this page.

And later in the NOTES section of that same page:

NOTES
       [...]

   Conventions used in this page
       [...]

       In  "Include",  we  first  note the "primary" header(s) that define the
       type according to either the C or POSIX.1 standards.   Under  "Alterna-
       tively",  we  note  additional headers that the standards specify shall
       define the type.

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.

like image 27
alx Avatar answered Sep 19 '22 14:09

alx