Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
See more:
What is a macro? Why it is used in C?

EDIT: removed shouting
Posted
Updated 3-Oct-12 10:32am
v2

Don't shout (don't use only capital letters)! It is rude!
Read this: http://en.wikipedia.org/wiki/C_preprocessor[^]
 
Share this answer
 
Comments
ridoy 4-Oct-12 0:25am    
+5
A macro is something the C compiler not even sees.

According to the first entry in a quick-Google-search (Macros - The C Preprocessor[^]):

A macro is a fragment of code which has been given a name. Whenever the name is used, it is replaced by the contents of the macro.


That is:
  • macros are C preprocessor stuff.
  • Using Google you may find a lot of documentation about.


In any case here you are a simple (silly, there are spectacular - and cryptic - usages of macros) example:
C
#define BUFSIZE  100
//...
int buf[BUFSIZE];
//...
if (k>BUFSIZE)
{
  //...
}


the preprocessor replaces every occurrence of the macro BUFSIZE with the number 100, thus the C compiler parses (something like) the following source:

C
//...
int buf[100];
//...
if (k>100)
{
  //...
}
 
Share this answer
 
Comments
ridoy 4-Oct-12 0:25am    
+5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900