How to detect Operating System through a C program Last Updated : 19 Jul, 2020 Comments Improve Suggest changes Like Article Like Report One can find out the operating system on which the program is running with the help of C programming. This piece of information is very useful for the case where we want to build platform independent program. To find the OS (Operating System) we check out the macro defined by the compiler, for example windows with 32-bit OS has "_WIN32" as macro so if the macro is defined then the system we are working on is windows with 32-bit operating system. Similarly other OS has different macro defined. The list of macro for some popular OS are as follows: Sr. No.Operating SystemMacro PresentNotes 1.Windows 32-bit + 64-bit_WIN32for all Windows OS 2.Windows 64 bit_WIN64Only for 64 bit windows 3.Apple__APPLE__For all Apple OS 4.Apple__MACH__alternative to above 5.iOS embeddedTARGET_OS_EMBEDDEDinclude TargetConditionals.h 6.iOS simulator TARGET_IPHONE_SIMULATOR include TargetConditionals.h7.iPhoneTARGET_OS_IPHONEinclude TargetConditionals.h 8.MacOSTARGET_OS_MACinclude TargetConditionals.h 9.Android__ANDROID__subset of linux 10.Unix based OS__unix__- 11.Linux__linux__subset of unix 12.POSIX based_POSIX_VERSIONWindows with Cygwin 13.Solaris__sun- 14.HP UX__hpux- 15.BSDBSDall BSD flavors 16.DragonFly BSD__DragonFly__- 17.FreeBSD__FreeBSD__- 18.NetBSD__NetBSD__- 19.OpenBSD__OpenBSD__- Note: It must be noted that the macros are valid for GNU GCC and G++ and may vary for other compilers. Below is the program to detect which OS we are working on: C // C program to detect Operating System #include <stdio.h> // 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; } Output: Below is the output of the above program on a Windows OS: Similarly one can find out the operating system and can set the appropriate code for the system Comment More infoAdvertise with us Next Article How to detect Operating System through a C program A aditya_taparia Follow Improve Article Tags : C Programs Operating Systems C Macro Similar Reads CLI programs in C for playing media and shut down the system Command Line Interface: CLI is a text-based user interface (UI) used to view and manage computer files.Command-line interfaces are also called command-line user interfaces, the console uses interfaces and characters uses interfaces.In programming, the user gives input during the execution of a progr 6 min read C Program to Find the Size of int, float, double and char Write a C program to find the size of the data types: int, float, double, and char in bytes and print it on the output screen.ExamplesInput: charOutput: Size of char: 1 byteInput: intOutput:Size of int: 4 bytesDifferent Methods to Find the Size of int, float, double and char in CWe can find the size 4 min read C Program to Hide a Console Window On Startup Here in this article, we have created a console application that will write into a file. This console application will work in the background by hiding the console window. To ensure that the program or the console application is still running, we can see the live data appended in the text file creat 4 min read C Program to Print the Program Name and All its Arguments The command-line argument (CLA) is the parameter provided in the system upon request. Command-line conflict is an important concept in system C. It is widely used when one needs to control your system from the outside. Command-line arguments are transferred to the main () path. Argc calculates the n 2 min read Program to find out the data type of user input Take a input from user and find out the data type of input value. Examples : Input : geek Output : The input is a string Input : chetna Output : The input is a string Input : 4 Output : The input is a integer Below is C program to find the datatype of user input : C // C program to find data type #i 2 min read Like