用C++写个程序,如何判断一个操作系统是16位还是32位的?不能用sizeof()函数?(不用sizeof()函数求当前主机上的一个int占用几个字节)

方法一:

16位的系统下,

int i = 65536;

cout << i; // 输出0;//装不下,最高位溢出,剩下16位的当然是0;

int i = 65535;

cout << i; // 输出-1;//-1的补码是65535

而32位的系统下:

int i = 65536;

cout << i; // 输出65536;

int i = 65535;

cout << i; // 输出65535;

方法2:

int a = ~0;//按位取反运算,结果为(11111111111111111111111111111111)

if( a>65536 )

{

cout<<"32 bit"<<endl;

}

else

{

cout<<"16 bit"<<endl;

}

不用sizeof,求int占用的字节数

Cpp代码 
  1. #include <iostream>  
  2.   
  3.    
  4.   
  5. using namespace std;  
  6.   
  7.    
  8.   
  9. #define my_sizeof(L_Value)  (char* )(&L_Value + 1) - (char* )&L_Value  
  10.   
  11.    
  12.   
  13. int main()  
  14.   
  15.    
  16.   
  17. {  
  18.   
  19.     int i;  
  20.   
  21.     double f;  
  22.   
  23.     double a[4];  
  24.   
  25.     double* q;  
  26.   
  27.     cout<<my_sizeof(i)<<endl;//4  
  28.   
  29.     cout<<my_sizeof(f)<<endl;//8  
  30.   
  31.     cout<<my_sizeof(a)<<endl;//32  
  32.   
  33.     cout<<my_sizeof(q)<<endl;//4  
  34.   
  35.     cout<<my_sizeof("aadf")<<endl;//5  
  36.   
  37.     system("pause");  
  38.   
  39.     return 0;  
  40.   
  41. }  


(char* )&L_Value返回L_Value的地址的第一个字节,(char* )(&L_Value+1)返回L_Value的地址的下一个地址的第一个字节,所以他们之差为它所占字节数。

或者用如下方式

Cpp代码 
  1. #include <iostream.h>  
  2.   
  3. template <class Any>  
  4.   
  5. int LengofArray(Any* p)  
  6.   
  7. {  
  8.   
  9.     return int(p+1) - int(p);  
  10.   
  11. }  
  12.   
  13.    
  14.   
  15. int main()  
  16.   
  17. {  
  18.   
  19.     int* i;  
  20.   
  21.     double* q;  
  22.   
  23.     char a[10];  
  24.   
  25.     cout<<LengofArray(i)<<endl;  
  26.   
  27.     cout<<LengofArray(q)<<endl;  
  28.   
  29.     cout<<LengofArray(&a)<<endl;  
  30.   
  31.     return 0;  
  32.   
  33. }  
  34.    
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
### 回答1: 我可以为您编一段 C 语言代码,用于判断系统版本:#include <stdio.h> #include <stdlib.h>int main() { printf("请输入系统版本:"); int version; scanf("%d", &version); if(version == 1) { printf("您的系统版本是1.0\n"); } else if(version == 2) { printf("您的系统版本是2.0\n"); } else { printf("您的系统版本不受支持\n"); } return 0; } ### 回答2: 要一个判断系统版本的程序,可以使用C语言编。下面是一个简单的实现示例: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #ifdef _WIN32 #include <windows.h> #elif __linux__ #include <sys/utsname.h> #endif void getSystemVersion(char* version) { #ifdef _WIN32 OSVERSIONINFO osvi; ZeroMemory(&osvi, sizeof(OSVERSIONINFO)); osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); GetVersionEx(&osvi); sprintf(version, "Windows %d.%d", osvi.dwMajorVersion, osvi.dwMinorVersion); #elif __linux__ struct utsname buf; uname(&buf); sprintf(version, "Linux %s", buf.release); #endif } int main() { char version[50]; getSystemVersion(version); printf("系统版本:%s\n", version); return 0; } ``` 这个程序包含了对`_WIN32`和`__linux__`的预处理指令,分别用于判断编译环境是否为Windows和Linux。在不同的操作系统下,使用不同的方法获取系统版本信息。 对于Windows系统,通过调用`GetVersionEx`函数来获取操作系统的版本信息,并使用`dwMajorVersion`和`dwMinorVersion`来提取主版本号和次版本号。 对于Linux系统,使用`struct utsname`结构体和`uname`函数来获取系统的版本信息,并提取`release`字段作为版本号。 最后,在`main`函数中调用`getSystemVersion`函数获取系统版本信息,并通过`printf`函数输出到控制台。 请注意,这只是一个简单的示例,可能无法覆盖所有的操作系统版本信息。在实际开发中,可能需要更复杂的逻辑来判断系统版本。 ### 回答3: 可以使用C语言编一个判断系统版本的程序。在Windows操作系统中,可以使用`GetVersionEx`函数来获取系统版本信息。以下是一个示例代码: ```c #include <stdio.h> #include <Windows.h> int main() { OSVERSIONINFOEX osvi; ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX)); osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX); if (GetVersionEx((OSVERSIONINFO*)&osvi)) { printf("系统版本:"); if (osvi.dwMajorVersion == 10 && osvi.dwMinorVersion == 0) { printf("Windows 10\n"); } else if (osvi.dwMajorVersion == 6 && osvi.dwMinorVersion == 3) { printf("Windows 8.1\n"); } else if (osvi.dwMajorVersion == 6 && osvi.dwMinorVersion == 2) { printf("Windows 8\n"); } else if (osvi.dwMajorVersion == 6 && osvi.dwMinorVersion == 1) { printf("Windows 7\n"); } else if (osvi.dwMajorVersion == 6 && osvi.dwMinorVersion == 0) { printf("Windows Vista\n"); } else if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 2) { if (osvi.wProductType == VER_NT_WORKSTATION && osvi.wSuiteMask == VER_SUITE_PERSONAL) { printf("Windows XP Home Edition\n"); } else { printf("Windows Server 2003\n"); } } else if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 1) { printf("Windows XP\n"); } else if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 0) { printf("Windows 2000\n"); } else { printf("未知版本\n"); } } else { printf("获取系统版本信息失败\n"); } return 0; } ``` 该程序中使用`GetVersionEx`函数获取系统版本信息,并根据不同的版本号进行判断和输出相应的系统版本名称。值得注意的是,`GetVersionEx`函数在Windows 10及以后的版本中已被标记为不推荐使用,可能无法获取准确的数据,建议在使用时做进一步的兼容性处理。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值