C Tutorial/Preprocessor/MACRO

Материал из C\C++ эксперт
Перейти к: навигация, поиск

MACRO

Replacement of the identifier by using a statement or expression.


<source lang="cpp">#define CUBE(x) x*x*x

  1. include <stdio.h>

main (){

   int k = 5;
   int j = 0;
   j = CUBE(k);
   printf ("value of j is %d\n", j);

}</source>

value of j is 125

MACRO AND FUNCTION

Macro just indicates replacement, not the function call.


<source lang="cpp">#include <stdio.h>

  1. define add(x1, y1) x1 + y1
  2. define mult(x1,y1) x1 * y1

main () {

   int a,b,c,d,e;
   a = 2;
   b = 3;
   c = 4;
   d = 5;
   e = mult(add(a, b), add(c, d));
   // mult(a+b, c+d)
   // a+b * c+d
   printf ("The value of e is %d\n", e);

}</source>

The value of e is 19