The C Preprocessor Yongjoon Lee April 22, 2005. What is it? Things with # –#include Processes the C source files BEFORE handing it to compiler. –`Pre`-process.

Slides:



Advertisements
Similar presentations
CS 11 C track: lecture 7 Last week: structs, typedef, linked lists This week: hash tables more on the C preprocessor extern const.
Advertisements

Compilation and Debugging 101. Compilation in C/C++ hello.c Preprocessor Compiler stdio.h tmpXQ.i (C code) hello.o (object file)
Files in C Rohit Khokher.
Real World Applications: Computing Resistance Problem –Given single-character codes for the colored bands that mark a resistor, compute its resistance.
Programming III SPRING 2015 School of Computer and Information Sciences Francisco R. Ortega, Ph.D. McKnight Fellow and GAANN Fellow LECTURE #3 Control.
C Programming Day 1 based upon Practical C Programming by Steve Oualline CS550 Operating Systems.
Chapter 13 & 14 C Preprocessor and Other C Topics Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education.
CSc 352 An Introduction to the C Preprocessor Saumya Debray Dept. of Computer Science The University of Arizona, Tucson
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 17 - The Preprocessor Outline 17.1Introduction 17.2The #include Preprocessor Directive 17.3The.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 13 - The Preprocessor Outline 13.1Introduction.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 13 - The Preprocessor Outline 13.1Introduction 13.2The #include Preprocessor Directive 13.3The.
1 CSE 303 Lecture 13b The C preprocessor reading: Programming in C Ch. 13 slides created by Marty Stepp
The Preprocessor #include #define N 10 C program Preprocessor Modified C program Preprocessor Object codes.
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
 2007 Pearson Education, Inc. All rights reserved C Preprocessor.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 19 - The Preprocessor Outline 19.1 Introduction 19.2 The #include Preprocessor Directive 19.3.
The preprocessor and the compilation process COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ.
Windows Programming Lecture 05. Preprocessor Preprocessor Directives Preprocessor directives are instructions for compiler.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 12 - The Preprocessor Directives (Macros)
C Programming Functions Macros. Functions vs. Methods Java classes include methods which can be called from any code with appropriate access (recall public.
Macros. There are three basic phases for C programming. preprocessing, compiling, and linking. C input file is first passed to a preprocessing program.
C Tutorial Session #2 Type conversions More on looping Common errors Control statements Pointers and Arrays C Pre-processor Makefile Debugging.
Announcements Final NEXT WEEK (August 13 th Thursday at 16:00) Recitations will be held on August 12 th Wednesday We will solve sample final questions.
C Hints and Tips The preprocessor and other fun toys.
Engineering Computing I Chapter 4 Functions and Program Structure.
1 Preprocessor –How it works File Inclusion: #include Macro Definition: #define Predefined macros –__LINE__, __FILE__, __DATE__, __TIME__ Conditional Compilation.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Preprocessing Lecture 12 April 7, 2005.
C Programming Day 3. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Storage Class Specifiers Every variable in a C.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Preprocessor Midterm Review Lecture 7 Feb 17, 2004.
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 8 – C: Miscellanea Control, Declarations, Preprocessor, printf/scanf.
Compiler Directives. The C Preprocessor u The C preprocessor (cpp) changes your source code based on instructions, or preprocessor directives, embedded.
1 Lecture08: Program Development & Preprocessor 11/12/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
EEL 3801 C++ as an Enhancement of C. EEL 3801 – Lotzi Bölöni Comments  Can be done with // at the start of the commented line.  The end-of-line terminates.
THE PREPROCESSOR
The Preprocessor Directives Introduction Preprocessing – Occurs before program compiled Inclusion of external files Definition of symbolic constants.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 13 - The Preprocessor Outline 13.1Introduction 13.2The #include Preprocessor Directive 13.3The.
© Oxford University Press All rights reserved. CHAPTER 10 THE PREPROCESSOR DIRECTIVE.
1 Object-Oriented Programming -- Using C++ Andres, Wen-Yuan Liao Department of Computer Science and Engineering De Lin Institute of Technology
1 Lecture12: Preprocessor and Debugger 6/6/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
Pre-Compiler Directives for TTCN-3 Ina Schieferdecker in response to CR5089 and 5090.
Programming in C Project Organization Compiler Directives.
Unit 10 Code Reuse. Key Concepts Abstraction Header files Implementation files Storage classes Exit function Conditional compilation Command-line arguments.
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 13 – C: The Rest of the Preprocessor.
13 C Preprocessor.
Announcements Final Exam will be on August 19 at 16:00
CISC105 – General Computer Science
INC 161 , CPE 100 Computer Programming
Chapter 13 - The Preprocessor
Functions Separate Compilation
14. THE PREPROCESSOR.
Pre-processor Directives
Introduction to Programming
Variables, Types and Expressions
Preprocessor C program → Modified C program → Object Code
Variables, Types and Expressions
Announcements Midterm2 Grades to be announced NEXT Monday
Register Variables Declaring a variable as a "register" variable is an advisory to the compiler to keep the normal location of the variable in a register,
C Preprocessor(CPP).
Preprocessor.
Comments, Prototypes, Headers & Multiple Source Files
Programming in C Miscellaneous Topics.
CSc 352 An Introduction to the C Preprocessor
Programming in C Miscellaneous Topics.
C Preprocessing File I/O
C Preprocessor Seema Chandak.
Chapter 11: The Preprocessor
What Is? function predefined, programmer-defined
Conditional Compilation
Week 2 - Friday CS222.
Preprocessor Directives and Macros Chapter 21
Presentation transcript:

The C Preprocessor Yongjoon Lee April 22, 2005

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

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

File Inclusion Recall : #include –#include System directories –#include “foo.h” Current directories –gcc -I

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

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

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

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};

Conditional Inclusion #if #ifdef #ifndef #elif #else #endif

Conditional Compilation Debugging #define DEBUG_MODE #ifdef DEBUG_MODE /* … Debug code … */ #endif OS/Platform #ifdef _WIN32 #include #elif /* …… */

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

The Printf Yongjoon Lee April 22, 2005

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

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

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