As title says, is there any elegant and safe way to determine if architecture is 32bit or 64bit. By elegant, you can think of precise, correct, short, clean, and smart way. By safe, think of safe in term of standard, C89/C99, and operating system independence.
-
13In C, the question is ambiguous: do you want to find out whether the code of the program is 64-bit code, or whether the CPU supports 64-bit code (i.e. as a compile-time or a run-time test)? – Martin v. Löwis Aug 17 '09 at 14:19
-
6do you mean at compile time or at runtime? Any specific OS? – Nathan Fellman Aug 17 '09 at 14:19
-
1The question cannot be answered without first defining what you mean by a 32-bit or 64-bit architecture. There is no universally accepted definition. – Keith Thompson Oct 12 '14 at 19:04
7 Answers
short answer: no
long answer: it depends on too many OS/compiler combinations. For example at runtime, on linux you can query the proc filesystem whereas on windows you can query the register.
You can prove that the compiler that are being used for compilation has a 32/64 bits target using something like:
bool is_32bit() {
return sizeof(int *) == 4;
}
bool is_64bit() {
return sizeof(int *) == 8;
}
this could works under few assumptions (e.g. it works at runtime). You can search for compile-time #define
for your platform but it is a well-known mess.

- 114,442
- 31
- 189
- 228
-
If you turn optimization on it won't evaluate at runtime, unneeded 32/64 bits code will be removed. Also, *bool* is not in any C standard. – Aug 18 '09 at 00:26
-
5"Also, bool is not in any C standard." - false, it is in the C99 standard in
- http://en.wikipedia.org/wiki/Stdbool.h – Peter Burns Aug 18 '09 at 01:10 -
oops. point taken, still, it isn't C89 which is specified in the question probably because of this kind of weird things in c99. – Aug 18 '09 at 01:38
If you are using GCC (as indicated in the tags), you can test, as a compile-time test
#if __SIZEOF_POINTER__ == 8
to find out whether it's a 64-bit system. Make sure the GCC version you are using defines __SIZEOF_POINTER__
at all before using it.

- 124,830
- 17
- 198
- 235
-
1
-
1+0 dfa's answer is portable(once you remove his extraneous bool type) and will make for cleaner code. The function will always return either true or false and the compiler will remove the unneeded code. – Aug 18 '09 at 00:21
The size of pointers isn't really a good thing to test - there's not much in standard C that you can do with the result of that test anyway.
My suggestion is test ((size_t)-1)
, the largest object size that C understands:
if ((size_t)-1 > 0xffffffffUL)
{
printf("> 32 bits\n");
}
else
{
printf("<= 32 bits\n");
}
If it's greater than 0xffffffffUL
then you can in principle have objects larger than 2**32 - 1
bytes, which seems like a more meaningful test than a nebulous "32 bits versus 64 bits".
(For example, if you know that the maximum value of size_t
is only 2**32 - 1
, then there's no point trying to mmap()
a region bigger than 1 or 2 GB.)

- 233,326
- 40
- 323
- 462
The most common way is to test sizeof(void*)
and sizeof(int)
(note that they do not necessarily have to be the same).
Another possibility on x86/x64 CPUs is to test for the 'lm' flag. If it is present the CPU understands the AMD64 instruction set.

- 50,202
- 14
- 95
- 141
-
What you suggest is to find both sizes and then determine architecture. Other suggestion, to test for the 'lm' flag sounds that it requires assembler code in C. Right? – mtasic85 Aug 17 '09 at 14:27
-
1I've only seen the test for specific instructions in code (and it was assembler, yes). Many OSes expose the CPU flags in userland however. Linux for example gives you this information in /proc/cpuinfo. But this would result in OS dependent code. – ypnos Aug 17 '09 at 14:38
-
1What if you've got a 32 bit compiler version running on a 64-bit machine, surely that wouldn't work? – hookenz Aug 17 '09 at 22:34
-
1@Matt: You have to decide what you want. If you want information about the host system, try CPU flags. If you want information about the build architecture, use sizeof(). – ypnos Aug 18 '09 at 15:11
-
2sizeof(int) is pretty useless in this situation, as it is 32bit on all of the most common 64bit platforms. – Kaiserludi Nov 06 '15 at 17:00
A safe and portable technique is unfortunately impossible (because safe and portable only allows you the rules in the C Standard).
sizeof(int)
with some of the more common compilers may give you 4 for a 32 bit platform and 8 for a 64 bit platform but this is not guaranteed.
All the C standard says is that an int should be the 'natural' size for calculations on the target, and so many compilers have left sizeof(int) as 4 even in a 64 bit world, on the grounds that it is 'enough'.
sizeof(void*)
is better because a pointer must be the appropriate size to address the whole address space. sizeof(void*)
is therefore likely to give you 4 or 8 as appropriate.
Technically though, even this isn't guaranteed as a sizeof gives you the number of bytes needed to store something, and a byte doesn't have to be 8 bits. A byte is technically the smallest addressable unit of memory which just happens to be 8 bits on most platforms people are used to. 8 bit addressable is very common, but I work with chips that are 16 bit addressable and 16 bit word size (so sizeof(int)
is 1).
So, if your byte size is not 8 bit, then sizeof(void*)
could give you a variety of values.
On the other hand, if you are merely trying to differentiate between x86 and x64 (32bit and 64 bit PC processors) then sizeof(void*) will be sufficient, and portable across compilers.

- 871
- 6
- 18
-
1You can multiply sizeof(void *) by CHAR_BIT to find its size in bits (although but the standard doesn't guarantee that all the bits in the representation are value bits - in fact in current x86_64 implementations there's typically only somewhere in the region of 48 value bits in a virtual address). – caf Aug 17 '09 at 23:42
32-bit on the code bank or 32-bit on the data bank. :-) 8086 processors had 16-bit data with 20-bit code memory. Also, modern Havard machines do odd things with code/data separation...
You might check the cpuid
instruction for x86 processors. Other processor families may not have such an instruction...YMMV.

- 39,638
- 28
- 112
- 212
int iedx;
__asm
{
mov eax, 0x80000001;
cpuid;
mov, iedx,edx;
}
if (iedx & (1 << 29))
{
return 1;
}
return 0;

- 363
- 1
- 3
- 11
-
computer architecture in most times refers not to x86 and x86_64, but to generic architecture. This code is not standard and not portable. – osgx Mar 21 '10 at 20:44