VOOZH about

URL: https://www.geeksforgeeks.org/operating-systems/how-to-detect-operating-system-through-a-c-program/

⇱ How to detect Operating System through a C program - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to detect Operating System through a C program

Last Updated : 15 Jul, 2025

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.h
7.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: Output: Below is the output of the above program on a Windows OS: 👁 Image
Similarly one can find out the operating system and can
Comment
Article Tags: