494 | | |
| 494 | === Macros === |
| 495 | Remember to put always the parameters between parentheses, so that maybe the parameter needs to be calculated before to use it, just like: |
| 496 | {{{ |
| 497 | #!C |
| 498 | #define MAX(a,b) ((a) > (b) ? (a) : (b)) |
| 499 | |
| 500 | result = 2 * MAX( x, y & 0xFF ); |
| 501 | }}} |
| 502 | |
| 503 | ==== The # Operator ==== |
| 504 | When used inside a macro, it is replaced by the parameter given to the macro call, so: |
| 505 | {{{ |
| 506 | #!C |
| 507 | #define impr(i) printf( "value " #i " = %d", i) |
| 508 | |
| 509 | int x = 18, y = 2; |
| 510 | impr(x-y); |
| 511 | // result: "value x-y = 16" |
| 512 | }}} |
| 513 | |
| 514 | ==== The ## Operator ==== |
| 515 | When used inside a macro, it is replaced by the parameters given to the macro and removing spaces, so: |
| 516 | {{{ |
| 517 | #!C |
| 518 | #define impr(i) printf( #var " with " #num " = %d" var ## num ) |
| 519 | // var ## num (x and 5) is replaced by x5 |
| 520 | |
| 521 | int x4 = 15, x5 = 16; |
| 522 | impr( x, 5 ); |
| 523 | // result: "x with 5 = 16" |
| 524 | }}} |
| 525 | |
| 526 | |
| 527 | ==== #include, #if, #else, #elif, #endif ==== |
| 528 | {{{ |
| 529 | #!C |
| 530 | #if VERSION == 1 |
| 531 | #define MY_PROJ "version1.h" |
| 532 | #else |
| 533 | #define MY_PROJ "version2.h" |
| 534 | #endif |
| 535 | |
| 536 | #include MY_PROJ |
| 537 | }}} |
| 538 | |
| 539 | |
| 540 | ==== #ifdef, #ifndef, #undef ==== |
| 541 | {{{ |
| 542 | #!C |
| 543 | #define MAX(a,b) ((a) > (b) ? (a) : (b)) |
| 544 | |
| 545 | #ifdef MAX |
| 546 | #undef MAX |
| 547 | #endif |