Friday, June 10, 2011

Macro Processor

Macro Processor is a program that lets you define the code that is reused many times giving it a specific Macro name and reuse the code by just writing the Macro name only.

Generally it doesn't come as a separate program but as a bundle to either assembler or compiler

Note : Please don't confuse Macro Processor with Micro Processor, remember Micro Processor is a hardware device and it's a completely different area of study

There are three main steps of using a macro

1.Define the macro name
2.Give it's definetion
3.Use the macro name from with in the program anywhere to use it's definetion (this step is called macro call)

And the most important thing is that, macro processing is done before the compilation replacing the code in place of macro calls and deleting macro definetions.If you are on an open source OS, this can easily be tested.For others, this is how it happens.This is my helloworld programme


#include<stdio.h>
#define hell printf("\nHello world\n");printf("I am a good boy\n\n");


int main()
{
hell;
}

Now there is are a lot of options in gcc compiler and if you use gcc helloworld.c -E , it gives you the code that is processed before it is passed to the compiler.It includes a lot of things along with the code.But since our main discussion is about Macro processing, let's see how the macro processed code looks like

# 916 "/usr/include/stdio.h" 3 4

# 2 "helloworld.c" 2


int main()
{
printf("\nHello world\n");printf("I am a good boy\n\n");;
}




You can clearly see that macro processing is already done replacing hell with the corresponding macro definition.This is passed onto the compiler which does the remaining job. This is the compiled output


Hello world
I am a good boy



Note : There are a lot of interesting things gcc command does apart from compiling type gcc --help for more info
Gcc logo

There are a lot more uses for macro and don't confuse functions with macros even though both are used for same purpose and if any one asks you the difference tell them, the things I showed you above, function gets compiled but a macro gets macro processed

One important use of these macros is for booting.During the boot time, the system uses a lot of macros to get itself started since they need not be compiled to use, but just macro processing is enough which is a small task

So, for today, alvida guys, thanks for baring with me, and if you have suggestions pass the comments as you like and hit the +1, button below this post, if you want you can post this one to your friend, since I have enabled posting, see the social buttons for all the things you need.

11 comments:

  1. good blog.. helped me in my exam.. thank you :)

    ReplyDelete
  2. you should elaborate more about this topic

    ReplyDelete
  3. Thank u this is very helpful very wellform explanation this clear my all doubte..keep it up ��

    ReplyDelete
  4. These functions are done in the two passes. Here passes refers to reading the source program once. The function of each pass is given below.
    https://tipsontechnology.com/overview-of-macro-processor/

    ReplyDelete