In ANSI C: How can I find out the operating system?

How can I find out which operating system is used? - I have the same C code for a Windows and Linux computer and need this information during running the code.

Thanks for some hints.
If you need it to compile different. use #ifdef linux or #ifdef win32
there are predefined macros like linux or win32 to find the OS of the system. you can find out the complete list of macros from http://predef.sourceforge.net/preos.html
let me tell you- how to use them. for example you want to clear screen while you are running your code.
in windows, we use system command
>cls
in linux, we use system command
$clear

1
2
3
4
5
6
7
8
9
#ifdef linux
// linux related code goes here
system("clear");
#endif

#ifdef win32
// windows related code goes here
system("cls");
#endif 
Yes, alas, there is no runtime function in either C or C++ to tell you what OS is running. The only way is, essentially, a priori knowledge from the compile-time environment. Usually this manifests via macros.

See here for useful information:
http://predef.sourceforge.net/preos.html

Good luck!

[edit] BTW, pushpik, your code is not correct. Be careful about giving incorrect solutions...
Last edited on
Thanks for the information.
There is no win32. It's _WIN32.
Topic archived. No new replies allowed.