Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Writing OS Independent Code in C/C++

  • Difficulty Level : Basic
  • Last Updated : 29 May, 2017

How to write a utility program that lists all contents of directory irrespective of Operating System.

Most of the C/C++ compilers, define macros which can be used to detect operating system. For example, in GCC, following are common macros.

_WIN32 : Defined for both 32 bit and 64 bit windows OS.
_WIN64 : Defined for 64 bit windows OS.

unix, __unix, __unix__ : Defined in UNIX OS

__APPLE__, __MACH__ : Defined in Mac OS

Source : StackOverflow

In Windows, we us dir call to list all directories and in most of the other Operating Systems “ls” is used. Below is simple C++ implementation to list directories of folder irrespective of OS.

// C++ program to list all directories.
#include <bits/stdc++.h>
using namespace std;
int main()
#ifdef _WIN32
    return 0;

The above OS independent code is totally different from Java’s platform independence. In Java, there is intermediate byte code that is very clean way of handling platform dependencies. Here we have to remember compiler specific macros and write code in using clumsy #ifdef and #else, and the most important, we need to recompile the code for every OS.

This article is contrubuted by Shubham Agrawal. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!