Is there a 64 bit type that in every OS(32/64 bit) and for every compiler has a size of 64?
The same question is also for 32 bit type. (It should be int?)
The origin of the question is : I am implementing the system which has 2 kinds of instructions :
- 32 bit
- 64 bit
I want to write something like:
typedef int instruction32bit;
typedef long long instruction64bit //it is not correct some system have sizeof(long long) = 128
If you want your code to be truly portable, then you probably want to typedef your own type, and use for example
This will work MOST of the time, but if it doesn’t for a particular system/compiler/whatever, you can add do something like this:
Of course, for each model of compiler/OS (or group thereof) that doesn’t support int32_t and int64_t, you probably will need a special
#ifdef.This is exactly what all truly portable code does, because no matter how much you find that “nearly all compilers do X”, if you get your code popular enough, there’s always someone who wants to compile the code with “Bob’s Compiler Project” which doesn’t have this feature. Of course, the other thing is to just leat those who use “Bob’s compiler” edit the typedef itself, and not accept the “For Bob’s compiler, you need this …” patch that inevitably gets sent your way.
As Carl Norum points out in a comment, the
#ifdefmay be possible to convert to a#ifin many cases, and then use generic types such asintandlong.