 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Preprocessing Lecture 12 April 7, 2005.

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.
Chapter 13 & 14 C Preprocessor and Other C Topics Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education.
1 Lecture-4 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
 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.
Introduction to C Programming
Basic Elements of C++ Chapter 2.
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.
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)
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.
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.
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.
CSC 143A 1 CSC 143 Introduction to C++ [Appendix A]
L what are predefined functions? l what is? n function name n argument(s) n return value n function call n function invocation n nested function call l.
Compiler Directives. The C Preprocessor u The C preprocessor (cpp) changes your source code based on instructions, or preprocessor directives, embedded.
PROGRAM ESSENTIALS. TOKENS  SMALLEST UNITS OF A PROGRAM LANGUAGE  Special Symbols  Mathematical Operators  Punctuation  Word Symbols  Key Words.
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.
 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
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.
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
Pre-Compiler Directives for TTCN-3 Ina Schieferdecker in response to CR5089 and 5090.
Programming in C Project Organization Compiler Directives.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Template, Preprocessing Lecture 9 November 2, 2004.
13 C Preprocessor.
Chapter Topics The Basics of a C++ Program Data Types
What Is? function predefined, programmer-defined
Predefined Functions Revisited
INC 161 , CPE 100 Computer Programming
Basic Elements of C++.
Chapter 13 - The Preprocessor
14. THE PREPROCESSOR.
Pre-processor Directives
Basic Elements of C++ Chapter 2.
Preprocessor C program → Modified C program → Object Code
Introduction to C++ Programming
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.
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
Presentation transcript:

 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Preprocessing Lecture 12 April 7, 2005

 2003 Prentice Hall, Inc. All rights reserved. 2 Preprocessing –Occurs before program compiled Inclusion of external files Definition of symbolic constants Macros Conditional compilation Conditional execution pf preprocessing directive –All directives begin with # Can only have whitespace before directives –Directives not C++ statements Do not end with ;

 2003 Prentice Hall, Inc. All rights reserved. 3 The #include Preprocessor Directive #include directive –Puts copy of file in place of directive –Two forms #include –For standard library header files –Searches pre-designated directories #include "filename" –Searches in current directory –Normally used for programmer-defined files Usage –Loading header files #include –Programs with multiple source files –Header file Has common declarations and definitions Classes, structures, enumerations, function prototypes Extract commonality of multiple program files

 2003 Prentice Hall, Inc. All rights reserved. 4 The #define Preprocessor Directive: Symbolic Constants #define –Symbolic constants Constants represented as symbols When program compiled, all occurrences replaced –Format #define identifier replacement-text #define PI –Everything to right of identifier replaces text #define PI= Replaces PI with "= " Probably an error –Cannot redefine symbolic constants Advantage: Takes no memory Disadvantages –Name not seen by debugger (only replacement text) –Do not have specific data type const variables preferred

 2003 Prentice Hall, Inc. All rights reserved. 5 The #define Preprocessor Directive: Macros Macro –Operation specified 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

 2003 Prentice Hall, Inc. All rights reserved. 6 The #define Preprocessor Directive: Macros Example #define CIRCLE_AREA( x ) ( PI * ( x ) * ( x ) ) area = CIRCLE_AREA( 4 ); becomes area = ( * ( 4 ) * ( 4 ) ); Use parentheses –Without them, #define CIRCLE_AREA( x ) PI * x * x area = CIRCLE_AREA( c + 2 ); becomes area = * c + 2 * c + 2; which evaluates incorrectly

 2003 Prentice Hall, Inc. All rights reserved. 7 The #define Preprocessor Directive: Macros Multiple arguments #define RECTANGLE_AREA( x, y ) ( ( x ) * ( y ) ) rectArea = RECTANGLE_AREA( a + 4, b + 7 ); becomes rectArea = ( ( a + 4 ) * ( b + 7 ) ); #undef –Undefines symbolic constant or macro –Can later be redefined

 2003 Prentice Hall, Inc. All rights reserved. 8 Conditional Compilation Control preprocessor directives and compilation –Cannot evaluate cast expressions, sizeof, enumeration constants Structure similar to if #if !defined( NULL ) #define NULL 0 #endif –Determines if symbolic constant NULL defined –If NULL defined, defined( NULL ) evaluates to 1 #define statement skipped –Otherwise #define statement used –Every #if ends with #endif

 2003 Prentice Hall, Inc. All rights reserved. 9 Conditional Compilation Can use else –#else –#elif is "else if" Abbreviations –#ifdef short for #if defined(name) –#ifndef short for #if !defined(name) "Comment out" code –Cannot use /*... */ with C-style comments Cannot nest /* */ –Instead, use #if 0 code commented out #endif –To enable code, change 0 to 1

 2003 Prentice Hall, Inc. All rights reserved. 10 Conditional Compilation 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

 2003 Prentice Hall, Inc. All rights reserved. 11 The #error and #pragma Preprocessor Directives #error tokens –Prints implementation-dependent message –Tokens are groups of characters separated by spaces #error 1 - Out of range error has 6 tokens –Compilation may stop (depends on compiler) #pragma tokens –Actions depend on compiler –May use compiler-specific options –Unrecognized #pragma s are ignored

 2003 Prentice Hall, Inc. All rights reserved. 12 The # and ## Operators # operator –Replacement text token converted to string with quotes #define HELLO( x ) cout << "Hello, " #x << endl; –HELLO( JOHN ) becomes cout << "Hello, " "John" << endl; Same as cout << "Hello, John" << endl; ## operator –Concatenates two tokens #define TOKENCONCAT( x, y ) x ## y –TOKENCONCAT( O, K ) becomes OK

 2003 Prentice Hall, Inc. All rights reserved. 13 Line Numbers #line –Renumbers subsequent code lines, starting with integer #line 100 –File name can be included –#line 100 "file1.cpp" Next source code line is numbered 100 For error purposes, file name is "file1.cpp" Can make syntax errors more meaningful Line numbers do not appear in source file

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

 2003 Prentice Hall, Inc. All rights reserved. 15 Assertions assert is a macro –Header –Tests value of an expression If 0 ( false ) prints error message, calls abort –Terminates program, prints line number and file –Good for checking for illegal values If 1 ( true ), program continues as normal –assert( x <= 10 ); To remove assert statements –No need to delete them manually –#define NDEBUG All subsequent assert statements ignored