Created
January 6, 2017 11:49
-
-
Save tinesubic/2d8817f4b83ac544c0f942ba6a6c569e to your computer and use it in GitHub Desktop.
Preprocessor examples
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define DEBUG_1 0 | |
//Because DEBUG_1 is set to zero, the else | |
//clause will be removed from code before compilation. | |
#if DEBUG_1 == 0 | |
processNoDebug(); | |
#else | |
processWithDebug() | |
#endif | |
#ifdef VERBOSE | |
printVerboseLog(); | |
#endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define SYS_MODE 16 | |
#ifndef SYS_OS | |
#error "OS variable must be set." | |
#endif | |
#define RUN_MODE 32 | |
#if SYS_MODE != RUN_MODE | |
#warning "System does not match target architecture. May cause compilation errors." | |
#endif | |
#pragma message "User msg" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define VAR_NAME(NUM) var##NUM | |
#define STRINGIFY(ARG) #ARG | |
int VAR_NAME(1) = 8; // int var1 = 8; | |
int VAR_NAME(2) = 16; // int var2 = 16; | |
printf("%s\n", STRINGIFY(x==0)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define A 123 | |
#define B 42 | |
#define min(X, Y) ((X) < (Y) ? (X) : (Y)) | |
#define SWAP(a, b) { \ | |
a ^= b; \ | |
b ^= a; \ | |
a ^= b; \ | |
} | |
"Example A:" A | |
"Example B:" B | |
"min: " min(A,B) | |
"swap: " SWAP(4,5) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
__FILE__ | |
__DATE__ | |
__TIME__ | |
__GNUC__ | |
__LINE__ | |
__GNUC_MINOR__ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment