tags:

views:

443

answers:

6

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\n");
        break;
        case 8: printf("64\n");
        break;
    }
}

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.

Thanks for the advice.

+5  A: 
  • 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() result is mainly a compile time constant, it won't returns something different depending on the OS version it is running under.

What do you want to know really?

AProgrammer
I faced this questions when i was jus curious to explore what we could do from C. But i am expecting this would be needed, when i want to know whether the underlying architecture which my program is running is 32bit or 64bit. based on that i would want to load corresponding library...Yes..Sizeof is a compile time constant...Can you suggest any run time way to find out
james
In general a 32 bit executable won't be able to load a 64 bit library even under a 64 bit OS; libraries and executables need to be compiled under the same -- or at least compatible -- assumptions.
AProgrammer
If you're on an x86 machine, you can execute a CPUID instruction to get processor information. I don't know if other architectures have anything analogous.
TMN
@TMN, my understanding is that prabodh want to know what the OS support... (I'm still wondering if it has any use excepted for diagnostic)
AProgrammer
A: 

To answer your question strictly as given:

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main(void) {
    long wordBits = sysconf(_SC_WORD_BIT);
    if (wordBits == -1 && errno == EINVAL)
        return EXIT_FAILURE;
    else
        printf("%ld\n", wordBits);
    return EXIT_SUCCESS;
}

This would work in any situation where glibc is correctly configured, and would print your register size to stdout, or return an exit code of 1 otherwise.

See Also

Matt Joiner
Why a downvote ?
RaphaelSP
lol this is hilarious
Matt Joiner
+1  A: 

In Windows you could look at the PROCESSOR_ARCHITECTURE environment variable. It will return either "x86" when a program is running in 32 bit mode (either because it is running under a 32 bit OS or because it is running on a 64 bit OS but as a 32 bit program under WOW64) or either "IA64"or "AMD64" if running as native 64 bit program on a 64 bit OS.

Andrew O'Reilly
+1  A: 

In addition to compile-time methods, and if you are running on Windows: a call to IsWow64Process ( http://msdn.microsoft.com/en-us/library/ms684139.aspx ) will return true if a process is a 32-bit one running under a 64-bit Windows.

RaphaelSP
A: 

I think your solution is probably valid in most common cases; certainly in all standard IA64 data models pointers are 64bit. This may not however be true of all architectures in theory. It may be safer to test sizeof(uintptr_t) if the compiler has the C99 header; but again it assumes that address width is indicative of register width; it depends whether by "64bit" you are referring to address range or integer range - they need not be the same.

Since 32bit and 64bit compilation requires either a different compiler or a different compiler switch, the target architecture must be known at build-time, and need not be determined at run-time.

Most compilers provide built-in architecture macros to allow this to be determined at runtime. A comprehensive list of such macros for a variety of compilers, OS's and architectures is defined at: http://predef.sourceforge.net/

Clifford
macros don't run at run-time, Clifford, they are compile-time constructs.
Chris Kaminski
Be careful, don't confuse IA-64 with IA-32e which is also referred to as Intel 64, amd64, x86-64. The IA-64 is Itanium CPU, which is **not** backwards compatible with x86 / IA-32.
mctylr
@Chris Kaminski: Yes; that is exactly what I said, and was kind of my point; you compile for a target, so the information is known at compile time.
Clifford
@mctylr: Duly noted, I have removed the example; now an exercise for the reader!
Clifford
A: 

The only way to answer this question is either:

(1) Use some system-specific feature (API call, environment variable, etc) that would tell you whether the OS is 32 or 64 bit.

or

(2) Use some compiler-provided macro that would tell you the same thing (if available).

There's no way to determine what kind of OS you have by any built-in standard language features. They are provided by the compiler and completely independent from OS.

AndreyT