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

Similar documents
Chapter 7: Preprocessing Directives

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

Conditional Compilation

Unit 4 Preprocessor Directives

OBJECT ORIENTED PROGRAMMING USING C++

Programming for Engineers C Preprocessor

Errors During Compilation and Execution Background Information

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

Appendix A. The Preprocessor

C Preprocessor. Prabhat Kumar Padhy

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

fpp: Fortran preprocessor March 9, 2009

A Fast Review of C Essentials Part II

Problem Solving and 'C' Programming

COMsW Introduction to Computer Programming in C

The Compilation Process

Practical C Issues:! Preprocessor Directives, Multi-file Development, Makefiles. CS449 Fall 2017

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

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

C for Engineers and Scientists: An Interpretive Approach. Chapter 14: File Processing


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

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

Contents Lecture 3. C Preprocessor, Chapter 11 Declarations, Chapter 8. Jonas Skeppstedt Lecture / 44

Intermediate Programming, Spring 2017*

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

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

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

The New C Standard (Excerpted material)

Columbus Schema for C/C++ Preprocessing

CS 326 Operating Systems C Programming. Greg Benson Department of Computer Science University of San Francisco

Topic 6: A Quick Intro To C

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

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

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

#include. Practical C Issues: #define. #define Macros. Example. #if

COSC 2P91. Bringing it all together... Week 4b. Brock University. Brock University (Week 4b) Bringing it all together... 1 / 22

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

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #54. Organizing Code in multiple files

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

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

Lecture 2: C Programming Basic

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

The Make Utility. Independent compilation. Large programs are difficult to maintain. Problem solved by breaking the program into separate files

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

CMPSC 311- Introduction to Systems Programming Module: Build Processing

PROGRAMMAZIONE I A.A. 2017/2018

Summer May 11, 2010

The C Preprocessor. Richard M. Stallman, Zachary Weinberg. For gcc version (GCC)

MODULE 10 PREPROCESSOR DIRECTIVES

CSE 374 Programming Concepts & Tools. Brandon Myers Winter 2015 C: Linked list, casts, the rest of the preprocessor (Thanks to Hal Perkins)

The C Preprocessor. Richard M. Stallman, Zachary Weinberg. For gcc version (pre-release) (GCC)

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

CMPSC 311- Introduction to Systems Programming Module: Build Processing

Compiler, Assembler, and Linker

More on C programming

ISO/IEC JTC1/SC22/WG5 N1247

SISTEMI EMBEDDED. The C Pre-processor Fixed-size integer types Bit Manipulation. Federico Baronti Last version:

The Make Utility. Independent compilation. Large programs are difficult to maintain. Problem solved by breaking the program into separate files

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

CS240: Programming in C

C PROGRAMMING LANGUAGE. POINTERS, ARRAYS, OPERATORS AND LOOP. CAAM 519, CHAPTER5

IAR Embedded Workbench MISRA C:2004. Reference Guide

Macros in C/C++ Computer Science and Engineering College of Engineering The Ohio State University. Lecture 33

SOFTWARE ARCHITECTURE 5. COMPILER

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

SISTEMI EMBEDDED. The C Pre-processor Fixed-size integer types Bit Manipulation. Federico Baronti Last version:

C Programming Review CSC 4320/6320

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

The C Preprocessor. for GCC version Richard M. Stallman Zachary Weinberg

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

CSE 374 Programming Concepts & Tools

Inno Setup Preprocessor: Introduction

The C Preprocessor (and more)!

CSE 374 Programming Concepts & Tools

SISTEMI EMBEDDED. The C Pre-processor Fixed-size integer types Bit Manipulation. Federico Baronti Last version:

MODULE 5: Pointers, Preprocessor Directives and Data Structures

Unit 1: Introduction to C Language. Saurabh Khatri Lecturer Department of Computer Technology VIT, Pune

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

CS3157: Advanced Programming. Outline

Lesson 5: Functions and Libraries. EE3490E: Programming S1 2018/2019 Dr. Đào Trung Kiên Hanoi Univ. of Science and Technology

