CS 161 Introduction to Programming and Problem Solving Chapter 13 C++ Preprocessor Herbert G. Mayer, PSU Status 10/8/2014 Initial content copied verbatim.

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

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.
1 CS 161 Introduction to Programming and Problem Solving Chapter 9 C++ Program Components Herbert G. Mayer, PSU Status 10/20/2014.
CSc 352 An Introduction to the C Preprocessor Saumya Debray Dept. of Computer Science The University of Arizona, Tucson
Structure of a C program
 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.
Guide To UNIX Using Linux Third Edition
 2000 Prentice Hall, Inc. All rights reserved. Chapter 13 - The Preprocessor Outline 13.1Introduction 13.2The #include Preprocessor Directive 13.3The.
CS 161 Introduction to Programming and Problem Solving Chapter 13 Console IO Herbert G. Mayer, PSU Status 9/8/2014 Initial content copied verbatim from.
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.
The Pre-processor A unique feature of c language is the pre-processor. A program can use the tools provided by pre-processor to make his program easy to.
Computer Science 210 Computer Organization Introduction to C.
ECE 103 Engineering Programming Chapter 10 Variables, AKA Objects Herbert G. Mayer, PSU CS Status 6/19/2015 Initial content copied verbatim from ECE 103.
 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.
Introduction to C Programming CE Lecture 7 Compiler options and makefiles.
Chapter 13 C Preprocessor C How to Program, 8/e ©2016 by Pearson Education, Inc., Hoboken, NJ. All Rights Reserved.
1 Programming in C Hello World! Soon I will control the world! Soon I will control the world!
C Hints and Tips The preprocessor and other fun toys.
Fundamentals of C and C++ Programming. EEL 3801 – Lotzi Bölöni Sub-Topics  Basic Program Structure  Variables - Types and Declarations  Basic Program.
CECS 121 Test 1. Functions allow you to group program statements under one name C and C++ are case-sensitive so main(), MAIN(), and Main() are all different.
CSCI 171 Presentation 8 Built-in Functions, Preprocessor Directives, and Macros.
C How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
CS 161 Introduction to Programming and Problem Solving Chapter 19 Single-Dimensional Arrays Herbert G. Mayer, PSU Status 10/8/2014 Initial content copied.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Preprocessing Lecture 12 April 7, 2005.
ECE 103 Engineering Programming Chapter 18 Iteration Herbert G. Mayer, PSU CS Status 7/19/2015 Initial content copied verbatim from ECE 103 material developed.
ECE 103 Engineering Programming Chapter 36 C Storage Classes Herbert G. Mayer, PSU CS Status 8/4/2014 Initial content copied verbatim from ECE 103 material.
Dr. Mark L. HornickCS-1030 Dr. Mark Hornick 1 C++ Global functions Declarations & Definitions Preprocessor Directives.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Preprocessor Midterm Review Lecture 7 Feb 17, 2004.
CS 161 Introduction to Programming and Problem Solving Chapter 18 Control Flow Through C++ Program Herbert G. Mayer, PSU Status 10/8/2014 Initial content.
Compiler Directives. The C Preprocessor u The C preprocessor (cpp) changes your source code based on instructions, or preprocessor directives, embedded.
Lecture-3 Functions and Recursion. C Preprocessor Includes header files like stdio.h Expands macros defined Handles conditional compilations PreprocessorProcessor.
THE PREPROCESSOR
The Preprocessor Directives Introduction Preprocessing – Occurs before program compiled Inclusion of external files Definition of symbolic constants.
ECE 103 Engineering Programming Chapter 31 C Scopes Herbert G. Mayer, PSU CS Status 8/1/2015 Initial content copied verbatim from ECE 103 material developed.
ECE 103 Engineering Programming Chapter 7 Compiling C Programs Herbert G. Mayer, PSU CS Status 6/19/2015 Initial content copied verbatim from ECE 103 material.
 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.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
Lecturer: Nguyen Thi Hien Software Engineering Department Home page: hienngong.wordpress.com Chapter 2: Language C++
C language + The Preprocessor. + Introduction The preprocessor is a program that processes that source code before it passes through the compiler. It.
CS 161 Introduction to Programming and Problem Solving Chapter 17 Nested Loops Herbert G. Mayer, PSU Status 9/8/2014 Initial content copied verbatim from.
2. C FUNDAMENTALS. Example: Printing a Message /* Illustrates comments, strings, and the printf function */ #include int main(void) { printf("To C, or.
ECE 103 Engineering Programming Chapter 30 C Functions Herbert G. Mayer, PSU CS Status 8/9/2014 Initial content copied verbatim from ECE 103 material developed.
13 C Preprocessor.
Computer Science 210 Computer Organization
Introduction to C++ Systems Programming.
INC 161 , CPE 100 Computer Programming
Chapter 13 - 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).
Preprocessor.
CSc 352 An Introduction to the C Preprocessor
ECE 103 Engineering Programming Chapter 19 Nested Loops
C Preprocessor Seema Chandak.
ECE 103 Engineering Programming Chapter 20 Change in Flow of Control
ECE 103 Engineering Programming Chapter 18 Iteration
What Is? function predefined, programmer-defined
Week 2 - Friday CS222.
Preprocessor Directives and Macros Chapter 21
Presentation transcript:

