9

In my code I'm using ssize_t but when I try to compile project I got error: unknown type name ‘ssize_t’; did you mean ‘size_t’?

To compile project I'm using cc -std=c11 -O3 I'm including stdint.h and I've also tried stddef.h and others.

Do I have to use some flags or what?

alk
  • 69,737
  • 10
  • 105
  • 255
Jadw1
  • 139
  • 1
  • 2
  • 3

1 Answers1

14

The "signed-size_t" is not part of standard C, instead it's specific to POSIX (Unix, BSD, Linux, etc) and it's in sys/types.h:

https://pubs.opengroup.org/onlinepubs/7908799/xsh/systypes.h.html

On Windows, ssize_t is not defined but SSIZE_T is - but I assume you're on a POSIX system.

Dai
  • 141,631
  • 28
  • 261
  • 374
  • 1
    Although correct, the link to the POSIX specs you give is quiet a bit outdated. The current one is [here](https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/sys_types.h.html). – alk May 12 '19 at 14:08
  • On top of this, there's unlikely any reason to use `ssize_t` except with the interfaces that return it, which will not be available unless you include a header that defines it. – R.. GitHub STOP HELPING ICE May 12 '19 at 14:09
  • 1
    @R.. Or if you want to create a function that returns a size and can report errors. – alx - recommends codidact May 12 '19 at 14:17
  • @alx That's a bad design, imo. Use a discriminated-union (aka _tagged union_) instead - it'll use the same amount of storage, but clearly signifies intent. – Dai May 21 '21 at 05:08
  • @Dai, that's how most of the standard C library and the POSIX functions and system calls are designed. I don't think it's so bad. In fact, I think the _tagged union_ is bad (because of performance). I'm not sure how you plan to use a `union` without knowing which of the fields you can use, as it's Undefined Behavior. You probably need a `struct` enclosing the `union` and giving information about the field to be used, and that's already a lot of overhead compared to a simple number that can be positive or negative to mean two different things. Moreover, unsigned integers are very dangerous – alx - recommends codidact May 25 '21 at 08:14
  • ... Moreover, unsigned integers are very dangerous, as they silently wrap around. See . So I use `ssize_t` wherever I need to use a `size_t`, unless I have no option. I know it adds portability issues, but I don't plan to program outside of a POSIX environment, anyway. – alx - recommends codidact May 25 '21 at 08:17