The C pre-processor runs on your C and C++ code at compile time prior to the main compile process. Source lines starting with the # character are processed to roduce a new source code file (internally - you don't normally see this) which is then the input to the compile process "proper".
Preprocessor DIRECTIVES include:
#include
To include the contents of another file at this point. If the other file is embedded in chevrons (< to >), the included file is a standard system one from system libraries, but if it's embedded in double quotes, it's your own include file from your own folder / directory. Include files often have an extension
.h for header, but this doesn't matter to C / C++ - it's just a label
#define
Define a variable within the pre-preocessor. Subseqent refefences to that named variable will be substituted for the value given at compile time, allowing you to fix data at a specific build - such things as array sizes. It's convention to write preprocessor variable name all in CAPITALS to help the maintenance programmer, but it's not a vital rule.
Preprocessor variables can also be set on the compile time command line; in
cc and
gcc and
g++, that's done with the -D switch.
#ifdef
#ifndef
Allow you to check whether a variable has been defined - very useful to see ig your file has been brought in by another file at compile time, or is being compiled on its own. Also useful for conditional code based on compile time selection, and to avoid code being loaded twice where there's a comple #include structure. "ifdef" is "if defined" and "ifndef" is "if NOT defined".
#endif
Signifies the end of a block started by "ifdef" or "ifndef".
Note that directives do NOT end with a semicolon character - they are a different language to C and C++, and they end at the end of the line.
Example -
[here].
(written 2015-10-27)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
C230 - C and C based languages - Revision of C [4466] Moving from C to C++ - Structured to Object Oriented - a lesson for engineers - (2015-03-28)
[4566] C - why is slow to write and debug) but fast to run? - (2015-11-01)
C204 - C and C based languages - Functions, Macros and programs in multiple files [775] Do not duplicate your code - (2006-06-23)
[1163] A better alternative to cutting and pasting code - (2007-04-26)
[1478] Some new C programming examples - files, structs, unions etc - (2007-12-19)
[2570] Function Prototypes in C - (2010-01-11)
[2575] Sharing variables between files of code in C - extern - (2010-01-14)
[2841] C Course exercise and sample answer - source in 2 files - (2010-06-30)
[3237] Using functions to keep look and feel apart from calculations - simple C example - (2011-04-09)
[3717] Returning extra results from a function in C - (2012-05-03)
[3721] Naming blocks of code, structures and Object Orientation - efficient coding in manageable chunks - (2012-05-06)
[4338] Passing arrays into functions in C - (2014-12-02)
[4554] Passing information into functions in C - by name, by value - (2015-10-26)
[4557] Function prototype - what they are and why you should use them - C and C++ - (2015-10-27)
C201 - C and C based languages - C Language Fundamentals [888] Turning C from source to a running program - (2006-10-06)
[1671] Compiling C programs with gcc - an overview - (2008-06-10)
[2005] Variables and pointers and references - C and C++ - (2009-01-23)
[2576] What does const mean? C and C++ - (2010-01-15)
[2842] Staring a C course with Hello World - why? - (2010-06-30)
[3120] Learning to write good programs in C and C++ - separating out repeated code - (2011-01-04)
[3234] Your program - you just provide the filling in the sandwich - (2011-04-08)
[3278] Do I need to initialise variables - programming in C, C++, Perl, PHP, Python, Ruby or Java. - (2011-05-05)
[3591] Integer types, and integer overflows, in C - (2012-01-25)
[3917] BODMAS - the order a computer evaluates arithmetic expressions - (2012-11-09)
[4324] Learning to program - variables and constants - (2014-11-22)
Some other Articles
When do I use the this keyword in C++?Well House Consultants - Python courses / what's special.Strings in C - strncmp strncpy and friendsPreprocessor directives in C and C++ - what they meanRUby - loading, using, changing, storing JSON format dataScenario outlines - tables of values to test - in Gherkin / CucumberTesting your new class - first steps with cucumberBuild up classes into applications sharing data types in Ruby