21

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.

N 1.1
  • 12,418
  • 6
  • 43
  • 61
  • almost dup: http://stackoverflow.com/questions/2301007/determine-word-size-of-my-processor – amit kumar Mar 08 '10 at 14:08
  • This tells you what the code was compiled for, it doesn't tell you the capabilities of the CPU. But 32bit code will be running in 32bit mode, so I guess that's all you need to know. – Steve Jessop Mar 08 '10 at 14:19

6 Answers6

9
  • 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
  • 51,233
  • 8
  • 91
  • 143
  • 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 –  Mar 08 '10 at 14:26
  • 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 Mar 08 '10 at 14:30
  • 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 Mar 08 '10 at 15:02
  • @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 Mar 08 '10 at 15:31
6

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
  • 112,946
  • 110
  • 377
  • 526
3

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,645
  • 13
  • 15
3

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.

Raphaël Saint-Pierre
  • 2,498
  • 1
  • 19
  • 23
  • +1. Everybody's all "`sizeof()` is a compile time constant. Yes, but if the flux capacitor has 20 valence electrons in a vacuum..." Then this guy goes "hey did you try plugging it in?" – P.Brian.Mackey May 03 '18 at 14:40
2

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/

Clifford
  • 88,407
  • 13
  • 85
  • 165
  • macros don't run at run-time, Clifford, they are compile-time constructs. – Chris K Mar 08 '10 at 20:39
  • 2
    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 Mar 08 '10 at 20:42
  • @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 Mar 09 '10 at 14:38
  • @mctylr: Duly noted, I have removed the example; now an exercise for the reader! – Clifford Mar 09 '10 at 14:39
2

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.

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765