The C Preprocessor: cpp - PowerPoint PPT Presentation

1 / 11
About This Presentation
Title:

The C Preprocessor: cpp

Description:

C provides a way to specify a define as a flag to gcc. If you plan on changing your define regularly, this method keeps you from having ... – PowerPoint PPT presentation

Number of Views:155
Avg rating:3.0/5.0
Slides: 12
Provided by: alanb9
Category:
Tags: cpp | preprocessor

less

Transcript and Presenter's Notes

Title: The C Preprocessor: cpp


1
The C Preprocessor cpp
  • Chapter 13

2
Whats CPP?
  • Any source code line beginning with a are
    preprocessor directives
  • The preprocessor runs before the compiler and
    follows the directives that are placed in the
    source, then passes the modified source on to the
    compiler.

3
Types of Directives
  • include brings in the file specified and
    appends it to the source code.
  • define
  • Named constants identifier that is to be
    replaced with some other text.
  • Macros More like a function, they actually
    allow you to use parameters. Work very well for
    very small functions. They have much less
    overhead than functions since theres no function
    call or return, just a simple text replacement.
    Macros are also type independent.
  • Both named constants and macros should always
    have their names in all caps. It makes it much
    easier to see in your source what is and what
    isnt a preprocessor directive.

4
Includes
  • System header files
  • include ltstdio.hgt
  • User defined header files
  • include file.h
  • path is relative from the directory your source
    code is in.
  • What should be put in header files?
  • defined macros constants
  • function declarations (prototypes)
  • inline function definitions
  • type names defined with typedef
  • includes with extreme caution
  • When putting includes in header files, you should
    be careful not to have multiple header files
    including the same files (ie stdio.h). This
    would cause multiple copies of the included
    file(s) to be appended to your source code.
  • Information that is only needed in one source
    file should not be put in separate header files.

5
Named Constants
  • Some times you just need to use a constant value
    throughout your program.
  • define MAX_SIZE 10
  • Every occurrence of MAX_SIZE in your source will
    be replaced with the number 10.
  • You can also use this to replace other kinds of
    text
  • define PROMPT Please enter an integer
  • define BEGIN define END

6
An Alternative to define
  • C provides a way to specify a define as a flag to
    gcc. If you plan on changing your define
    regularly, this method keeps you from having to
    edit the source code every time. This comes in
    very handy for defining the platform youre
    running on or to turn on debugging.
  • gcc Wall DPLATFORMFreeBSD sort.c
  • define PLATFORM FreeBSD
  • gcc Wall DDEBUG1 sort.c
  • define DEBUG 1

7
Macros
  • The following is a macro to compute absolute
    value of a number from the book.
  • define ABS(n) ( (n) lt 0 ? (n) (n) )
  • Lets assume we call dist ABS(x1-x2) in our
    code. The preprocessor expands this text to
  • dist ( (x1-x2) lt 0 ? (x1-x2) (x1-x2) )
  • Note that the parentheses are VERY important in
    the macro definition. Why?
  • Macros are very hard to debug because you
    generally dont see the output of the
    preprocessor before it gets to the compiler. You
    can use gcc E to stop after the preprocessor and
    put the output to the screen.

8
Conditional Compilation
  • One of the most useful reasons to use cpp is for
    conditional compilation. Lets say we want our
    program to be able to run on multiple platforms,
    but each platform requires some changes to your
    source code to be able to run properly. You
    could create a different version of your source
    for each platform that you want to run on, or you
    can use conditional compilation and let cpp
    change your source based on what platform youre
    running on.

9
Conditional Compilation
  • In a previous slide, we defined a named constant
    called PLATFORM so lets use it to change our
    source code.
  • if PLATFORMFreeBSD statements specific to
    FreeBSDendifif PLATFORMRedHat statements
    specific to RedHat Linuxendif
  • The preprocessor if directive recognizes all of
    the standard C comparison operators and the
    logical operators like !, ,
  • There is a else and elif directive as well

10
More Conditional Compilation
  • ifdef
  • Allows you to change your source code based on
    whether a named constant has been defined or not.
  • ifdef DEBUG Debugging output statementsendif
    If DEBUG is defined regardless of what value it
    has, the output statements will be added to your
    source code.
  • if defined(DEBUG) Debugging output
    statementsendifHas the same effect as the
    ifdef example above.
  • ifndef
  • The same as ifdef except will execute when the
    constant is NOT defined.

11
Predefined Macros
  • _ _DATE_ _
  • The current date
  • _ _FILE_ _
  • The source filename
  • _ _LINE_ _
  • The line number in the source file
  • _ _STDC_ _
  • 1 for ANSI compilers, 0 for non-ANSI
  • _ _TIME_ _
  • The current time
Write a Comment
User Comments (0)
About PowerShow.com