C size_t and ssize_t negative value

polslinux picture polslinux · Aug 29, 2012 · Viewed 15.9k times · Source

size_t is declared as unsigned int so it can't represent negative value.
So there is ssize_t which is the signed type of size_t right?
Here's my problem:

#include <stdio.h>
#include <sys/types.h>

int main(){
size_t a = -25;
ssize_t b = -30;
printf("%zu\n%zu\n", a, b);
return 0;
}

why i got:

18446744073709551591
18446744073709551586

as result?
I know that with size_t this could be possible because it is an unsigned type but why i got a wrong result also with ssize_t??

Answer

cnicutar picture cnicutar · Aug 29, 2012

In the first case you're assigning to an unsigned type - a. In the second case you're using the wrong format specifier. The second specifier should be %zd instead of %zu.