 2007 Pearson Education, Inc. All rights reserved. 1 13 C Preprocessor.

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
 2005 Pearson Education, Inc. All rights reserved Introduction.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Introduction to C Programming
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
C Programming Language 4 Developed in 1972 by Dennis Ritchie at AT&T Bell Laboratories 4 Used to rewrite the UNIX operating system 4 Widely used on UNIX.
 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.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
© 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
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.
 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.
 2006 Pearson Education, Inc. All rights reserved Arrays.
Chapter 2 Overview of C Part I J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University.
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
1 Programs Composed of Several Functions Syntax Templates Legal C++ Identifiers Assigning Values to Variables Declaring Named Constants String Concatenation.
1 C++ Syntax and Semantics, and the Program Development Process.
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.
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.
Overview of C. C—a high-level programming language developed in 1972 by Dennis Ritchie at AT&T Bell Laboratories. We will discuss: –the elements of a.
 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.
PROGRAM ESSENTIALS. TOKENS  SMALLEST UNITS OF A PROGRAM LANGUAGE  Special Symbols  Mathematical Operators  Punctuation  Word Symbols  Key Words.
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.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
1 C Syntax and Semantics Dr. Sherif Mohamed Tawfik Lecture Two.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Template, Preprocessing Lecture 9 November 2, 2004.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
13 C Preprocessor.
INC 161 , CPE 100 Computer Programming
Chapter 2, Part I Introduction to C Programming
Chapter 13 - The Preprocessor
Functions Separate Compilation
14. THE PREPROCESSOR.
Pre-processor Directives
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).
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
Conditional Compilation
Preprocessor Directives and Macros Chapter 21
Presentation transcript:

 2007 Pearson Education, Inc. All rights reserved C Preprocessor

 2007 Pearson Education, Inc. All rights reserved. 2 Hold thou the good; define it well. — Alfred, Lord Tennyson I have found you an argument; but I am not obliged to find you an understanding. — Samuel Johnson A good symbol is the best argument, and is a missionary to persuade thousands. — Ralph Waldo Emerson

 2007 Pearson Education, Inc. All rights reserved. 3 Conditions are fundamentally sound. — Herbert Hoover [December 1929] The partisan, when he is engaged in a dispute, cares nothing about the rights of the question, but is anxious only to convince his hearers of his own assertions. — Plato

 2007 Pearson Education, Inc. All rights reserved. 4 OBJECTIVES In this chapter you will learn:  To use #include for developing large programs.  To use #define to create macros and macros with arguments.  To use conditional compilation to specify portions of a program that should not always be compiled (such as code that assists you in debugging).  To display error messages during conditional compilation.  To use assertions to test if the values of expressions are correct.

 2007 Pearson Education, Inc. All rights reserved Introduction 13.2 #include Preprocessor Directive 13.3 #define Preprocessor Directive: Symbolic Constants 13.4 #define Preprocessor Directive: Macros 13.5Conditional Compilation 13.6 #error and #pragma Preprocessor Directives 13.7 # and ## Operators 13.8Line Numbers 13.9Predefined Symbolic Constants 13.10Assertions

 2007 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

 2007 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

 2007 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

 2007 Pearson Education, Inc. All rights reserved. 9 Good Programming Practice 13.1 Using meaningful names for symbolic constants helps make programs more self-documenting.

 2007 Pearson Education, Inc. All rights reserved. 10 Good Programming Practice 13.2 By convention, symbolic constants are defined using only uppercase letters and underscores.

 2007 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 ) );

 2007 Pearson Education, Inc. All rights reserved The #define Preprocessor Directive: Macros  Use parentheses – 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 ) );

 2007 Pearson Education, Inc. All rights reserved. 13 Common Programming Error 13.1 Forgetting to enclose macro arguments in parentheses in the replacement text can lead to logic errors.

 2007 Pearson Education, Inc. All rights reserved. 14 Performance Tip 13.1 Macros can sometimes be used to replace a function call with inline code prior to execution time. This eliminates the overhead of a function call. Today’s optimizing compilers will often inline functions for you, so many programmers no longer use macros for this purpose.

 2007 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

 2007 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 )

 2007 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

 2007 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

 2007 Pearson Education, Inc. All rights reserved. 19 Common Programming Error 13.2 Inserting conditionally compiled printf statements for debugging purposes in locations where C currently expects a single statement. In this case, the conditionally compiled statement should be enclosed in a compound statement. Thus, when the program is compiled with debugging statements, the flow of control of the program is not altered.

 2007 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

 2007 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

 2007 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

 2007 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

 2007 Pearson Education, Inc. All rights reserved Predefined Symbolic Constants  Four predefined symbolic constants – Cannot be used in #define or #undef

 2007 Pearson Education, Inc. All rights reserved. 25 Fig | Some predefined symbolic constants.

 2007 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

 2007 Pearson Education, Inc. All rights reserved. 27 Software Engineering Observation 13.1 Assertions are not meant as a substitute for error handling during normal runtime conditions. Their use should be limited to finding logic errors.