Hi
How should i identify the operating system on which my program is running in c++.

Kindly help

Recommended Answers

All 3 Replies

The short answer is you can't. The language itself is portable, therefore it works hard to avoid any explicit knowledge of the OS on which it is running.

Take TurboC (an often used, but misplaced example), and your OS is XP. Is your OS
a) DOS
b) The Virtual DOS Machine which XP provides to emulate DOS
c) XP

If you then further complicate it with say running vmware on Linux, which hosts XP as a guest operating system (then all of the above), which OS is your program now running under?

Unless you resort to using OS specific API functions (which pretty much gives the game away), there isn't going to be a standard way which gives you a definitive answer.

I only want to identify whether i m working on a windows based system or a unix based system. Can we do this.

Well since you need to compile it with a windows based compiler in order to run the code on a windows based machine (and likewise for Unix), then the answer is conditional compilation.
http://predef.sourceforge.net/prearch.html

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.