如何使用c程序找到我的机器是16位还是32位还是64位

时间:2022-07-20 02:50:31

Can somebody give simple c-program to find whether my machine is 16-bit or 32-bit or 64-bit ?

有人能给我一个简单的c程序来确定我的机器是16位还是32位还是64位?

6 个解决方案

#1


4  

If you are concern about linux OS only then you can use uname() call.You can pass struct utsname to this API and can get the details. You can get further details on following URL

如果您只关心linux操作系统,那么可以使用uname()调用。您可以将struct utsname传递给这个API,并可以获得详细信息。您可以了解以下URL的更多细节

http://linux.die.net/man/2/uname

http://linux.die.net/man/2/uname

Also looking into the uname command source code can help you more on this.

另外,查看uname命令源代码也可以在这方面提供更多帮助。

#2


1  

As an "implementation detail" this is exactly the sort of thing that is left out of the formal specification for the C language; given that the compiler is theoretically supposed to hide this from you, anything you could do to figure out this information technically depends on "undefined nonstandard behavior."

作为一个“实现细节”,这正是C语言的正式规范中所遗漏的东西;考虑到编译器理论上应该对您隐藏这些信息,您可以做的任何事情从技术上来说都依赖于“未定义的非标准行为”。

That's the pedantic answer. The practical answer is you can use sizeof(int) to determine the register width on your particular architecture with any sensible compiler.

这是迂腐的答案。实际的答案是,可以使用sizeof(int)用任何合理的编译器来确定特定体系结构上的寄存器宽度。

Note that this is determined at compile time, not run time, so it tells you whether your app was compiled in 32-bit or 64-bit (or whatever-bit) mode, not whether it's being eg run on a 64-bit machine emulating 32-bit x86. For that sort of info you need to look at totally platform-specific things like CPUID.

请注意,这是在编译时确定的,而不是在运行时确定的,因此它会告诉您应用程序是在32位模式还是64位模式(或任何)模式下编译的,而不是在模拟32位x86的64位机器上运行。对于这类信息,您需要查看完全特定于平台的内容,比如CPUID。

#3


0  

The compiler has to know at compile time what architecture it is building for, so there should be no need to determine this at runtime.

编译器必须在编译时知道它正在构建什么体系结构,因此不需要在运行时确定这个体系结构。

A compiler will typically have a predefined macro indicating the architecture; you will have to test all the architectures you intend to build for. A list of such macros for various architectures is provided at http://predef.sourceforge.net/prearch.html

编译器通常会有一个预定义的宏来指示架构;您将不得不测试您打算为之构建的所有架构。在http://predef.sourceforge.net/prearch.html中提供了对各种体系结构的这些宏的列表。

#4


0  

There are multiple layers here compiler - OS - Processor. Getting machine arch from a user C program is not advisable because you don't have enough information and its not portable.

这里有多层编译器- OS -处理器。从用户C程序获取机器arch是不可取的,因为您没有足够的信息,而且它是不可移植的。

However if u want to know for specific OS like linux here is the link

但是如果你想知道具体的操作系统,比如linux,这里有链接

You can use the help of the above tools in your program.

您可以在程序中使用上述工具的帮助。

#5


0  

You can use preprocessor:

您可以使用预处理器:

#ifdef __i386__
    blahblah
#elif __arm__
    blahblah
#elif defined(__x86_64__) || defined(_M_AMD64) || defined (_M_X64)
     blahblah
#endif

#6


-2  

This should work:

这应该工作:

#include <iostream>
int main(int argc, char ** arv){
  std::cout << "sizeof(void*)=" << sizeof(void*) << std::endl;
  return 0;
}

#1


4  

If you are concern about linux OS only then you can use uname() call.You can pass struct utsname to this API and can get the details. You can get further details on following URL

如果您只关心linux操作系统,那么可以使用uname()调用。您可以将struct utsname传递给这个API,并可以获得详细信息。您可以了解以下URL的更多细节

http://linux.die.net/man/2/uname

http://linux.die.net/man/2/uname

Also looking into the uname command source code can help you more on this.

另外,查看uname命令源代码也可以在这方面提供更多帮助。

#2


1  

As an "implementation detail" this is exactly the sort of thing that is left out of the formal specification for the C language; given that the compiler is theoretically supposed to hide this from you, anything you could do to figure out this information technically depends on "undefined nonstandard behavior."

作为一个“实现细节”,这正是C语言的正式规范中所遗漏的东西;考虑到编译器理论上应该对您隐藏这些信息,您可以做的任何事情从技术上来说都依赖于“未定义的非标准行为”。

That's the pedantic answer. The practical answer is you can use sizeof(int) to determine the register width on your particular architecture with any sensible compiler.

这是迂腐的答案。实际的答案是,可以使用sizeof(int)用任何合理的编译器来确定特定体系结构上的寄存器宽度。

Note that this is determined at compile time, not run time, so it tells you whether your app was compiled in 32-bit or 64-bit (or whatever-bit) mode, not whether it's being eg run on a 64-bit machine emulating 32-bit x86. For that sort of info you need to look at totally platform-specific things like CPUID.

请注意,这是在编译时确定的,而不是在运行时确定的,因此它会告诉您应用程序是在32位模式还是64位模式(或任何)模式下编译的,而不是在模拟32位x86的64位机器上运行。对于这类信息,您需要查看完全特定于平台的内容,比如CPUID。

#3


0  

The compiler has to know at compile time what architecture it is building for, so there should be no need to determine this at runtime.

编译器必须在编译时知道它正在构建什么体系结构,因此不需要在运行时确定这个体系结构。

A compiler will typically have a predefined macro indicating the architecture; you will have to test all the architectures you intend to build for. A list of such macros for various architectures is provided at http://predef.sourceforge.net/prearch.html

编译器通常会有一个预定义的宏来指示架构;您将不得不测试您打算为之构建的所有架构。在http://predef.sourceforge.net/prearch.html中提供了对各种体系结构的这些宏的列表。

#4


0  

There are multiple layers here compiler - OS - Processor. Getting machine arch from a user C program is not advisable because you don't have enough information and its not portable.

这里有多层编译器- OS -处理器。从用户C程序获取机器arch是不可取的,因为您没有足够的信息,而且它是不可移植的。

However if u want to know for specific OS like linux here is the link

但是如果你想知道具体的操作系统,比如linux,这里有链接

You can use the help of the above tools in your program.

您可以在程序中使用上述工具的帮助。

#5


0  

You can use preprocessor:

您可以使用预处理器:

#ifdef __i386__
    blahblah
#elif __arm__
    blahblah
#elif defined(__x86_64__) || defined(_M_AMD64) || defined (_M_X64)
     blahblah
#endif

#6


-2  

This should work:

这应该工作:

#include <iostream>
int main(int argc, char ** arv){
  std::cout << "sizeof(void*)=" << sizeof(void*) << std::endl;
  return 0;
}