Model Viva Questions for Programming in C lab

CSCI 171 Chapter Outlines

CSCI 123 Introduction to Programming Concepts in C++

File I/O. Preprocessor Macros

Slide Set 5. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

Object-Oriented Programming

Conduite de Projet Cours 4 The C build process

IMPORTANT QUESTIONS IN C FOR THE INTERVIEW

Operating Systems History and Overview

Modules:Context-Sensitive Keyword

#include <stdio.h> int main() { printf ("hello class\n"); return 0; }

CS410. Note: VuGujranwala.com is not responsible for any solved solution, but honestly we are trying our best to Guide correctly.

Bitwise Operators. Fall Jinkyu Jeong GEBD029: Basis and Practice in Programming Fall 2014 Jinkyu Jeong

Motivation was to facilitate development of systems software, especially OS development.

University of Passau Department of Informatics and Mathematics Chair for Programming. Bachelor Thesis. Andreas Janker

Intro to the Preprocessor

Compiler Construction

Enabling Variability-Aware Software Tools. Paul Gazzillo New York University

COMP322 - Introduction to C++

Transcription:

Chapter 7: Preprocessing Directives

Introduction Preprocessing Affect program preprocessing and execution Capabilities Inclusion of additional C source files Definition of symbolic constants and macros Conditional preprocessing of a program Format of preprocessing directives A preprocessing directive consists of a sequence of preprocessing tokens that begins with a pound sign #, which must be the first non-space character on the line. Some preprocessing directives are listed below.

Directive #define #elif #else #endif #error Description Define a preprocessor macro. Alternatively include some text based on the value of another expression, if the previous #if, #ifdef, #ifndef, or #leif test failed. Alternatively include some text, if the previous #if, #ifdef, #ifndef, or #elif test failed. Terminate conditional text. Produce a compile-time error with a designated message. #if Conditionally include text, based on the value of an expression. #ifdef #ifndef #include #line #pragma Conditionally include text, based on whether a macro name is defined. Conditionally include text, based on if a name is not a defined macro. Insert text from another source file. Give a line number for message. Compiler/interpreter specific features, not in C standard # Null directive defined Preprocessing operator that yields 1 if a name is defined as a preprocessing macro and 0 otherwies; used in #if and #elif.

7.1 Symbolic Constants and Macros The #define Preprocessing Directive This preprocessing directive is used to create symbolic constants and macros. Form #define identifier replacement-list defines an object-like macro that causes each subsequent instance of the macro names to be replaced by the replacement-list of preprocessing tokens that constitute the remainder of the directive. The new-line is a character that terminates the #define preprocessing directive.

Symbolic constants The simple form of macro is particularly useful for introducing named constants into a program. It allows for easier modification of the constants later on.when programs are processed, all occurrences of symbolic constants indicated by identifier are replaced by the replacement-list. Example: #define BLOCK_SIZE 0x100 we can write int size = BLOCK_SIZE; instead of int size = 0x100; in the program. Note: Cannot redefine symbolic constants with different values by multiple #define statements

A preprocessing directive of the form #define identifier(identifier-list-opt) replacement-list new-line defines a function-like macro with arguments, similar syntactically to a function call. The parameters are specified by the optional list of identifiers. Example: if a macro mul with two arguments is defined by #define mul(x,y) ((x)*(y)) then the source program line result = mul(5, a+b); is replaced with result = ((5)*(a+b));

NOTE: Parentheses are important in macro definitions. Example: If macro mul is defined as #define mul(x,y) (x*y) The statement result = mul(5, a+b); in the source program becomes result = (5*a+b); The evaluation will be incorrect.

#undef Undefine a symbolic constant or macro, which can later be redefined. Example: #define mul(x,y) ((x)*(y)) /* */ #undef mul int mul; /* mul can be used after it is undefined */

We ve covered Section 7.1 Read and understand Program 7.1 Solve in notebook end-of-chapter problems 3 4 5

