Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Casting to unsigned without specifying a type

#include <stdio.h>

int main(){
  ssize_t a= -1;
  size_t b = (unsigned)a;
  return 0;
}

a is 8 bytes all set to 1, however b becomes a 4 byte number when casted to unsigned without doing a proper (unsigned size_t), why is that? why doesn't it turn into an 8 byte unsigned variable?


1 Answers

unsigned is short for writing unsigned int, so unsigned and unsigned int are the same type.

unsigned size_t does not exist; size_t is already unsigned.

why doesn't it turn into an 8 byte unsigned variable?

Because ints are commonly 32 bit or 4 bytes.

If you want fixed width integers you can use stdint.h which defines uint32_t, int32_t etc.

like image 72
marco-a Avatar answered Feb 20 '26 12:02

marco-a



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!