Skip to content

Instantly share code, notes, and snippets.

@psobko
Created October 28, 2013 17:09
Show Gist options
  • Save psobko/7200704 to your computer and use it in GitHub Desktop.
Save psobko/7200704 to your computer and use it in GitHub Desktop.
Preprocessor Macros
//http://www.tutorialspoint.com/cprogramming/c_preprocessors.htm
#define Substitutes a preprocessor macro
#include Inserts a particular header from another file
#undef Undefines a preprocessor macro
#ifdef Returns true if this macro is defined
#ifndef Returns true if this macro is not defined
#if Tests if a compile time condition is true
#else The alternative for #if
#elif #else an #if in one statement
#endif Ends preprocessor conditional
#error Prints error message on stderr
#pragma Issues special commands to the compiler, using a standardized method
---
#define MAX_ARRAY_LENGTH 20
#undef FILE_SIZE
#define FILE_SIZE 42
#ifndef MESSAGE
#define MESSAGE "You wish!"
#endif
#ifdef DEBUG
/* Your debugging statements here */
#endif
//Continuation (multiple lines)
#define message_for(a, b) \
printf(#a " and " #b ": We love you!\n")
//Parametrized Macros
#define square(x) ((x) * (x))
#define MAX(x,y) ((x) > (y) ? (x) : (y))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment