Elimination Elimination - 1 year ago 79
C Question

Can I assume the size of `long int` is always 4 bytes?

Is it always true that

long int
(which as far as I understand is a synonym for
) is
bytes? can I rely on that? If not, could it be true for a POSIX based OS?

Answer Source

The standards say nothing regarding the exact size of any integer types aside from char. Typically, long is 32-bit on 32-bit systems and 64-bit on 64-bit systems.

So no, you can't make any assumptions on size. If you need a type of a specific size, you can use the types defined in stdint.h. It defines the following types:

  • int8_t: signed 8-bit
  • uint8_t: unsigned 8-bit
  • int16_t: signed 16-bit
  • uint16_t: unsigned 16-bit
  • int32_t: signed 32-bit
  • uint32_t: unsigned 32-bit
  • int64_t: signed 64-bit
  • uint64_t: unsigned 64-bit

The stdint.h header is described in section 7.18 of the standard, with exact width types in section The standard states that these typedefs are optional, but they exist on most implementations.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download