Click here to Skip to main content
12,952,273 members (43,635 online)
Rate this:
 
Please Sign up or sign in to vote.
See more:
What is a macro? Why it is used in C?

EDIT: removed shouting
Posted 3-Oct-12 9:54am
Updated 3-Oct-12 10:32am
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Don't shout (don't use only capital letters)! It is rude!
Read this: http://en.wikipedia.org/wiki/C_preprocessor[^]
  Permalink  
Comments
ridoy 4-Oct-12 0:25am
   
+5
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

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:
#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:

//...
int buf[100];
//...
if (k>100)
{
  //...
}
  Permalink  
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)

    Print Answers RSS
Top Experts
Last 24hrsThis month
OriginalGriff 6,239
CHill60 3,490
Maciej Los 3,083
Jochen Arndt 1,975
ppolymorphe 1,880


Advertise | Privacy | Mobile
Web01 | 2.8.170525.1 | Last Updated 3 Oct 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100