Question

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.

Was it helpful?

Solution

  • 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?

OTHER TIPS

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

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.

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.

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.

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 build time. A comprehensive list of such macros for a variety of compilers, OS's and architectures is defined at: http://predef.sourceforge.net/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top