C: Lesson 18

ArtyArty
3 min read

C Preprocessor

  • Includes other files being compiled

  • Defines symbolic constants and macros

  • Processes conditional compilation and conditional execution of preprocessor directives.

  • Preprocessor directives begin with # and only white-space characters and comments may appear before a preprocessor directive on a line.

#include Preprocessor Directive

  • The #include directive causes a copy of a specified file to be included in place of the directive.

  • The two forms of the #include directive are

    • #include <filename"> Finds standard library implementation

    • #include “filename“ Finds user defined implementation - starts searches in the same directory and may search in other locations too

#define Preprocessor Directive for Constants & Macros

  • The #define directive creates symbolic constants.

    • format: #define identifier replacement-text
  • Constants are represented as symbols

    • eg: #define PI 3.14159
  • Macros are operations defined as symbols.

    • eg #define CIRCLE_AREA(x) ( (PI) * (x) * (x) )
  • The replacement text for a macro or symbolic constant is normally any text on the line after the identifier in the #define directive.

  • We can use backlash (\) at the end of the line if the macro needs to continue on the next line

  • Replacements would look something like the following

    • Before processed: area = CIRCLE_AREA(4)

    • After processed: area = ((3.14159) * (4) * (4))

Conditional Compilation

  • Controls the execution of preprocessor directives.

  • Each of the conditional preprocessor directives evaluates a constant integer expression

  • Cast expressions, sizeof expressions and enumeration constants cannot be evaluated in preprocessor directives.

  • Common use cases:

    • To comment out portions of the code we want to have in development purposes only

    • To make use of debuggers

# Operator

  • The # operator causes a replacement text token to be converted to a string surrounded by quotes.

  • The # operator must be used in a macro with arguments because the

    operand of # refers to an argument of the macro.

    • eg: #define HELLO(name) printf("hello " #name "\n")

## Operator

  • The ## operator concatenates two tokens.

    • eg. #define TOKEN_CONCAT(A, B) A ## b

Line Numbers

  • line_number: Sets the current line number to this value.

  • "filename" (optional): Sets the current file name that the compiler uses in messages (e.g., for warnings and errors).

Predefined Symbolic Constants

SymbolDescription
__FILE__The current filename as a string literal.
__LINE__The current line number in the source file (as an integer).
__DATE__The current compilation date as a string literal (e.g., "Apr 19 2025").
__TIME__The current compilation time as a string literal (e.g., "14:32:01").
__STDC__Defined as 1 when the compiler conforms to the ANSI C standard.
__STDC_VERSION__The version of the C standard supported (e.g., 199901L for C99).
__STDC_HOSTED__Defined as 1 if the implementation is a hosted environment.

Assertions

  • The assert macro is defined in the <assert.h> header

  • Tests the value of an expression.

0
Subscribe to my newsletter

Read articles from Arty directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Arty
Arty