comparing off_t and ssize_t with other types

后端 未结 3 1141
我寻月下人不归
我寻月下人不归 2020-12-11 11:59

I am new to C and recently ran into some trouble with mismatching data types and their memory allocation. I am writing a very simple program to calculate the xor checksum o

3条回答
  •  悲&欢浪女
    2020-12-11 12:47

    The C standard states that the long type is large enough to represent the constant LONG_MAX, which must be at least 2147483647 (231-1). If we take this lower bound as the value of LONG_MAX, then it's possible that it isn't large enough. It's one less than 2 GiB, after all.

    ssize_t isn't in the C standard, but is defined in the POSIX standard. It must be large enough to represent the constant SSIZE_MAX, which must be at least 32767 (215-1). Don't rely on this type either.

    On my machine, long and ssize_t are both 4 bytes. You can verify the sizes yourself using the sizeof operator. You may get different results. If you want your program to be portable, don't rely on something implementation-specific.

    Lastly, if you really don't want to use those typedefs, I recommend using the unsigned long long type. It's large enough to represent the constant ULLONG_MAX, which must be at least 18446744073709551615 (264-1).

    See also: http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/limits.h.html

提交回复
热议问题