Can somebody give simple c-program to find whether my machine is 16-bit or 32-bit or 64-bit ?
- This isn't possible in general, but for specific architectures or operating systems it can be done.caf– caf2010-09-06 08:31:00 +00:00Commented Sep 6, 2010 at 8:31
- I am using linux gcc compilerJagan– Jagan2010-09-06 08:31:36 +00:00Commented Sep 6, 2010 at 8:31
- Is there even a 16bit Linux/GCC implementation!?Clifford– Clifford2010-09-06 09:16:00 +00:00Commented Sep 6, 2010 at 9:16
- And are you interested in address range or data bus width? The answer to the last then begs the question: why do you need to know? Answering that may get you a more useful answer.Clifford– Clifford2010-09-06 09:22:45 +00:00Commented Sep 6, 2010 at 9:22
- gcc does not support 16-bit machines.R.. GitHub STOP HELPING ICE– R.. GitHub STOP HELPING ICE2010-09-06 12:24:28 +00:00Commented Sep 6, 2010 at 12:24
6 Answers
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
http://linux.die.net/man/2/uname
Also looking into the uname command source code can help you more on this.
Comments
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."
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.
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.
2 Comments
sizeof(int) for this. It will (in practice) not give you the right answer. Instead, sizeof(size_t), sizeof(ptrdiff_t), or sizeof(char *) would be good choices. (On any sane machine they'll all be the same..)int type that isn't register width?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
5 Comments
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.
However if u want to know for specific OS like linux here is the link
You can use the help of the above tools in your program.
Comments
This should work:
#include <iostream> int main(int argc, char ** arv){ std::cout << "sizeof(void*)=" << sizeof(void*) << std::endl; return 0; }