macro and preprocessor in C

Thread Starter

@vajra

Joined May 2, 2018
154
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
21,225
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.
 

Thread Starter

@vajra

Joined May 2, 2018
154
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

Header.h file
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>
#include"Header.h"
int main (void)
{  

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

MrSoftware

Joined Oct 29, 2013
2,196
#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.
 

Thread Starter

@vajra

Joined May 2, 2018
154
#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:
I am having trouble to understand difference between Pre-processor Directive and micros

My understanding following are Pre-processor Directive

#include, #if, #ifdef, #ifndef, #else, #elif, #endif, #define, #undef, #line, #error, and #pragma are all preprocessing directives.

I found example of micros

Code:
#include <stdio.h>
 
// macro definition
#define LIMIT 5
int main()
{
    for (int i = 0; i < LIMIT; i++) {
        printf("%d \n",i);
    }
 
    return 0;
}
 
Last edited:

MrSoftware

Joined Oct 29, 2013
2,196
The "directive" is the term that you type, such as #define, #if, etc.. these are the directives.

You use directives to make a macro. For example, here we use the "#define" directive to make the MIN macro:

C++:
#include <iostream>
using namespace std;

#define MIN(a,b) (((a)<(b)) ? a : b)

int main () {
   int i, j;
 
   i = 100;
   j = 30;
 
   cout <<"The minimum is " << MIN(i, j) << endl;

   return 0;
}
 
Top