CS 161 Introduction to Programming and Problem Solving Chapter 13 C++ Preprocessor Herbert G. Mayer, PSU Status 10/8/2014 Initial content copied verbatim from ECE 103 material by prof. Phillip PSU

Syllabus Preprocessor Directives Include Files Macros Named Constants Examples

2 Preprocessor Directives Before a C++ program is compiled, the source code file is first handled by a preprocessor prog_1.c prog_2.c prog_3.c etc. Source files Compiler Preprocessor prog_1.h prog_2.h prog_3.h etc. User Header files stdio.h stdlib.h math.h etc. Library Header files Assembler To Linker

3 The C++ preprocessor does this:  Strips comments from the source code  Acts on preprocessor directives The preprocessor does not compile code! Preprocessor directive → a special command recognized only by the preprocessor  # symbol prefixes each directive Directives are used for:  Modifying the compilation process  Creating macro definitions

4 Common directives: #include f:Inserts the contents of a specified text file into the source code #define m:Defines a macro #undef m:Cancels a previously defined macro #if expr:Begins a conditional block → expr tested #ifdef m:Tests whether a macro is defined #ifndef m:Tests whether a macro is not yet defined #else:Alternative branch #elif expr:Else if → expr tested #endif:Ends a conditional block

5 Include Files C++ provides access to common functions (e.g., I/O, math routines) via external libraries Each library has a header file that may contain prototypes, macros, and definitions A header file is a text file with a.h extension Before accessing a library’s functions, use the #include directive to load its header file #include directives are typically placed at the beginning of the source file

6 Syntax: #include Angle bracket form → searches for a header file in the system include directory #include "filename" Quoted form → searches for a header file starting with the current working directory

7 Example: /* Loads system header for stdio library */ #include /* Loads system header for math library */ #include /* Loads user header file */ #include "mystuff.h" /* Loads user header file */ #include "../LIB/mylib.h"

8 Macros A preprocessor macro is used for:  Defining a symbolic name for a constant value  Defining a terse alias for something else To define a macro, use this directive: #define mname mtext mname → macro name, formal parameters allowed mtext → substitution text At least one whitespace

9 The preprocessor expands macros in the C code Wherever it sees mname in the source file, it substitutes mtext in its place A macro definition can contain previously defined macros Macros are not at all the same as C variables! No storage is created in memory; macros only exist at compile-time An old C programming convention rules that macro names be fully capitalized to differentiate them from C variable names; this is NOT a language rule, hence NOT enforced by the C compiler!

10 Example: /* Pay rate in $ per hour */ /* Time in hours per week */ #include #define RATE #define HRS_WK1 40 int main (void) { // main float rate2 = 22.50; float hrs_wk2 = 35; float pay1, pay2; /* Weekly pay in $ */ pay1 = RATE1 * HRS_WK1; pay2 = rate2 * hrs_wk2; printf("RATE1=%f\n",RATE1); return 0; } //end main [stdio.h definitions] [are inserted here ] int main (void) { // main float rate2 = 22.50; float hrs_wk2 = 35; float pay1, pay2; pay1 = 9.25 * 40; pay2 = rate2 * hrs_wk2; printf("RATE1=%f\n",9.25); return 0; } //end main Original source fileAfter macro expansion

11 Macro expansion is very literal. Be careful of unintended side-effects Example: #define A 5 #define B A + 1 #define FACTOR1 A * B/ #define FACTOR2 (A) * (B)/ #define Tip_Rate 0.15 #define UINT unsigned int #define PRINTBLANK printf("\n")

12 Commenting Out Code By Using #if … #endif This is illegal in C90. Comments cannot be nested within other comments. /* x = r * cos(a); /* x component */ y = r * sin(a); /* y component */ */ This is legal in C90. The code within the #if 0 … #endif is removed from the source before it ever reaches the compiler. #if 0 x = r * cos(a); /* x component */ y = r * sin(a); /* y component */ #endif

13 Named Constants magic number → a non-obvious literal number or character that is directly embedded in a statement Avoid magic numbers. Use named constants instead! Example: /* Newton's equation of motion */ y = –0.5 * 9.81*t*t *t + 2.5; What do these numbers mean? 9.81 = Earth’s gravitational acceleration g (m/s 2 ) 75.2 = Initial velocity v y0 (m/s) in y -axis direction 2.5 = Initial position y 0 (m) in y -axis direction

14 Example: #include #define G_EARTH 9.81 // m/s^2 #define VELOCITY_Y // m/s #define POSITION_Y0 2.5 // m int main (void) { // main double t; // Time (s) double y; // Height (m) at time t printf( "Enter time (s): ” ); scanf( "%lf", &t ); y = -0.5 * G_EARTH * t * t + VELOCITY_Y0 * t + POSITION_Y0; printf( "y(%.2f s) = %.2f m\n", t, y ); return 0; } //end main

15 Question: Can I just use constants instead? Yes, you could define something like this: static const double g_earth = 9.81; Comparison of #define versus const #defineconst variable Cannot create a pointer to a macroCan create a pointer to a const variable Ignores scopeRespects scope and is type-safe No symbol in the debuggerHas a symbol in the debugger Can be used to set the size of an arrayCannot be used as array dimension Can be used in a switch case valueCannot be used in a switch case