Programming for Engineers C Preprocessor

Similar documents
OBJECT ORIENTED PROGRAMMING USING C++

Chapter 7: Preprocessing Directives

Have examined process Creating program Have developed program Written in C Source code

MODULE 10 PREPROCESSOR DIRECTIVES

Gabriel Hugh Elkaim Spring CMPE 013/L: C Programming. CMPE 013/L: C Programming

Weeks 10/11 C Preprocessor Macros and Functions

C Programming. The C Preprocessor and Some Advanced Topics. Learn More about #define. Define a macro name Create function-like macros.

Conditional Compilation

fpp: Fortran preprocessor March 9, 2009

Basic C Programming (2) Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

Unit 4 Preprocessor Directives

C for Engineers and Scientists: An Interpretive Approach. Chapter 7: Preprocessing Directives

COMsW Introduction to Computer Programming in C

COMP322 - Introduction to C++ Lecture 02 - Basics of C++

Programming for Engineers Introduction to C

Appendix A. The Preprocessor

COP 3275: Chapter 02. Jonathan C.L. Liu, Ph.D. CISE Department University of Florida, USA

C Fundamentals & Formatted Input/Output. adopted from KNK C Programming : A Modern Approach

CSCI 2132 Software Development. Lecture 8: Introduction to C

Intermediate Programming, Spring 2017*

Fundamental of Programming (C)

C and C++ 2. Functions Preprocessor. Alan Mycroft

COMP322 - Introduction to C++

Fundamentals of Programming

Programming for Engineers Iteration

c) Comments do not cause any machine language object code to be generated. d) Lengthy comments can cause poor execution-time performance.

Introduction to C Programming (Part A) Copyright 2008 W. W. Norton & Company. All rights Reserved

CMSC 246 Systems Programming

The C Pre Processor ECE2893. Lecture 18. ECE2893 The C Pre Processor Spring / 10

Control flow and string example. C and C++ Functions. Function type-system nasties. 2. Functions Preprocessor. Alastair R. Beresford.

C: How to Program. Week /June/18


Topic 6: A Quick Intro To C. Reading. "goto Considered Harmful" History

Problem Solving and 'C' Programming

P.G.TRB - COMPUTER SCIENCE. c) data processing language d) none of the above

Oregon State University School of Electrical Engineering and Computer Science. CS 261 Recitation 2. Spring 2016

Rule 1-3: Use white space to break a function into paragraphs. Rule 1-5: Avoid very long statements. Use multiple shorter statements instead.

CMPUT 201: Practical Programming Methodology. Guohui Lin Department of Computing Science University of Alberta September 2018

A Fast Review of C Essentials Part II

Summer May 11, 2010

CODE TIME TECHNOLOGIES. Abassi RTOS MISRA-C:2004. Compliance Report

C: Program Structure. Department of Computer Science College of Engineering Boise State University. September 11, /13

Preprocessing directives are lines in your program that start with `#'. The `#' is followed by an identifier that is the directive name.

Macros and Preprocessor. CGS 3460, Lecture 39 Apr 17, 2006 Hen-I Yang

C: How to Program. Week /Mar/05

Errors During Compilation and Execution Background Information

C Programming for Engineers Functions

ME 461 C review Session Fall 2009 S. Keres

Week 2 C Fundamentals; Formatted Input/Output; int and float Types

Basic C Programming. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

A Crash Course in C. Steven Reeves

Full file at C How to Program, 6/e Multiple Choice Test Bank

Topic 6: A Quick Intro To C

INDEX. Figure I-0. Listing I-0. Table I-0. Symbols.DIRECTIVE (see Assembler directives)? preprocessor operator 3-34

Programming and Data Structures

A. Year / Module Semester Subject Topic 2016 / V 2 PCD Pointers, Preprocessors, DS

Tutorial 1: Introduction to C Computer Architecture and Systems Programming ( )

CSCI 171 Chapter Outlines

Course Outline Introduction to C-Programming

Chapter 2. Lexical Elements & Operators

Structured programming

Lecture 2: C Programming Basic

3 PREPROCESSOR. Overview. Listing 3-0. Table 3-0.

Writing an ANSI C Program Getting Ready to Program A First Program Variables, Expressions, and Assignments Initialization The Use of #define and

SEQUENTIAL STRUCTURE. Erkut ERDEM Hacettepe University October 2010

MA 511: Computer Programming Lecture 2: Partha Sarathi Mandal

