简体   繁体   English

32位还是64位? 使用C代码

[英]32-bit or 64-bit? Using C code

有没有办法通过在C中写入一些代码来确定机器是32位还是64位?

#include <limits.h>
/* ... */
printf("This machine's pointers to void, in a program "
       "compiled with the same options as this one and "
       "with the same compiler, use %d bits\n",
    (int)sizeof (void*) * CHAR_BIT);

If by "some code in C" you mean standard C code, then the answer is no, it is not possible. 如果用“C中的一些代码”表示标准C代码,则答案是否定的,这是不可能的。 For a C program, the "world" it can see is created entirely by the implementation (by the compiler). 对于C程序,它可以看到的“世界”完全由实现(由编译器)创建。 The compiler can emulate absolutely any "world", completely unrelated to the underlying hardware. 编译器可以完全模拟任何与底层硬件完全无关的“世界”。

For example, you can run a 32-bit compiler on a 64-bit machine and you will never be able to detect the fact that this is a 64-bit machine from your program. 例如,您可以在64位计算机上运行32位编译器,并且您永远无法从程序中检测到这是64位计算机的事实。

The only way you can find out anything about the machine is to access some non-standard facilities, like some OS-specific API. 您可以找到有关该机器的任何信息的唯一方法是访问一些非标准设施,例如某些特定于操作系统的API。 But that goes far beyond the scope of C language. 但这远远超出了C语言的范围。

Needless to add, the already suggested in other answers methods based on sizeof and other standard language facilities do not even come close detect the bit-ness of the machine. 不用补充一点,基于sizeof和其他标准语言设施的其他答案方法中已经提出的建议甚至不能近距离地检测机器的位数。 Instead they detect the bit-ness of the platform provided by the compiler implementation, which in general case is a totally different thing. 相反,他们会检测编译器实现提供的平台的位数,这在一般情况下是完全不同的。

There is no such thing as 32 bit or 64 bit environments, this is just too much of an oversimplification. 没有32位或64位环境这样的东西,这太过于过于简单了。 You have several characteristics of integer, pointer, and float sizes that come into play if you want to write a portable application. 如果要编写可移植应用程序,则可以使用整数,指针和浮点大小的几个特性。

The size of your different integer types can be checked with the appropriate macros such as UINT_MAX etc, pointer size by UINTPTR_MAX . 可以使用适当的宏(例如UINT_MAX等)检查不同整数类型的大小,指针大小( UINTPTR_MAX Since there might be padding bits, to deduce the width of the types you should print something like (unsigned long long)(T)-1 where T is an unsigned type. 由于可能存在填充位,为了推断类型的宽度,您应该打印类似(unsigned long long)(T)-1 ,其中T是无符号类型。

The pointer size 指针大小

sizeof (void *) * CHAR_BIT

is a good indicator, but might be misleading on more exotic architectures (eg if the size of the address bus is no multiple of the word size) and only represent the execution environment (which might be different from the actual hardware - see AndreyT's answer ). 是一个很好的指标,但可能会误用更奇特的架构(例如,如果地址总线的大小不是字大小的倍数)并且仅代表执行环境(可能与实际硬件不同 - 请参阅AndreyT的答案 ) 。

During the preprocessing phase, the best you can do within the framework of the C language is something like 在预处理阶段,你可以在C语言框架内做的最好的事情是这样的

UINTPTR_MAX == UINT64_MAX

The same limitations as above apply. 与上述相同的限制适用。

Yes, if running on an x86 processor use the CPUID instruction: 是的,如果在x86处理器上运行,请使用CPUID指令:

http://en.wikipedia.org/wiki/CPUID http://en.wikipedia.org/wiki/CPUID

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM