© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 13 - The Preprocessor Outline 13.1Introduction.

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.
Pemrograman C Risanuri Hidayat. Why C  Compact, fast, and powerful  “Mid-level” Language  Standard for program development (wide acceptance)  It is.
Chapter 13 & 14 C Preprocessor and Other C Topics Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education.
Lecture 2 Introduction to C Programming
Introduction to C Programming
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 8 - Characters and Strings Outline 8.1Introduction.
 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.
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.
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.
Chapter 2 Overview of C Part I J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University.
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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
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 Preprocessing Lecture 12 April 7, 2005.
Created by Harry H. Cheng,  2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach Chapter 7: Preprocessing.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
 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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
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.
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.
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
Separate Compilation and Namespaces
Chapter 13 - The Preprocessor
Functions Separate Compilation
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
Conditional Compilation
Preprocessor Directives and Macros Chapter 21
Presentation transcript:

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education 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

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Objectives In this chapter, you will learn: –To be able to use #include for developing large programs. –To be able to use #define to create macros and macros with arguments. –To understand conditional compilation. –To be able to display error messages during conditional compilation. –To be able to use assertions to test if the values of expressions are correct.

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Introduction 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

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved The #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: 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

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved The #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 " = " –Cannot redefine symbolic constants once they have been created

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved The #define Preprocessor Directive: Macros Macro –Operation defined in #define –A macro without arguments is treated like a symbolic constant –A macro with arguments has its arguments substituted for replacement text, when the macro is expanded –Performs a text substitution – no data type checking –The macro #define CIRCLE_AREA( x ) ( PI * ( x ) * ( x ) ) would cause area = CIRCLE_AREA( 4 ); to become area = ( * ( 4 ) * ( 4 ) );

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved The #define Preprocessor Directive: Macros Use parenthesis –Without them the macro #define CIRCLE_AREA( x ) PI * ( x ) * ( x ) would cause area = CIRCLE_AREA( c + 2 ); to become area = * c + 2 * c + 2; Multiple arguments #define RECTANGLE_AREA( x, y ) ( ( x ) * ( y ) ) would cause rectArea = RECTANGLE_AREA( a + 4, b + 7 ); to become rectArea = ( ( a + 4 ) * ( b + 7 ) );

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved The #define Preprocessor Directive: Macros #undef –Undefines a symbolic constant or macro –If a symbolic constant or macro has been undefined it can later be redefined

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Conditional Compilation Conditional compilation –Control preprocessor directives and compilation –Cast expressions, sizeof, enumeration constants cannot be evaluated in preprocessor directives –Structure similar to if #if !defined( NULL ) #define NULL 0 #endif Determines if symbolic constant NULL has been defined –If NULL is defined, defined( NULL ) evaluates to 1 –If NULL is not defined, this function defines NULL to be 0 –Every #if must end with #endif –#ifdef short for #if defined( name ) –#ifndef short for #if !defined( name )

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Conditional Compilation Other statements –#elif – equivalent of else if in an if statement –#else – equivalent of else in an if statement "Comment out" code –Cannot use /*... */ –Use #if 0 code commented out #endif –To enable code, change 0 to 1

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Conditional Compilation Debugging #define DEBUG 1 #ifdef DEBUG cerr << "Variable x = " << x << endl; #endif –Defining DEBUG to 1 enables code –After code corrected, remove #define statement –Debugging statements are now ignored

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved The #error and #pragma Preprocessor Directives #error tokens –Tokens are sequences of characters separated by spaces "I like C++" has 3 tokens –Displays a message including the specified tokens as an error message –Stops preprocessing and prevents program compilation #pragma tokens –Implementation defined action (consult compiler documentation) –Pragmas not recognized by compiler are ignored

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved The # and ## Operators # –Causes a replacement text token to be converted to a string surrounded by quotes –The statement #define HELLO( x ) printf( “Hello, ” #x “\n” ); would cause HELLO( John ) to become printf( “Hello, ” “John” “\n” ); –Strings separated by whitespace are concatenated when using printf

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved The # and ## Operators ## –Concatenates two tokens –The statement #define TOKENCONCAT( x, y ) x ## y would cause TOKENCONCAT( O, K ) to become OK

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Line 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 Compiler messages will think that the error occurred in "myfile.C" Makes errors more meaningful Line numbers do not appear in source file

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Predefined Symbolic Constants Four predefined symbolic constants –Cannot be used in #define or #undef

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Assertions assert macro –Header –Tests value of an expression –If 0 ( false ) prints error message and calls abort –Example: assert( x <= 10 ); –If NDEBUG is defined All subsequent assert statements ignored #define NDEBUG