EDIABAS BEST/2 LANGUAGE DESCRIPTION. VERSION 6b. Electronic Diagnostic Basic System EDIABAS - BEST/2 LANGUAGE DESCRIPTION

Lecture 10. Command line arguments Character handling library void* String manipulation (copying, searching, etc.)

Overview of C, Part 2. CSE 130: Introduction to Programming in C Stony Brook University

Programming for Engineers Functions

Appendix. Grammar. A.1 Introduction. A.2 Keywords. There is no worse danger for a teacher than to teach words instead of things.

EL2310 Scientific Programming

Chapter 2 - Introduction to C Programming

Tutorial No. 2 - Solution (Overview of C)

Lecture 5: C programming

Programming and Data Structure

C Preprocessor. Prabhat Kumar Padhy

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

ET156 Introduction to C Programming

Programming Fundamentals (CS 302 ) Dr. Ihsan Ullah. Lecturer Department of Computer Science & IT University of Balochistan

AN OVERVIEW OF C, PART 3. CSE 130: Introduction to Programming in C Stony Brook University

Unit 3. Operators. School of Science and Technology INTRODUCTION

CS240: Programming in C

Lecture 4 CSE July 1992

BSM540 Basics of C Language

Arithmetic type issues

JTSK Programming in C II C-Lab II. Lecture 1 & 2

Lecture 3. More About C

ANSI C Programming Simple Programs

