1 / 15

The C Preprocessor

Learn about the C Preprocessor, its functions, and how it processes C source files before compilation. Explore file inclusion, macros, conditional compilation, and the appropriate use of macros.

edgardoj
Download Presentation

The C Preprocessor

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. The C Preprocessor Yongjoon Lee April 22, 2005

  2. What is it? • Things with # • #include <stdio.h> • Processes the C source files BEFORE handing it to compiler. • `Pre`-process • gcc –E • cpp

  3. Why care? • File Inclusion • Basic Macros • Parameterized Macros • Conditional Compilation • Style and (mis)use of Macros

  4. File Inclusion • Recall : #include <filename> • #include <foo.h> • System directories • #include “foo.h” • Current directories • gcc -I

  5. Macros • We can #define constants #define HALF_PI 1.5708 #define BAD_PI HALF_PI+HALF_PI • Solution? ((((((((((The parentheses!)))))))))) #define PI ((HALF_PI)+(HALF_PI))

  6. Parameterized Macros • Macros with param : #define BAAAD_DOUBLE(x) (x*2) #define BAD_DOUBLE(x) ((x)+(x)) #define OKAY_DOUBLE(x) ((x)*2) • WHAT TO TAKE HOME: • Don’t use 2+ parameters. • No assignments within macro

  7. Parameterized Macros cont’d • Want to wrap malloc() calls. • Can we? • YES! #define NEW_T(t,sz) ((t*) malloc((sz)*sizeof(t))

  8. Appropriate use of Macros • Do • Use parentheses • Handle header file inclusion • Capitalize macro names • Don’t • Use more than 2 parameters. • Replace function calls with macros for ‘performance’ • Make the code harder to read#define LOOP(s) while(1) {s};

  9. Conditional Inclusion • #if • #ifdef • #ifndef • #elif • #else • #endif

  10. Conditional Compilation • Debugging #define DEBUG_MODE #ifdef DEBUG_MODE /* … Debug code … */ #endif • OS/Platform #ifdef _WIN32 #include <windows.h> #elif /* …… */

  11. File Header • File header • #ifndef FOO_H #define FOO_H /* … the whole file … * #endif • Why??? • Global variables • Cycles of includes • Inefficient

  12. The Printf Yongjoon Lee April 22, 2005

  13. Basics • printf(“format string”,vars); • Format string? • “This year is %d\n” • “Your score is %d\n” • Conversion by % • %d : int • %f : float, double • %c : char • %s : char *, string • %e : float, double in scientific form

  14. Examples %d conversion • Padding ( width ) %12d %012d • Precision %12.4d %12.6d • Left/right justification %12d %-12d • Similar in %f, %e, … conversions

  15. Example %c, %s conversion • %c has only precision 1 • Precision in %s means string length starting from the beginning • Padding and justification are same

More Related