 2000 Prentice Hall, Inc. All rights reserved. Chapter 13 - The Preprocessor Outline 13.1Introduction 13.2The #include Preprocessor Directive 13.3The.

Slides:



Advertisements
Similar presentations
Compilation and Debugging 101. Compilation in C/C++ hello.c Preprocessor Compiler stdio.h tmpXQ.i (C code) hello.o (object file)
Advertisements

Files in C Rohit Khokher.
Programming III SPRING 2015 School of Computer and Information Sciences Francisco R. Ortega, Ph.D. McKnight Fellow and GAANN Fellow LECTURE #3 Control.
Chapter 13 & 14 C Preprocessor and Other C Topics Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Introduction to C Programming
1 Lecture-4 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
 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.
Programming C/C++ on Eclipe Trình bày: Ths HungNM C/C++ Training.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 13 - The Preprocessor Outline 13.1Introduction 13.2The #include Preprocessor Directive 13.3The.
Introduction to C Programming
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
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.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 CMPE-013/L Pre-Processor Commands Gabriel Hugh Elkaim Spring 2013.
Object-Oriented Programming in C++
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)
Chapter 4 Functions and Program Structure Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering.
Macros. There are three basic phases for C programming. preprocessing, compiling, and linking. C input file is first passed to a preprocessing program.
1 Programs Composed of Several Functions Syntax Templates Legal C++ Identifiers Assigning Values to Variables Declaring Named Constants String Concatenation.
Chapter 13 C Preprocessor C How to Program, 8/e ©2016 by Pearson Education, Inc., Hoboken, NJ. All Rights Reserved.
C Hints and Tips The preprocessor and other fun toys.
Programming Practice (6) - Sorting Algorithms
Fundamentals of C and C++ Programming. EEL 3801 – Lotzi Bölöni Sub-Topics  Basic Program Structure  Variables - Types and Declarations  Basic Program.
Engineering Computing I Chapter 4 Functions and Program Structure.
L function n predefined, programmer-defined l arguments, (formal) parameters l return value l function call, function invocation l function definition.
1 Preprocessor –How it works File Inclusion: #include Macro Definition: #define Predefined macros –__LINE__, __FILE__, __DATE__, __TIME__ Conditional Compilation.
C How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Polymorphism, Template, Preprocessor Lecture 6 June 28, 2004.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Preprocessing Lecture 12 April 7, 2005.
1 A simple C++ program // ======================================================= // File:helloworld.cpp // Author:Vana Doufexi // Date:1/4/2006 // Description:Displays.
Created by Harry H. Cheng,  2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach Chapter 7: Preprocessing.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Preprocessor Midterm Review Lecture 7 Feb 17, 2004.
Compiler Directives. The C Preprocessor u The C preprocessor (cpp) changes your source code based on instructions, or preprocessor directives, embedded.
CHAPTER 2 PART #3 C++ INPUT / OUTPUT 1 st Semester King Saud University College of Applied studies and Community Service CSC1101 By: Fatimah.
Program in Multiple Files. l all C++ statements are divided into executable and non-executable l executable - some corresponding machine code is generated.
THE PREPROCESSOR
The Preprocessor Directives Introduction Preprocessing – Occurs before program compiled Inclusion of external files Definition of symbolic constants.
© 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
Adv. UNIX:pre/111 Advanced UNIX v Objectives of these slides: –look at the features of the C preprocessor Special Topics in Comp. Eng.
C PREPROCESSOR. Introduction  It is a program that processes our source program before it is passed to the compiler.  Preprocessor commands (often known.
C language + The Preprocessor. + Introduction The preprocessor is a program that processes that source code before it passes through the compiler. It.
Pre-Compiler Directives for TTCN-3 Ina Schieferdecker in response to CR5089 and 5090.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Template, Preprocessing Lecture 9 November 2, 2004.
COM S 326X Deep C Programming for the 21st Century Prof. Rozier
13 C Preprocessor.
What Is? function predefined, programmer-defined
INC 161 , CPE 100 Computer Programming
Chapter 13 - The Preprocessor
14. THE PREPROCESSOR.
Pre-processor Directives
Preprocessor C program → Modified C program → Object Code
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).
Chapter 3: Input/Output
Programming in C Miscellaneous Topics.
CSc 352 An Introduction to the C Preprocessor
Programming in C Miscellaneous Topics.
C Preprocessor Seema Chandak.
Lab guide # 12 Review C Preprocessor And Q&A.
C Programming Language
What Is? function predefined, programmer-defined
Chapter 1 c++ structure C++ Input / Output
Conditional Compilation
Preprocessor Directives and Macros Chapter 21
Presentation transcript:

 2000 Prentice Hall, Inc. All rights reserved. Chapter 13 - The Preprocessor Outline 13.1Introduction 13.2The #include Preprocessor Directive 13.3The #define Preprocessor Directive: Symbolic Constants 13.4The #define Preprocessor Directive: Macros 13.5Conditional Compilation 13.6The #error and #pragma Preprocessor Directives 13.7The # and ## Operators 13.8Line Numbers 13.9Predefined Symbolic Constants 13.10Assertions

 2000 Prentice Hall, Inc. All rights reserved. 13.1Introduction Preprocessing –Occurs before a program is compiled –Inclusion of other files –Definition of symbolic constants and macros –Conditional compilation of program code –Conditional execution of preprocessor directives Format of preprocessor directives –Lines begin with # –Only whitespace characters before directives on a line

 2000 Prentice Hall, Inc. All rights reserved. 13.2The #include Preprocessor Directive #include –Copy of a specified file included in place of the directive #include - Searches standard library for file Use for standard library files #include "filename" Searches current directory, then standard library Use for user-defined files Used for –Loading header files ( #include ) –Programs with multiple source files to be compiled together –Header file - has common declarations and definitions (classes, structures, function prototypes) #include statement in each file

 2000 Prentice Hall, Inc. All rights reserved. 13.3The #define Preprocessor Directive: Symbolic Constants #define –Preprocessor directive used to create symbolic constants and macros. Symbolic constants –When program compiled, all occurrences of symbolic constant replaced with replacement text Format #define identifier replacement-text –Example: #define PI –everything to right of identifier replaces text #define PI = replaces " PI" with " = ", probably results in an error –Cannot redefine symbolic constants with more #define statements

 2000 Prentice Hall, Inc. All rights reserved. 13.4The #define Preprocessor Directive: Macros Macro –Operation defined in #define –Macro without arguments: treated like a symbolic constant –Macro with arguments: arguments substituted for replacement text, macro expanded –Performs a text substitution - no data type checking Example: #define CIRCLE_AREA( x ) ( PI * ( x ) * ( x ) ) area = CIRCLE_AREA( 4 ); becomes area = ( * ( 4 ) * ( 4 ) );

 2000 Prentice Hall, Inc. All rights reserved. 13.4The #define Preprocessor Directive: Macros (II) Use parenthesis –Without them: #define CIRCLE_AREA( x ) PI * ( x ) * ( x ) area = CIRCLE_AREA( c + 2 ); becomes area = * c + 2 * c + 2; Evaluates incorrectly Multiple arguments #define RECTANGLE_AREA( x, y ) ( ( x ) * ( y ) ) rectArea = RECTANGLE_AREA( a + 4, b + 7 ); becomes rectArea = ( ( a + 4 ) * ( b + 7 ) );

 2000 Prentice Hall, Inc. All rights reserved. 13.4The #define Preprocessor Directive: Macros (III) #undef –Undefines a symbolic constant or macro, which can later be redefined

 2000 Prentice Hall, Inc. All rights reserved. 13.5Conditional Compilation Conditional compilation –Control preprocessor directives and compilation –Cast expressions, sizeof, enumeration constants cannot be evaluated Structure similar to if #if !defined( NULL ) #define NULL 0 #endif –Determines if symbolic constant NULL defined If NULL is defined, defined(NULL) evaluates to 1 If NULL not defined, defines NULL as 0 –Every #if ends with #endif –#ifdef short for #if defined(name) –#ifndef short for #if !defined(name)

 2000 Prentice Hall, Inc. All rights reserved. 13.5Conditional Compilation (II) Other statements #elif - equivalent of else if in an if structure #else - equivalent of else in an if structure "Comment out" code –Cannot use /*... */ –Use #if 0 code commented out #endif to enable code, change 0 to 1

 2000 Prentice Hall, Inc. All rights reserved. 13.5Conditional Compilation (III) Debugging #define DEBUG 1 #ifdef DEBUG cerr << "Variable x = " << x << endl; #endif –Defining DEBUG enables code –After code corrected, remove #define statement –Debugging statements are now ignored

 2000 Prentice Hall, Inc. All rights reserved. 13.6The #error and #pragma Preprocessor Directives #error tokens –Tokens - sequences of characters separated by spaces "I like C++" has 3 tokens –Prints message and tokens (depends on implementation) –For example: when #error encountered, tokens displayed and preprocessing stops (program does not compile) #pragma tokens –Implementation defined action (consult compiler documentation) –Pragmas not recognized by compiler are ignored

 2000 Prentice Hall, Inc. All rights reserved. 13.7The # and ## Operators # –Replacement text token converted to string with quotes #define HELLO( x ) cout << "Hello, " #x << endl; HELLO(John) becomes cout << "Hello, " "John" << endl; Strings separated by whitespace are concatenated when using cout ## –Concatenates two tokens #define TOKENCONCAT( x, y ) x ## y TOKENCONCAT( O, K ) becomes OK Notice #

 2000 Prentice Hall, Inc. All rights reserved. 13.8Line Numbers #line –renumbers subsequent code lines, starting with integer value –file name can be included #line 100 "myFile.c" –Lines are numbered from 100 beginning with next source code file –For purposes of errors, file name is "myfile.C" –Makes errors more meaningful –Line numbers do not appear in source file

 2000 Prentice Hall, Inc. All rights reserved. 13.9Predefined Symbolic Constants Five predefined symbolic constants –Cannot be used in #define or #undef

 2000 Prentice Hall, Inc. All rights reserved Assertions assert macro –Header –Tests value of an expression –If 0 (false) prints error message and calls abort assert( x <= 10 ); If NDEBUG defined... –All subsequent assert statements ignored –#define NDEBUG