问题描述
无论如何都可以从 C prog 中找出操作系统当前是在 32 位还是 64 位模式下运行.我正在使用一个简单的程序如下
Is there anyway from a C prog to find whether the OS is currently running in 32bit or 64bit mode. I am using a simple program as below
int main(void){
switch(sizeof(void*)){
case 4: printf("32
");
break;
case 8: printf("64
");
break;
}
}
这是正确的方法吗?此代码是否适用于所有场景,例如,如果硬件是 64 位而操作系统是 32 位,它将返回什么?我没有机器在不同的配置中测试这个.
Is this a correct approach ?Would this code work in all the scenarios like, If the hardware is 64bit and the OS is 32bit what would it return ? I don't have machine to test this in diff configurations.
谢谢你的建议.
推荐答案
一般来说,一个 32 位的可执行文件将无法判断它是在 64 位操作系统下运行还是在 32 位操作系统下运行(某些操作系统可能有办法分辨,我不知道,但我知道尚未搜索),64 位可执行文件将无法在 32 位操作系统下运行(如果您不考虑 32 位操作系统模拟运行 64 位操作系统的处理器的可能性...)
In general, a 32 bits executable won't be able to tell if it is running under a 64 bit OS or a 32 bit one (some OS could have a way to tell, I know of none but I haven't searched), a 64 bit executable won't run under a 32 bit OS (if you discount the possibility for the 32 bits OS to emulate a processor running a 64 bits OS...)
sizeof() 结果主要是一个编译时间常数,它不会根据它运行的操作系统版本返回不同的东西.
sizeof() result is mainly a compile time constant, it won't returns something different depending on the OS version it is running under.
你真正想知道什么?
这篇关于如何查看机器是32位还是64位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!