---
title: "How do I check OS with a preprocessor directive?"  
description: "How do I check OS with a preprocessor directive?"  
author: "Revati S Misra"  
published: 2023-07-24  
updated: 2023-07-25  
canonical: https://www.mindstick.com/forum/159254/how-do-i-check-os-with-a-preprocessor-directive  
category: "Operating System"  
tags: ["operating system", "preprocessor"]  
reading_time: 2 minutes  

---

# How do I check OS with a preprocessor directive?

How do I [check](https://yourviews.mindstick.com/story/2248/never-forget-to-check-these-specifications-before-buying-a-mobile-phone) OS with a [preprocessor directive](https://answers.mindstick.com/qa/104733/define-preprocessor-directive)?

## Replies

### Reply by Aryan Kumar

Sure, I can help you with that.

Here is how you can check the operating system in C using preprocessor directives:

C

```plaintext
#include <stdio.h>

#ifdef _WIN32
#define OS "Windows"
#elif __linux__
#define OS "Linux"
#elif __APPLE__
#define OS "Mac OS X"
#else
#define OS "Unknown"
#endif

int main() {
  printf("The operating system is %s\n", OS);
  return 0;
}
```

This code first includes the `<stdio.h>` header file, which provides the `printf()` function. Then, it defines a preprocessor macro called `OS`. The value of this macro will be different depending on the operating system that the code is running on.

For example, if the code is running on Windows, the `OS` macro will be defined as `"Windows"`. If the code is running on Linux, the `OS` macro will be defined as `"Linux"`. And so on.

The `#ifdef` preprocessor directive is used to check if a macro is defined. If the macro is defined, the code between the `#ifdef` and `#endif` directives will be compiled. Otherwise, the code will be skipped.

In this case, the `#ifdef` directive is used to check if the `_WIN32` macro is defined. If it is, the code will compile and print the string "Windows" to the console. If it is not, the code will compile and print the string "Unknown" to the console.

You can also use the `#elif` and `#else` preprocessor directives to check for other operating systems. For example, the following code will print the string "Linux" if the code is running on Linux, and the string "Unknown" if it is running on any other operating system:

C

```plaintext
#include <stdio.h>

#ifdef __linux__
#define OS "Linux"
#else
#define OS "Unknown"
#endif

int main() {
  printf("The operating system is %s\n", OS);
  return 0;
}
```


---

Original Source: https://www.mindstick.com/forum/159254/how-do-i-check-os-with-a-preprocessor-directive

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