(2½ Hours) [Total Marks: 75

Introduction to C Final Review Chapters 1-6 & 13

Computers Programming Course 5. Iulian Năstac

Functions. Functions are everywhere in C. Pallab Dasgupta Professor, Dept. of Computer Sc & Engg INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

Chapter 11 Introduction to Programming in C

UNIT- 3 Introduction to C++

BBM 101 Introduc/on to Programming I Fall 2014, Lecture 3. Aykut Erdem, Erkut Erdem, Fuat Akal

Final C Details. CSE 333 Autumn 2018

C Review. MaxMSP Developers Workshop Summer 2009 CNMAT

Chapter 11 Introduction to Programming in C

Transcription:

Programming for Engineers C Preprocessor ICEN 200 Spring 2018 Prof. Dola Saha 1

C Preprocessor The C preprocessor executes before a program is compiled. Some actions it performs are the inclusion of other files in the file being compiled, definition of symbolic constants and macros, conditional compilation of program code and conditional execution of preprocessor directives. Preprocessor directives begin with # and only whitespace characters and comments may appear before a preprocessor directive on a line. 2

#include Preprocessor Directive A copy of a specified file will be included in place of the directive Two forms: #include <filename> o Standard library headers o Searches pre-designated compiler and system directories #include filename o Programmer defined headers o Searches in the same directory as the file 3

Symbolic Constants The #define directive format is o #define identifier replacement-text All occurrences of identifier will be replaced by replacementtext automatically before the program is compiled. For example, #define PI 3.14159 replaces all subsequent occurrences of the symbolic constant PI with the numeric constant 3.14159. If the constant value needs to be modified throughout the program, it can be modified once in the preprocessor directive. BEST Practice: DO NOT use numbers inside the code, always use preprocessor directive to create constants. 4

Example code #include <stdio.h> #define FREEZING_PT 32.0f #define SCALE_FACTOR (5.0f / 9.0f) int main(void) { float fahrenheit, celsius; printf("enter Fahrenheit temperature: "); scanf("%f", &fahrenheit); celsius = (fahrenheit - FREEZING_PT) * SCALE_FACTOR; printf("celsius equivalent is: %.1f\n", celsius); } return 0; 5

After Preprocessing Blank line Blank line Lines brought in from stdio.h Blank line Blank line Blank line Blank line int main(void) { float fahrenheit, celsius; } printf("enter Fahrenheit temperature: "); scanf("%f", &fahrenheit); celsius = (fahrenheit - 32.0f) * (5.0f / 9.0f); printf("celsius equivalent is: %.1f\n", celsius); return 0; 6

Macro The macro-identifier is replaced in the program with the replacement-text before the program is compiled. Macros may be defined without arguments processed like a symbolic constant with arguments o the arguments are substituted in the replacement text o then the macro is expanded i.e., the replacement-text o replaces the identifier and argument list in the program. #define CIRCLE_AREA(x) ((PI) * (x) * (x)) 7

Example Macro #define CIRCLE_AREA(x) ((PI) * (x) * (x)) Wherever CIRCLE_AREA(y) appears in the file, the value of y is substituted for x in the replacement-text, the symbolic constant PI is replaced by its value (defined previously) and the macro is expanded in the program. Example Statement: area = CIRCLE_AREA(4); Expanded: area = ((3.14159) * (4) * (4)); Example Statement: area = CIRCLE_AREA(c + 2); Expanded: area = ((3.14159) * (c + 2) * (c + 2)); 8

Corresponding Function Function circlearea o double circlearea(double x) { return 3.14159 * x * x; } performs the same calculation as macro CIRCLE_AREA, but the function s argument is evaluated only once when the function is called. 9

Example MACRO with two arguments #define RECTANGLE_AREA(x, y) ((x) * (y)) Statement o rectarea = RECTANGLE_AREA(a + 4, b + 7); Expanded to o rectarea = ((a + 4) * (b + 7)); 10

Macro Error #define PRODUCT(x, y) (x * x) int result = PRODUCT(4,5) // returns 20 int result = PRODUCT(2+2,3+2) // returns 10 11

MACRO Error For example, if we call CIRCLE_AREA as follows: result = CIRCLE_AREA(++radius); the call to the macro CIRCLE_AREA is expanded to: result = ((3.14159) * (++radius) * (++radius)); which increments radius twice in the statement. In addition, the result of the preceding statement is undefined because C allows a variable to be modified only once in a statement. 12

MACRO Error An example that illustrates the need to put parentheses around a macro s replacement list: #define TWO_PI 2*3.14159 /* needs parentheses around replacement list */ During preprocessing, the statement conversion_factor = 360/TWO_PI; becomes conversion_factor = 360/2*3.14159; The division will be performed before the multiplication. 13

MACRO Error Each occurrence of a parameter in a macro s replacement list needs parentheses as well: #define SCALE(x) (x*10) /* needs parentheses around x */ During preprocessing, the statement j = SCALE(i+1); becomes j = (i+1*10); This statement is equivalent to j = i+10; 14

Multiline MACRO If the replacement text for a macro or symbolic constant is longer than the remainder of the line, a backslash (\) must be placed at the end of the line, indicating that the replacement text continues on the next line. #define NUMBERS 1, \ 2, \ 3 int x[] = { NUMBERS }; Expanded: int x[] = { 1, 2, 3 }; 15

#undef If a macro ceases to be useful, it may be undefined with the #undef directive. #undef takes a single argument, the name of the macro to undefine. Once a macro has been undefined, that identifier may be redefined as a macro by a subsequent #define directive. #define SIZE 10 #undef SIZE int x = SUM; // Error #define SIZE 100 16

Preprocessor Cast expressions, sizeof expressions and enumeration constants cannot be evaluated in preprocessor directives. Whitespace may appear, following are same MACRO #define FOUR (2 + 2) #define FOUR (2 + 2) #define FOUR (2 /* two */ + 2) 17

Conditional Compilation The conditional preprocessor construct is much like the if selection statement. Consider the following preprocessor code: o #if!defined(my_constant) #define MY_CONSTANT 0 #endif determines whether MY_CONSTANT is defined that is, whether MY_CONSTANT has already appeared in an earlier #define directive. 18

Conditional Compilation Every #if construct ends with #endif. Directives #ifdef and #ifndef are shorthand for #if defined(name) and #if!defined(name). A multiple-part conditional preprocessor construct may be tested by using the #elif (the equivalent of else if in an if statement) and the #else (the equivalent of else in an if statement) directives. These directives are frequently used to prevent header files from being included multiple times in the same source file. 19

Comment Code If the code contains multiline comments, /* and */ cannot be used to accomplish this task, because such comments cannot be nested. Instead, you can use the following preprocessor construct: o #if 0 code prevented from compiling #endif To enable the code to be compiled, replace the 0 in the preceding construct with 1. 20

Debug #ifdef DEBUG printf("variable x = %d\n", x); #endif causes a printf statement to be compiled in the program if the symbolic constant DEBUG has been defined (#define DEBUG) before directive #ifdef DEBUG. o 21

Predefined Symbolic Constants 22

Assertions The assert macro defined in the <assert.h> header tests the value of an expression at execution time. If the value of the expression is false (0), assert prints an error message and calls function abort (of the general utilities library <stdlib.h>) to terminate program execution. This is a useful debugging tool for testing whether a variable has a correct value. For example, suppose variable x should never be larger than 10 in a program. 23