Jan
5

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.

 

JIGAR PATEL

hey I am an Artist who love to write code…! Well I am an EC graduate From Ganpat University and now i am working as Embedded software engineer in one private firm.. find me at here JigAr Patel

More PostsWebsiteTwitterFacebook

You may like to read this also....

Post comment

*

CommentLuv badge
Follow us on Twitter! Follow us on Twitter!

Search in this website

our sponsors

latest comments

Find us on Facebook

Top Authors

35 posts
saurabh
21 posts
10 posts

Find us on stackoverflow

Polls

Tell us who you are

View Results

Loading ... Loading ...

My Bookmarks

Sponsers Link