What is ssize_t in C?

I previously covered the size_t type in C, which is used to represent the size of an allocated block of memory. But lots of C functions use a type called ssize_t. What’s that? What is the extra s?

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.h>
#include <unistd.h>

ssize_t read(int fildes, void *buf, size_t nbyte);
ssize_t write(int fildes, const void *buf, size_t nbyte);
👋 I'm Jim, a full-stack product engineer. Want to build an amazing product and a profitable business?Read more about me or Get in touch!

More by Jim

Tagged #size_t, #ssize_t, #system-calls, #c, #programming, #posix. All content copyright James Fisher 2017. This post is not associated with my employer.Found an error? Edit this page.