Skip to main content

CSC 282.01, Class 09: Macros and the C Preprocessor

Overview

  • Preliminaries
    • Notes and news
    • Upcoming work
    • Questions
  • Reminder: The C Preprocessor
  • Including files
  • Constants
  • Command-line options
  • Macros
  • CPP conditionals
  • Detour: Standard header file format

News / Etc.

  • Good morning! Welcome back from break!
  • Thanks for your hard work on the API API!
  • You should be able to find the code we develop in today’s class in the repo called intro-macros-2017S.

Upcoming work

  • TBD

Good things to do

  • BAX opening reception tomorrow!
  • Baseball Saturday, Womens’ Tennis Saturday, …

Questions

Reminder: The C Preprocessor

  • First phase of compilation
  • Compilation is (turn “human”-readable into computer readable)
  • First phase: Textual manipulation of program.
    • Include files
    • Replaces the definiens with the definition (from #DEFINE)
    • Conditionally does stuff
    • Strips comments

Including files

#include "filename" includes local file #include <filename> includes file in the include path

Constants

#define NAME VALUE

Command-line flags

-DNAME=VALUE

Macros

Also defined with #DEFINE. Are kind of like procedures. They take parameters, they have a body.

#define TWICE(X) X+X

vs.

int
twice (int x)
{
  return x+x;
}

Remind: C preprocessor does textual substitution. The input type to TWICE is “program text” and the output type is “program text”.

Why would we use the macro rather than the procedure (in this situation)?
More general. The macro can work with any type that accepts +.

CPP conditionals

Detour: Standard header file format