Chapter 13 - The Preprocessor



Similar documents
The C Programming Language course syllabus associate level

How To Port A Program To Dynamic C (C) (C-Based) (Program) (For A Non Portable Program) (Un Portable) (Permanent) (Non Portable) C-Based (Programs) (Powerpoint)

IS0020 Program Design and Software Tools Midterm, Feb 24, Instruction

Ubuntu. Ubuntu. C++ Overview. Ubuntu. History of C++ Major Features of C++

1 Abstract Data Types Information Hiding

ASCII Encoding. The char Type. Manipulating Characters. Manipulating Characters

Sources: On the Web: Slides will be available on:

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

So far we have considered only numeric processing, i.e. processing of numeric data represented

PL / SQL Basics. Chapter 3

Informatica e Sistemi in Tempo Reale

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

MISRA-C:2012 Standards Model Summary for C / C++

Some Scanner Class Methods

Lecture 5: Java Fundamentals III

Introduction to Java Applications Pearson Education, Inc. All rights reserved.

KITES TECHNOLOGY COURSE MODULE (C, C++, DS)

1 Introduction. 2 An Interpreter. 2.1 Handling Source Code

Computer Programming C++ Classes and Objects 15 th Lecture


Visual Basic Programming. An Introduction

Eventia Log Parsing Editor 1.0 Administration Guide

Programming Languages CIS 443

Chapter 3. Input and output. 3.1 The System class

Bachelors of Computer Application Programming Principle & Algorithm (BCA-S102T)

Unix Scripts and Job Scheduling

The programming language C. sws1 1

Programming Database lectures for mathema

Keywords are identifiers having predefined meanings in C programming language. The list of keywords used in standard C are : unsigned void

We will learn the Python programming language. Why? Because it is easy to learn and many people write programs in Python so we can share.

6.170 Tutorial 3 - Ruby Basics

C Programming. for Embedded Microcontrollers. Warwick A. Smith. Postbus 11. Elektor International Media BV. 6114ZG Susteren The Netherlands

JavaScript: Introduction to Scripting Pearson Education, Inc. All rights reserved.

Lexical Analysis and Scanning. Honors Compilers Feb 5 th 2001 Robert Dewar

Chapter 2: Elements of Java

COMP 356 Programming Language Structures Notes for Chapter 4 of Concepts of Programming Languages Scanning and Parsing

PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?

Storage Classes CS 110B - Rule Storage Classes Page 18-1 \handouts\storclas

Basic Java Constructs and Data Types Nuts and Bolts. Looking into Specific Differences and Enhancements in Java compared to C

Moving from CS 61A Scheme to CS 61B Java

CP Lab 2: Writing programs for simple arithmetic problems

C++ INTERVIEW QUESTIONS

Install Java Development Kit (JDK) 1.8

Programming languages C

C / C++ and Unix Programming. Materials adapted from Dan Hood and Dianna Xu

PROBLEM SOLVING SEVENTH EDITION WALTER SAVITCH UNIVERSITY OF CALIFORNIA, SAN DIEGO CONTRIBUTOR KENRICK MOCK UNIVERSITY OF ALASKA, ANCHORAGE PEARSON

Exercise 1: Python Language Basics

PRI-(BASIC2) Preliminary Reference Information Mod date 3. Jun. 2015

CS106A, Stanford Handout #38. Strings and Chars

Topics. Parts of a Java Program. Topics (2) CS 146. Introduction To Computers And Java Chapter Objectives To understand:

Hands-on Exercise 1: VBA Coding Basics

Embedded C Programming, Linux, and Vxworks. Synopsis

Applied Informatics C++ Coding Style Guide

Massachusetts Institute of Technology Department of Electrical Engineering and Computer Science

Virtuozzo Virtualization SDK

Simple C Programs. Goals for this Lecture. Help you learn about:

Lexical analysis FORMAL LANGUAGES AND COMPILERS. Floriano Scioscia. Formal Languages and Compilers A.Y. 2015/2016

Chapter One Introduction to Programming

C++ Outline. cout << "Enter two integers: "; int x, y; cin >> x >> y; cout << "The sum is: " << x + y << \n ;

Pemrograman Dasar. Basic Elements Of Java

Introduction to Python

IC4 Programmer s Manual

Chapter 2 Writing Simple Programs

Computer Programming Tutorial

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

ESCI 386 IDL Programming for Advanced Earth Science Applications Lesson 6 Program Control

Recognizing PL/SQL Lexical Units. Copyright 2007, Oracle. All rights reserved.

Passing 1D arrays to functions.

MSP430 C/C++ CODE GENERATION TOOLS Compiler Version 3.2.X Parser Error/Warning/Remark List

Command Scripts Running scripts: include and commands

JavaScript. JavaScript: fundamentals, concepts, object model. Document Object Model. The Web Page. The window object (1/2) The document object

Exercise 4 Learning Python language fundamentals

AP Computer Science Java Subset

Chapter 2 Basics of Scanning and Conventional Programming in Java

CS 1133, LAB 2: FUNCTIONS AND TESTING

Python Lists and Loops

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

Programming Project 1: Lexical Analyzer (Scanner)

VB.NET Programming Fundamentals

About The Tutorial. Audience. Prerequisites. Copyright & Disclaimer

Generalizing Overloading for C++2000

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

