Tutorial by Examples

To conditionally include a block of code, the preprocessor has several directives (e.g #if, #ifdef, #else, #endif, etc). /* Defines a conditional `printf` macro, which only prints if `DEBUG` * has been defined */ #ifdef DEBUG #define DLOG(x) (printf(x)) #else #define DLOG(x) #endif Norm...
The most common uses of #include preprocessing directives are as in the following: #include <stdio.h> #include "myheader.h" #include replaces the statement with the contents of the file referred to. Angle brackets (<>) refer to header files installed on the system, while q...
The simplest form of macro replacement is to define a manifest constant, as in #define ARRSIZE 100 int array[ARRSIZE]; This defines a function-like macro that multiplies a variable by 10 and stores the new value: #define TIMES10(A) ((A) *= 10) double b = 34; int c = 23; TIMES10(b); //...
If the preprocessor encounters an #error directive, compilation is halted and the diagnostic message included is printed. #define DEBUG #ifdef DEBUG #error "Debug Builds Not Supported" #endif int main(void) { return 0; } Possible output: $ gcc error.c error.c: error: #e...
If there are sections of code that you are considering removing or want to temporarily disable, you can comment it out with a block comment. /* Block comment around whole function to keep it from getting used. * What's even the purpose of this function? int myUnusedFunction(void) { int i =...
Token pasting allows one to glue together two macro arguments. For example, front##back yields frontback. A famous example is Win32's <TCHAR.H> header. In standard C, one can write L"string" to declare a wide character string. However, Windows API allows one to convert between wide c...
A predefined macro is a macro that is already understood by the C pre processor without the program needing to define it. Examples include Mandatory Pre-Defined Macros __FILE__, which gives the file name of the current source file (a string literal), __LINE__ for the current line number (an int...
Pretty much every header file should follow the include guard idiom: my-header-file.h #ifndef MY_HEADER_FILE_H #define MY_HEADER_FILE_H // Code body for header file #endif This ensures that when you #include "my-header-file.h" in multiple places, you don't get duplicate declara...
We can also use macros for making code easier to read and write. For example we can implement macros for implementing the foreach construct in C for some data structures like singly- and doubly-linked lists, queues, etc. Here is a small example. #include <stdio.h> #include <stdlib.h>...
There are times when an include file has to generate different output from the preprocessor depending on whether the compiler is a C compiler or a C++ compiler due to language differences. For example a function or other external is defined in a C source file but is used in a C++ source file. Since...
Function-like macros are similar to inline functions, these are useful in some cases, such as temporary debug log: #ifdef DEBUG # define LOGFILENAME "/tmp/logfile.log" # define LOG(str) do { \ FILE *fp = fopen(LOGFILENAME, "a"); \ ...
C99 Macros with variadic args: Let's say you want to create some print-macro for debugging your code, let's take this macro as an example: #define debug_print(msg) printf("%s:%d %s", __FILE__, __LINE__, msg) Some examples of usage: The function somefunc() returns -1 if failed and 0 ...

Page 1 of 1