借助C编程,可以找出程序在其上运行的操作系统。对于我们要构建平台独立程序的情况,此信息非常有用。要找到OS(操作系统),我们检出由编译器定义的宏,例如,具有32位OS的窗口具有“ _WIN32 ”作为宏,因此,如果定义了宏,则我们正在使用的系统是具有32位的Windows。位操作系统。同样,其他操作系统也定义了不同的宏。一些流行的操作系统的宏列表如下:
Sr. No. | Operating System | Macro Present | Notes |
---|---|---|---|
1. | Windows 32-bit + 64-bit | _WIN32 | for all Windows OS |
2. | Windows 64 bit | _WIN64 | Only for 64 bit windows |
3. | Apple | __APPLE__ | For all Apple OS |
4. | Apple | __MACH__ | alternative to above |
5. | iOS embedded | TARGET_OS_EMBEDDED | include TargetConditionals.h |
6. | iOS simulator | TARGET_IPHONE_SIMULATOR | include TargetConditionals.h |
7. | iPhone | TARGET_OS_IPHONE | include TargetConditionals.h |
8. | MacOS | TARGET_OS_MAC | include TargetConditionals.h |
9. | Android | __ANDROID__ | subset of linux |
10. | Unix based OS | __unix__ | – |
11. | Linux | __linux__ | subset of unix |
12. | POSIX based | _POSIX_VERSION | Windows with Cygwin |
13. | Solaris | __sun | – |
14. | HP UX | __hpux | – |
15. | BSD | BSD | all BSD flavors |
16. | DragonFly BSD | __DragonFly__ | – |
17. | FreeBSD | __FreeBSD__ | – |
18. | NetBSD | __NetBSD__ | – |
19. | OpenBSD | __OpenBSD__ | – |
笔记:
It must be noted that the macros are valid for GNU GCC and G++ and may vary for other compilers.
以下是检测我们正在使用哪个操作系统的程序:
C
// C program to detect Operating System
#include
// Driver Code
int main()
{
// Checking for windows OS with
// _WIN32 macro
#ifdef _WIN32
printf("Hey Geek it seems that"
"you are working on a Windows OS.\n");
// Checking for mac OS with
// __APPLE__ macro
#elif __APPLE__
printf("Hey Geek it seems that you"
"are working on a Mac OS.\n");
// Checking for linux OS with
// __linux__ macro
#elif __linux__
printf("Hey Geek it seems that you"
"are working on a Linux OS.\n");
// Checking for iOS embedded OS with
// TARGET_OS_EMBEDDED macro
#elif TARGET_OS_EMBEDDED
printf("Hey Geek it seems that you"
"are working on an iOS embedded OS.\n");
// Checking for iOS simulator OS with
// TARGET_IPHONE_SIMULATOR macro
#elif TARGET_IPHONE_SIMULATOR
printf("Hey Geek it seems that you"
"are working on an iOS simulator OS.\n");
// Checking for iPhone OS with
// TARGET_OS_IPHONE macro
#elif TARGET_OS_IPHONE
printf("Hey Geek it seems that you"
"are working on an iPhone OS.\n");
// Checking for MAC OS with
// TARGET_OS_MAC macro
#elif TARGET_OS_MAC
printf("Hey Geek it seems that you"
"are working on a MAC OS.\n");
// Checking for Android OS with
// __ANDROID__ macro
#elif__ANDROID__
printf("Hey Geek it seems that you"
"are working on an android OS.\n");
// Checking for unix OS with
// __unix__ macro
#elif __unix__
printf("Hey Geek it seems that you"
"are working on a unix OS.\n");
// Checking for POSIX based OS with
// _POSIX_VERSION macro
#elif _POSIX_VERSION
printf("Hey Geek it seems that you"
"are working on a POSIX based OS.\n");
// Checking for Solaris OS with
// __sun macro
#elif __sun
printf("Hey Geek it seems that you"
"are working on a Solaris OS.\n");
// Checking for HP UX OS with
// __hpux macro
#elif __hpux
printf("Hey Geek it seems that you"
"are working on a HP UX OS.\n");
// Checking for BSD OS with
// BSD macro
#elif BSD
printf("Hey Geek it seems that you"
"are working on a Solaris OS.\n");
// Checking for DragonFly BSD OS with
// __DragonFly__ macro
#elif __DragonFly__
printf("Hey Geek it seems that you"
"are working on a DragonFly BSD OS.\n");
// Checking for FreeBSD OS with
// __FreeBSD__ macro
#elif __FreeBSD__
printf("Hey Geek it seems that you"
"are working on a FreeBSD OS.\n");
// Checking for Net BSD OS with
// __NetBSD__ macro
#elif __NetBSD__
printf("Hey Geek it seems that you"
"are working on a Net BSD OS.\n");
// Checking for Open BSD OS with
// __OpenBSD__ macro
#elif __OpenBSD__
printf("Hey Geek it seems that you"
"are working on an Open BSD OS.\n");
// If neither of them is present
// then this is printed...
#else
printf("Sorry, the system are"
"not listed above.\n");
#endif
return 0;
}
输出:
以下是Windows操作系统上上述程序的输出:
同样,可以找到操作系统并为系统设置适当的代码
想要从精选的最佳视频中学习和练习问题,请查看《基础知识到高级C的C基础课程》。