C/Macro Preprocessor/Macro Function

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

Define Macro as simple function

<source lang="cpp">

  1. include <stdio.h>
  2. define SUM(i, j) ((i)+(j))

int main(void) {

 int sum;
 sum = SUM(10, 20);
 printf("%d", sum);
 return 0;

}


      </source>


Define small function in macro

<source lang="cpp">

  1. define CUBE(x) x*x*x
  2. include <stdio.h>

main () {

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

}


      </source>


Defining a macro to compare two values

<source lang="cpp"> /* The macro function can work with numerical values of any type. The values can be variables, constants or expressions.

  • /
  1. include <stdio.h>
  2. define COMPARE(x,y) ((x)<(y)?-1:((x)==(y)?0:1))

int main() {

 double a = 1.5;
 int n = 2;
 float z = 1.1f;
 printf("\n compare %lf (double) with %f (float): %d", a , z, COMPARE(a,z));
 printf("\n compare %lf (double) with %d (int): %d", a , n, COMPARE(a,n));
 printf("\n compare %d (int) with %f (float): %d", n , z, COMPARE(n,z));
 printf("\n compare \" n * a\" = %lf \n"
   " with \"n+1\" = to %d (double): %d",
    n*a , n+1, COMPARE(n*a,n+1));
 printf("\n");

}

      </source>


Defining a macro to output the value of an expression

<source lang="cpp">

  1. include <stdio.h>
  2. define print_value(expr) printf("\n" #expr " = %lf", (double)expr);

void main() {

 int n = 10;
 double f = 6.5;
 print_value( n );
 print_value( f );
 printf("\n");
 print_value(f * f + 4.0);
 print_value( n / 2 + 3);
 print_value( 3 * 4 + 12 / 2 - 8);
 printf("\n");

}


      </source>


Generate a square table

<source lang="cpp">

  1. include <stdio.h>
  2. define SQR(x) ((x) * (x))

int main() {

   int counter = 0;
   while (counter < 5)
       printf("x %d square %d\n", counter, SQR(++counter));
   return (0);

}


      </source>


Make string

<source lang="cpp">

  1. include <stdio.h>
  2. define mkstr(s) # s

int main(void) {

 printf(mkstr(I like C));
 return 0;

}


      </source>


Use Macro to check the int range

<source lang="cpp">

  1. include <stdio.h>
  2. include <conio.h>
  3. include <stdlib.h>
  4. define RANGE(i, min, max) (i < min) || (i > max) ? 1 : 0

int main(void) {

 int r;
 /* random numbers between 1 and 100 */
 do {
 
   do {
 
     r = rand();
 
   } while(RANGE(r, 1, 100));
 
   printf("%d ", r);
 
 } while(!kbhit());
 return 0;

}


      </source>


Use macro to find the minor variable

<source lang="cpp">

  1. include <stdio.h>
  2. define min(a, b) ((a<b) ? a : b )

int main() {

  int a,b,c;
  a = 1;
  b = 2;
  c = min ( a, b); 
  printf ( "a: %d, b: %d, c: %d\n", a, b, c );

}


      </source>


Use Macro to get the bigger int value

<source lang="cpp">

  1. include <stdio.h>
  2. define MAX(i, j) i>j ? i : j

int main(void) {

 printf("%d\n", MAX(3, 2));
 printf("%d\n", MAX(1, -1));
 /* this statement does not work correctly */
 printf("%d\n", MAX(100 && -1, 0));
 return 0;

}


      </source>


Use marco for small function

<source lang="cpp">

  1. include <stdio.h>
  2. define ABS(a) (a) < 0 ? -(a) : (a)

int main(void) {

 printf("abs of -1 and 1: %d %d", ABS(-1), ABS(1));
 return 0;

}

      </source>