Detecting 64bit compile in C

2019-01-06 18:12发布

is there a C macro or some kind of way that i can check if my c program was compiled as 64bit or 32bit at compile time in C?

Compiler: GCC Operating systems that i need to do the checks on: Unix/Linux

Also how could i check when running my program if the OS is capable of 64bit?

标签: c linux unix gcc
8条回答
闹够了就滚
2楼-- · 2019-01-06 18:36

Use this UINTPTR_MAX value to check build type.

#include <stdio.h>
#include <limits.h>

#if UINTPTR_MAX == 0xffffffffffffffffULL               
# define BUILD_64   1
#endif

int main(void) {

    #ifdef BUILD_64
    printf("Your Build is 64-bit\n");

    #else
    printf("Your Build is 32-bit\n");

    #endif
    return 0;
}
查看更多
ゆ 、 Hurt°
3楼-- · 2019-01-06 18:37

The same program source can (and should be able to) be compiled in 64-bit computers, 32-bit computers, 36-bit computers, ...

So, just by looking at the source, if it is any good, you cannot tell how it will be compiled. If the source is not so good, it may be possible to guess what the programmer assumed would be used to compile it under.

My answer to you is:

There is a way to check the number of bits needed for a source file only for bad programs.

You should strive to make your programs work no matter on how many bits they will be compiled for.

查看更多
登录 后发表回答