Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C
What is a macro? Why it is used in C?
 
EDIT: removed shouting
Posted 3-Oct-12 9:54am
Edited 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 at 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 at 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
0 Nirav Prabtani 298
1 OriginalGriff 289
2 Sergey Alexandrovich Kryukov 225
3 _Amy 165
4 Richard MacCutchan 135
0 OriginalGriff 7,799
1 Sergey Alexandrovich Kryukov 6,966
2 Maciej Los 3,994
3 Peter Leow 3,708
4 CHill60 2,742


Advertise | Privacy | Mobile
Web01 | 2.8.140721.1 | Last Updated 3 Oct 2012
Copyright © CodeProject, 1999-2014
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