Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
409 views
in Technique[技术] by (71.8m points)

c - how to find if the machine is 32bit or 64bit

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
  • 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?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...