tags:

views:

151

answers:

6

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

A: 

n = sizeof(void *);

n is equal to 8 on a 64 bit architecture and 4 on a 32 bit architecture.

HPT
No, it will tell you how large the compiler believes the pointer to be. It says nothing about the actual machine.
Ignacio Vazquez-Abrams
But still *typically* 4 on a 16 bit machine, even though the address range may be less (20 bit on 16 bit x86 for example).
Clifford
A: 

This should work:

#include <iostream>
int main(int argc, char ** arv){
  std::cout << "sizeof(void*)=" << sizeof(void*) << std::endl;
  return 0;
}
tibur
See my comment on HPT's answer.
Ignacio Vazquez-Abrams
Also, this is not C.
domen
+1  A: 

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.

Crashworks
Do not use `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..)
R..
sizeof(char *) won't work on platforms with varying pointer sizes (eg x86 in Real Mode with the near/far/huge pointer nonsense). size_t probably will. Which compilers have an `int` type that isn't register width?
Crashworks
A: 

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

Clifford
So what if you run a 32-bit program on a 64-bit system?
atzz
As far as the 32-bit program is concerned, it's running on a 32-bit system. It can't make use of any 64-bit features within its own address/process space. The only reason I could think it might be useful to know if it's on a 32-bit or 64-bit host operating system is to decide which version of an external program to download and install.
R..
@atzz: The program will think that it is running on a 32 bit machine, so any test you devise would return 32bit. The program will be running in the OS's 32bit subsystem which will be a virtual machine.
Clifford
Yes. But the topicstarter is interested in the information about actual hardware. (Or maybe I misunderstood the question...)
atzz
@atzz: I am sure he is; my point is that it is not that simple (and near impossible perhaps). A 32 bit program running in a VM on a 64 bit host is still a 32 bit program and will perform like a 32 bit program, and be subject to the same address and data width restrictions, so there is little advantage in detecting the platform as 64 bit even if it were possible since the program could not take advantage of such knowledge (R..'s point notwithstanding).
Clifford
+4  A: 

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.

Anil Vishnoi
A: 

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.

aeh