ArticleslgStudy

science

C preprocessor

C preprocessor is a science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand C preprocessor rather than just read about it. In short: The C preprocessor (CPP) is a text file processor that is used with C, C++ and other programming tools. The preprocessor provides for file inclusion (often header files), macro expansion, conditional compilation, and line control.

Key takeaways

  • C preprocessor belongs to science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect C preprocessor to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of C preprocessor from memory before moving on to harder problems.

Reference excerpt

The C preprocessor (CPP) is a text file processor that is used with C, C++ and other programming tools. The preprocessor provides for file inclusion (often header files), macro expansion, conditional compilation, and line control. Although named in association with C and used with C, the preprocessor capabilities are not inherently tied to the C language. It can be and is used to process other kinds of files.

Languages C, C++, and Objective-C compilers provide a preprocessor capability, as it is required by the definition of each language. Some compilers provide extensions and deviations from the target language standard. Some provide options to control standards compliance. For instance, the GNU C preprocessor can be made more standards compliant by supplying certain command-line flags. The C# programming language also allows for directives, though they are not read by a preprocessor and they cannot be used for creating macros, and are generally more intended for features such as conditional compilation. C# seldom requires the use of the directives, for example code inclusion does not require a preprocessor at all (as C# relies on a package/namespace system like Java, no code needs to be "included"). Similarly, F# and Visual J# are able to call these C# preprocessor directives. The Haskell programming language also allows the usage of the C preprocessor. Features of the preprocessor are encoded in source code as directives that start with #. Although C++ source files are often named with a .cpp extension, that is an abbreviation for "C plus plus"; not C preprocessor.

Preprocessor directives The following languages have the following accepted directives.

C/C++ The following tokens are recognised by the preprocessor in the context of preprocessor directives.

Until C++26, the C++ keywords import, export, and module were partially handled by the preprocessor as well. The Haskell programming language also accepts C preprocessor directives, which is invoked by writing {-# LANGUAGE CPP #-} at the top of the file. The accepted preprocessor directives align with those in standard C/C++.

C# Although C#, F# and Visual J# do not have a separate preprocessor, these directives are processed as if there were one.

C# does not use a preprocessor to handle these directives, and thus they are not handled or removed by a preprocessor, but rather directly read by the C# compiler as a feature of the language.

Objective-C The following tokens are recognised by the preprocessor in the context of preprocessor directives.

Swift The following tokens, called "compiler directives", are detected by the compiler. They are not entirely the same as C preprocessor directives, but have the same structure and syntax.

History The preprocessor was introduced to C around 1973 at the urging of Alan Snyder and also in recognition of the usefulness of the file inclusion mechanisms available in BCPL and PL/I. The first version offered file inclusion via #include and parameterless string replacement macros via #define. It was extended shortly after, firstly by Mike Lesk and then by John Reiser, to add arguments to macros and to support conditional compilation. The C preprocessor was part of a long macro-language tradition at Bell Labs, which was started by Douglas Eastwood and Douglas McIlroy in 1959.

Phases Preprocessing is defined by the first four (of eight) phases of translation specified in the C Standard.

Trigraph replacement: The preprocessor replaces trigraph sequences with the characters they represent. This phase was removed in C23 following the steps of C++17. Line splicing: Physical source lines that are continued with escaped newline sequences are spliced to form logical lines. Tokenization: The preprocessor breaks the result into preprocessing tokens and whitespace. It replaces comments with whitespace. Macro expansion and directive handling: Preprocessing directive lines, including file inclusion and conditional compilation, are executed. The preprocessor simultaneously expands macros and, since the 1999 version of the C standard, handles _Pragma operators.

Features

File inclusion There are two directives in the C preprocessor for including contents of files:

#include, used for directly including the contents of a file in-place (typically containing code of some kind) #embed, used for directly including or embedding the contents of a binary resource in-place

Code inclusion To include the content of one file into another, the preprocessor replaces a line that starts with #include with the content of the file specified after the directive. The inclusion may be logical in the sense that the resulting content may not be stored on disk and certainly is not overwritten to the source file. The file being included need not contain any sort of code, as this directive will copy the contents of whatever file is included in-place, but the most typical use of #include is to include a header file (or in some rarer cases, a source file). In the following example code, the preprocessor replaces the line #include <stdio.h> with the content of the standard library header file named 'stdio.h' in which the function printf() and other symbols are declared.

In this case, the file name is enclosed in angle brackets to denote that it is a system file. For a file in the codebase being built, double-quotes are used instead. The preprocessor may use a different search algorithm to find the file based on this distinction. For C, a header file is usually named with a .h extension. In C++, the convention for file extension varies with common extensions .h and .hpp. But the preprocessor includes a file regardless of the extension. In fact, sometimes code includes .c or .cpp files. To prevent including the same file multiple times, which often leads to a compiler error, a header file typically contains an #include guard or if supported by the preprocessor #pragma once to prevent multiple inclusion.

… excerpt ends here. Continue reading the full article.

Worked examples

Example 1 — a first encounter with C preprocessor

Start with the simplest possible case. Write down what C preprocessor claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to C preprocessor before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about C preprocessor ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of C preprocessor

In research
C preprocessor appears in science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses C preprocessor in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
C preprocessor is common in secondary-school and first-year university syllabi. It links to neighbouring topics C (programming language), Macro programming languages, Transformation languages, so understanding it makes those chapters shorter.
In everyday life
Look for C preprocessor outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “C preprocessor” →

How to study C preprocessor in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what C preprocessor means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain C preprocessor out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is C preprocessor in simple terms?

The C preprocessor (CPP) is a text file processor that is used with C, C++ and other programming tools. The preprocessor provides for file inclusion (often header files), macro expansion, conditional compilation, and line control.

Why does C preprocessor matter?

Because it connects several science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study C preprocessor?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on C preprocessor.

Tags

  • C (programming language)
  • Macro programming languages
  • Transformation languages

Keep exploring