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

前端 未结 6 474
长情又很酷
长情又很酷 2020-12-13 20:51

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){
     s         


        
6条回答
  •  长情又很酷
    2020-12-13 21:40

    To answer your question strictly as given:

    #include 
    #include 
    #include 
    #include 
    
    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

    • sysconf

提交回复
热议问题