0

On a system where both long and int is 4 bytes which is the best and why?

typedef unsigned long u32;

or

typedef unsigned int u32;

note: uint32_t is not an option

  • Is it C or C++ or some othe language? Add the tag. – Yu Hao Mar 14 '14 at 01:22
  • Can you explain why Standard C and Standard C++ are "not an option" ? You might need to mention which platform and compiler you are using, if they are not standards-compliant. – M.M Mar 14 '14 at 02:55

3 Answers3

1

Nowadays every platform has stdint.h or its C++ equivalent cstdint which define uint32_t. Please use the standard type rather than creating your own.

nodakai
  • 7,773
  • 3
  • 30
  • 60
0

The size will be the same between both, so it depend only on your use. If you need to store decimal values, use long.

A better and complete answer here: https://stackoverflow.com/questions/271076/what-is-the-difference-between-an-int-and-a-long-in-c/271132

Edit: I'm not sure about decimal with long, if someone can confirm, thanks.

Vadorequest
  • 16,593
  • 24
  • 118
  • 215
  • Can't you store decimal values using long? That what I though, but I'm not used to deal with C++, so I'm not sure at all. If you can't then tell me, I'll update the answer. I still think the link is the best explanation. – Vadorequest Feb 14 '14 at 14:19
0

Since you said the standard uint32_t is not an option, using long and int are both correct on 32-bit machines, I'll say

typedef unsigned int u32;

is a little better, because on two popular 64-bit machine data models (LLP64 and LP64), int is still 32-bit, while long could be 32-bit or 64-bit. See 64-bit data models

Yu Hao
  • 119,891
  • 44
  • 235
  • 294