How can i detect Windows or Linux in C,C++ programming?
Sometimes we need to write platform independent code which works in windows and linux machine without giving any flag or any modification in code or make file manually.
So in such case i usually use compiler generated macro to check the platform .
In Linux all c and c++ compiler define macro like linux
, In Windows all c and c++ compiler define macro like _WIN32
so we can use this macro to separate code for linux and windows see belows example code
#include<stdio.h> int main(int argc, char *argv[]) { #ifdef _WIN32 printf("in Windows"); #endif #ifdef linux printf("In Linux"); #endif return 0; } |
Now compile this code and run On linux it will print In Linux
and on windows it will print in Windows.
You may like to read this also....
Post comment
Search in this website
our sponsors
latest comments
- Shah Dhaval on How to detect memory leakage in C program?
- ruunyytikkil on how can I write applications in C or C++ for Android?
- anonym on How to convert SQL to Excel in Php?
- Academic Projects and Tech interview on C programming interview questions and answers for freshers
- Motasim billah on List of all standard version of c language
Find us on Facebook
Top Authors
Find us on stackoverflow
Polls
Loading ...
My Bookmarks
- Audio/video Recorder & player application based on MATLAB
- check dependency of your binary
- defination of all standard c programming language function
- Great Question-Answer on c programming
- know what your c code means
- Limition of c programming language
- List of all version of c programming language
- Online c compiler
- php freelancing work
- some more stuff on C programming language
- Volatile Keyword in Embedded System
- Write Android application in c language