7.2 Predefined Macros See Table 7.2 on p.234 LINE FILE DATE TIME STDC STDC_VERSION Not defined in Visual Studio

7.2 Predefined Macros For the preprocessor, multi-line statements MUST be split with \ Hard-to-find syntax error!! This is func in C99, but in Visual Studio it must be spelled like this

7.3 Source File Inclusion The #include Preprocessing Directive 1)#include <header.h> Searches standard library for header file and replaces the directive by the entire contents of the file. In Ch, the header is searched according to the paths specified by the the system variable _ipath. C compilers in Unix will typically search the header file in the directory /usr/include. In Visual C++, the header file is searched based on the paths in the environment variable INCLUDE. or cl I C:/home/assount/include program.c Used for standard library files 2) #include "header.h" C compilers and interpreters will first search the header file in the same directory where the file is being processed, which typically is the current directory. Then search the header file in the paths as if it was included by #include <header.h>.

See programming example

7.4 Conditional Preprocessing and Compilation Enables the user to control the compilation of the program, screen out portions of source code that are not to be compiled. Structure is similar to if and else statement in C. Conditional preprocessing directives #if, #else, #elif, and #endif

Example: #if defined(_hpux_) printf( I am using HP-UX\n ); #elif defined(_win32_) printf( I am using Windows\n); #endif

Preprocessing directives of the forms # ifdef identifier # ifndef identifier check whether the identifier is or is not currently defined as a macro name. #ifdef identifier is the short form of #if defined(identifier) #ifndef identifier is the short form of #if!defined(identifier)

Comment out a segment of code Comment out code segment which contains /*... */ Use following format to comment out the segment of code double d = some_func(); #ifdef JUNK /* This code segment will be commented out */ printf( d = %f\n, d); #endif The code segment will be commented out when JUNK is not defined, To uncomment the code segment, define JUNK or remove #ifdef JUNK and #endif.

To include a header file in a program only once, it is typically handled using the combination of the following preprocessing directives #ifndef, #define, and #endif. For example, a header file header.h may consist of the following code fragment. #ifndef HEADER_H #define HEADER_H #endif

SKIP Sections 7.5, 7.6, 7.7, 7.8, 7.9

7.10 pragma directive Signals some platform-dependent functionality, e.g.

Sample Problem: The system in Figure1 (a) consists of a single body with mass m moving on a horizontal surface. An external force p acts on the body. The coefficient of kinetic friction between body and horizontal surface is. The freebody diagram for the system is shown in Figure1 (b). Figure1: The system diagram and FBD of a sample problem

Program 1: /* File: accelmacro.c */ #include <stdio.h> #define M_G 9.81 #define FORCE(t) (4*(sin(t)-3)+20) #define ACCEL(p, mu, m) (((p)-(mu)*(m)*m_g)/(m)) int main() { double a, p, mu, m, t; } mu = 0.2; m = 5.0; t = 2.0; p = FORCE(t); a = ACCEL(p, mu, m); // or a = ACCEL(FORCE(t), mu, m); printf("acceleration a = %f (m/s^2)\n", a); return 0; Output: Acceleration = 1.364823 (m/s^2)

Program 2: /* File: accelhead.c */ #include <stdio.h> /* local header file */ #include "accel.h" int main() { /* declare variables */ double a, mu, m, t; /* File: accel.h */ #ifndef ACCEL_H #define ACCEL_H #define M_G 9.81 double force(double t); double accel(double t, double mu, double m); #endif } /* Initialize variables */ mu = 0.2; m = 5.0; t = 2.0; /* processing */ a = accel(t, mu, m); /* display the output and termination */ printf("acceleration a = %f (m/s^2)\n", a); return 0; double force(double t) { double p; } p = 4*(sin(t)-3)+20; return p; double accel(double t, double mu, double m) { double a, p; } p = force(t); a = (p-mu*m*m_g)/m; return a;

Homework for Ch.7 Due Monday, Oct 25 End of chapter problems 9 10 14