Introduction to Lex. General Description Input file Output file How matching is done Regular expressions Local names Using Lex

J a v a Quiz (Unit 3, Test 0 Practice)

Basics of I/O Streams and File I/O

Fundamentals of Programming

Introduction to Programming II Winter, 2014 Assignment 2

Introduction to Python

Compiler Construction

CS 111 Classes I 1. Software Organization View to this point:

How To Write Portable Programs In C

Perl in a nutshell. First CGI Script and Perl. Creating a Link to a Script. print Function. Parsing Data 4/27/2009. First CGI Script and Perl

PYTHON Basics

GUJARAT TECHNOLOGICAL UNIVERSITY, AHMEDABAD, GUJARAT COURSE CURRICULUM. Course Title: Advanced Computer Programming (Code: )

Transcription:

Chapter 13 - The Preprocessor Outline 13.1 Introduction 13.2 The#include Preprocessor Directive 13.3 The#define Preprocessor Directive: Symbolic Constants 13.4 The#define Preprocessor Directive: Macros 13.5 Conditional Compilation 13.6 The#error and#pragma Preprocessor Directives 13.7 The#and## Operators 13.8 Line Numbers 13.9 Predefined Symbolic Constants 13.10 Assertions 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. 1

13.1 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 13.2 The#include Preprocessor Directive #include Copy of a specified file included in place of the directive #include <filename> Used to include standard library headers such as stdio.h Header file has common declarations and definitions (classes, structures, function prototypes) #include "filename" Searches current directory, then standard library Use to include programmer-defined headers Can also be used to include other source files to be compiled together (not recommended) 2

13.3 The#define Preprocessor Directive: Symbolic Constants #define used to create symbolic constants and macros Symbolic constants When program compiled, all occurrences of symbolic constant are replaced with replacement text Format #define identifier replacement-text Example: #define PI 3.14159 Everything to right of identifier replaces text #define PI = 3.14159 Replaces PI with "= 3.14159" Cannot redefine symbolic constants once they have been created 13.4 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 3

Macro Example The macro #define CIRCLE_AREA( x ) ( PI * ( x ) * ( x ) ) would cause area = CIRCLE_AREA( 4 ); to become area = ( 3.14159 * ( 4 ) * ( 4 ) ); Use Parenthesis If the parentheses around each x is omitted: #define CIRCLE_AREA( x ) (PI * x * x ) the macro would cause area = CIRCLE_AREA( c + 2 ); to become area = 3.14159 * c + 2 * c + 2; 4

Macro: 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 ) ); #undef Preprocessor Directive:#undef Undefines a symbolic constant or macro If a symbolic constant or macro has been undefined it can later be redefined 5

13.5 Conditional Compilation Conditional compilation ( 條 件 式 編 譯 ) Control preprocessor directives and compilation 條 件 式 編 譯 前 指 示 A conditional preprocessor directive evaluate a constant integer expression The expression cannot include variables, cast expressions,sizeof, and enumeration constants Conditional Preprocessor Construct Structure similar toif #if!defined( NULL ) #define NULL 0 #endif Determines if symbolic constantnull has been defined If NULL is defined,defined( NULL ) evaluates to1 IfNULL is not defined, this function definesnull to be 0 Every #if must end with#endif #ifdef short for#if defined( name ) #ifndef short for#if!defined( name ) 6

13.5 Conditional Compilation Other statements #elif equivalent of else if in an if statement #else equivalent of else in an if statement "Comment out" code If the code contains comments, cannot use/*... */ Use #if 0 code commented out #endif To enable code, change0to1 Debugging 13.5 Conditional Compilation #define DEBUG 1 #ifdef DEBUG printf( Variable x = %d\n, x); #endif Defining DEBUG to 1 enables code After code corrected, remove#define statement Debugging statements are now ignored 7

13.6 The#error and#pragma Preprocessor Directives #error tokens Tokens are sequences of characters separated by spaces ( 和 字 串 不 同 的 是 tokens 沒 有 前 後 的 引 號 ) 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 # 13.7 The#and## Operators Causes a replacement text token to be converted to a string surrounded by quotes ( 將 token 加 上 前 後 引 號 變 成 字 串 ) 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 8

13.7 The#and## Operators ## Concatenates two tokens The statement #define TOKENCONCAT( x, y ) x ## y would cause TOKENCONCAT( O, K ) to become OK #line 13.8 Line Numbers Renumbers subsequent code lines, starting with integer value File name can be included #line 100 "myfile.c" Lines are numbered from100 beginning with next source code line Compiler messages will think that the error occurred in"myfile.c" Makes errors more meaningful Line numbers do not appear in source file 9

13.9 Predefined Symbolic Constants Four predefined symbolic constants Cannot be used in#define or#undef Symbolic constant Description LINE The line number of the current source code line (an integer constant). FILE DATE TIME The presumed name of the source file (a string). The date the source file is compiled (a string of the form "Mmm dd yyyy" such as "Jan 19 2001"). The time the source file is compiled (a string literal of the form "hh:mm:ss"). 13.10 Assertions Theassert macro Defined in header file<assert.h> Tests value of an expression If0 (false) prints error message and callsabort Example: assert( x <= 10 ); If NDEBUG is defined All subsequent assert statements ignored #define NDEBUG 10