Skip to content

Instantly share code, notes, and snippets.

@tinesubic
Created January 6, 2017 11:49
Show Gist options
  • Save tinesubic/2d8817f4b83ac544c0f942ba6a6c569e to your computer and use it in GitHub Desktop.
Save tinesubic/2d8817f4b83ac544c0f942ba6a6c569e to your computer and use it in GitHub Desktop.
Preprocessor examples
#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
#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"
#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));
#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)
#include <stdio.h>
__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