Preprocessor Directives and Macros Chapter 21

Slides:



Advertisements
Similar presentations
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 17 - The Preprocessor Outline 17.1Introduction 17.2The #include Preprocessor Directive 17.3The.
Advertisements

Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
© 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.
Macro & Function. Function consumes more time When a function is called, the copy of the arguments are passed to the parameters in the function. After.
You gotta be cool. C++ ? C++ Standard Library Header Files Inline Functions References and Reference Parameters Default Arguments and Empty Parameter.
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.
CS 161 Introduction to Programming and Problem Solving Chapter 13 C++ Preprocessor Herbert G. Mayer, PSU Status 10/8/2014 Initial content copied verbatim.
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)
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single.
Macros. There are three basic phases for C programming. preprocessing, compiling, and linking. C input file is first passed to a preprocessing program.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 6 Functions.
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
Introduction to C Programming CE Lecture 7 Compiler options and makefiles.
CSCI 130 Chapter 5 Functions. Functions are named uniquely Performs a specific task Is independent –should not interfere with other parts of program May.
1 Homework / Exam Finish up K&R Chapters 3 & 4 Starting K&R Chapter 5 Next Class HW4 due next class Go over HW3 solutions.
Chapter 13 Programming in the Large Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. TA: 鄭筱親 陳昱豪.
How to start Visual Studio 2008 or 2010 (command-line program)
Functions CIS Feb-06. Summary Slide Using Functions Mathematical Functions Misc. Functions Naming Conventions Writing Functions –Function Prototype.
Compilation & Linking Computer Organization I 1 November 2009 © McQuain, Feng & Ribbens The Preprocessor When a C compiler is invoked, the.
CSCI 171 Presentation 8 Built-in Functions, Preprocessor Directives, and Macros.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Preprocessing Lecture 12 April 7, 2005.
CSCI 171 Presentation 6 Functions and Variable Scope.
 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.
Separate Compilation Bryce Boe 2013/10/09 CS24, Fall 2013.
Lecture-3 Functions and Recursion. C Preprocessor Includes header files like stdio.h Expands macros defined Handles conditional compilations PreprocessorProcessor.
EEL 3801 C++ as an Enhancement of C. EEL 3801 – Lotzi Bölöni Comments  Can be done with // at the start of the commented line.  The end-of-line terminates.
THE PREPROCESSOR
The Preprocessor Directives Introduction Preprocessing – Occurs before program compiled Inclusion of external files Definition of symbolic constants.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 6: Functions.
 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.
Pre-Compiler Directives for TTCN-3 Ina Schieferdecker in response to CR5089 and 5090.
Defining Data Types in C++ Part 2: classes. Quick review of OOP Object: combination of: –data structures (describe object attributes) –functions (describe.
Unit 10 Code Reuse. Key Concepts Abstraction Header files Implementation files Storage classes Exit function Conditional compilation Command-line arguments.
COM S 326X Deep C Programming for the 21st Century Prof. Rozier
13 C Preprocessor.
Pointer to an Object Can define a pointer to an object:
ECE 382 Lesson 20 Lesson Outline Structs Functions Headers Example
What Is? function predefined, programmer-defined
INC 161 , CPE 100 Computer Programming
Chapter 13 - The Preprocessor
Functions Separate Compilation
14. THE PREPROCESSOR.
The Preprocessor Based on Chapter 1 in C++ for Java Programmers by Weiss When a C compiler is invoked, the first thing that happens is that the code is.
C Basics.
Chapter 6: Functions Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Pre-processor Directives
11/10/2018.
Introduction to Programming
Preprocessor C program → Modified C program → Object Code
6.12 Default Arguments.
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).
Homework Finish up K&R Chapters 3 & 4
C Preprocessor Seema Chandak.
The C Language: Intro.
Macro A fragment of code which has been given a name.
Chapter 11: The Preprocessor
What Is? function predefined, programmer-defined
Lecture 8 Object Oriented Programming (OOP)
Standard Version of Starting Out with C++, 4th Edition
Week 2 - Friday CS222.
Presentation transcript:

Preprocessor Directives and Macros Chapter 21 CSCI 130 Preprocessor Directives and Macros Chapter 21

Preprocessor Part of all C compiler packages First component that processes source code Source code changed based on directives all preprocessor directives begin with # Output - modified source code file used in next step of compilation deleted automatically by system

#include Imports other files into source code maybe library functions (stdio.h) use < > may be user defined functions use “ ” may have more than one function in file

Advantages of #include Structure Portability Conventions: similar functions grouped in one file file given descriptive name

Example - math library file Contents of user created mathfn.h file: int areaOfSquare(int length) { return length * length; } int areaOfRectangle(int width, int height) { return width * height;

main.c main.c is sometimes called the driver ‘drives’ the flow of logic often does not contain any function definitions contains function prototypes - includes functions with #include preprocessor directive #include “mathfn.h”

#define Used for substitution macros Used for function macros substituting values for variables Used for function macros defining a function ‘on the fly’

#define - substitution macro Creates substitution macro #define PI 3.14 area = radius * radius * PI circumference = 2 * radius * PI Changes in source code after precompiling area = radius * radius * 3.14 circumference = 2 * radius * 3.14 Space after constant indicates substitution macro

#define - function macro Shorthand for a more complicated operation Arguments not type sensitive Ex: #define HALFOF(value) ((value)/2) printf(“%f”, HALFOF(x + y)); Changes in source code after precompiling printf(“%f”, ((x+y)/2)); No space after function name indicates function macro

Other function macro examples #define AVG3(a, b, c) (((a) + (b) + (c)) / 5) #define SMALLER(x, y) ((x) < (y) ? (x) : (y)) #define SUM (x, y, z) ((x) + (y) + (z))

Common Errors-function macros Spaces after function macro name #define SUM (x, y, z) ((x) + (y) + (z)) Forgetting parenthesis #define AREA(x, y) x*y All parameters must be used #define SUM(x, y, z) ((x) + (y))

Macros vs. Functions Macros can be used for simple functions Size of program Functions exist as a single copy Macro expanded in code every time it is called Execution efficiency no overhead to use a macro overhead required for functions

#if and #endif Preprocessor directives controlling conditional compilation if statement determines if statements executed #if statement determines if statements compiled #elif, #else work as else if, else

Where would #if be used For purposes of CSCI 130: When including files If file included more than once, code is imported for each time out of memory Use #if and ‘defined’ keywords to conditionally include statements for compilation

Example #if #if defined mathfn_h #else #define mathfn_h //Note: no periods can be used here int areaOfSquare(int length) { return length * length; } int areaOfRectangle(int width, int height) { return width * height; #endif

Not (!) is allowed #if !defined mathfn_h #define mathfn_h int areaOfSquare(int length) { return length * length; } int areaOfRectangle(int width, int height) { return width * height; #endif

#ifndef directive #ifndef mathfn_h #define mathfn_h int areaOfSquare(int length) { return length * length; } int areaOfRectangle(int width, int height) { return width * height; #endif

#undef Opposite effect of #define #define PI 3.14 ... #undef PI