How to find if an OS is 32 bit or 64 bit using a program ?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Well my answer is that for a 64 bit OS, the pointer size should be 8 byte and for a 32 bit OS pointer size should be 4 byte ?
No. That is testing whether the program itself was compiled 32 bit or 64 bit. That doesn't tell you whether the OS is 32 bit or 64 bit.
In the x86 world, you can compile and run 32 bit programs under a 64 bit OS. Also, nothing in Linux itself says you can't compile and run 64 bit programs under a 32 bit OS. The x86_64 architecture won't let you do that and I don't know of any Linux ports to architectures that let you do that. But if the architecture let you, Linux probably would let you.
So checking the sizeof(void*) should be good enough. But is there any other way ?
There are ways for a human to do it that don't seem to require a human level of intelligence. For the basic mechanics (as opposed to the intelligence) of doing almost anything in Linux, if a human can do it then a program could as well.
Hko already gave you a more concrete version of that answer (a human might run uname and look at the result).
There are more sound ways to find out the bit-size of the system, but gergely89 gives a quite interesting view on dealing with some value comparing tasks that will certainly give different results on 32-bit and 64-bit systems. He didn't mark his sample as the way to test for 64-bit systems, but as a hint on coding differences. At least that's how I interpreted it. Thanks anyway!
Say, i don't know if the OS in question is windows or *nix.
So is there a way, i can write a program and conclude that the OS in use is 32 bit or 64 bit.
Yes but it is going to be platform specific so would have to hide it under a layer of abstraction. For example in windows you could call GetSystemInfo and check the processor type via "wProcessorArchitecture" in the returned SYSTEM_INFO. On Linux you maybe use the same method as wine and parse "/proc/cpuinfo" checking for lm (long mode) in the flags in cpuinfo, if present it is 64bit system.