# macro and preprocessor in C

#### @vajra

Joined May 2, 2018
144
I am having trouble to understand difference between macro and preprocessor directives in C,

What is the difference between them? seems like they are more or less the same? I tried to look up on the internet, but still can not understand it, can anyone help?

#### Papabravo

Joined Feb 24, 2006
12,499
The macro, or text substitution capability, is confined to the operation of #define. All of the other preprocessor directives and #define operate in a processing step that happens BEFORE the compiler sees the input text. You start with your input text, it goes through the preprocessor, and from there it goes to pass 1 of the compiler. From there it depends on the compiler how many additional passes are performed.

#### BobTPH

Joined Jun 5, 2013
2,047
Macro directives are a subset of the more general preprocessor directives.

Bob

#### @vajra

Joined May 2, 2018
144
The preprocessor runs before the compiler.

I found the page https://en.wikiversity.org/wiki/C/Preprocessors Example in page is incomplete

#if - #else - #endif

Code:
#define MAX_VALUE 10

#if (MAX_VALUE == 10)
printf("Correct max value");
#else
printf("Incorrect max value");
#endif
sample.c
Code:
#include<stdio.h>
int main (void)
{

return 0;
}
How to use #if - #else - #endif in main program ?

#### MrSoftware

Joined Oct 29, 2013
1,576
#if/else/endif are handled by the preprocessor, they never make it to the compiled code. There are many situations when you might use it, for example when writing multi-platform code:

C:
// Pick one!
#define WINDOWS
// #define LINUX

#ifdef WINDOWS
typedef HANDLE MyFileHandleType;
#elseif LINUX
typedef FILE* MyFileHandleType;
#else
#error unknown platform
#endif

....

main()
{
MyFileHandleType fileHandle;
#ifdef WINDOWS
fileHandle = CreateFileW(...);
#elseif LINUX
fileHandle = fopen(....);
#endif

}
The pre-processor would process this and the code that actually makes it to the compiler would be:

Code:
typedef HANDLE MyFileHandleType;

main()

{
MyFileHandleType fileHandle;
fileHandle = CreateFileW(...);
}
You can do a ton more with pre-processor features, this is a just simple example. Your compiler likely has a setting you can turn on to make it output the code after the pre-processor has gone over it, so you can check the result.