Conditional Compilation

Similar documents
Chapter 7: Preprocessing Directives

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

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

Errors During Compilation and Execution Background Information

Programming for Engineers C Preprocessor

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

Appendix A. The Preprocessor

A Fast Review of C Essentials Part II

Problem Solving and 'C' Programming

OBJECT ORIENTED PROGRAMMING USING C++

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

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

COMsW Introduction to Computer Programming in C

fpp: Fortran preprocessor March 9, 2009

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

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

Unit 4 Preprocessor Directives

The Preprocessor. CS 2022: Introduction to C. Instructor: Hussam Abu-Libdeh. Cornell University (based on slides by Saikat Guha) Fall 2011, Lecture 9

C Preprocessor. Prabhat Kumar Padhy

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

Intermediate Programming, Spring 2017*

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

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

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

TypeChef: Towards Correct Variability Analysis of Unpreprocessed C Code for Software Product Lines

Summer May 11, 2010

CS240: Programming in C

MODULE 5: Pointers, Preprocessor Directives and Data Structures

CSE 374 Programming Concepts & Tools

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

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

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

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

Columbus Schema for C/C++ Preprocessing

Intro to the Preprocessor

More on C programming

Lecture 2: C Programming Basic

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

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

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

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

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

Lecture 19 Notes Data Structures in C

EL6483: Brief Overview of C Programming Language

Bachelor Thesis. Configuration Lifting for C Software Product Lines

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

Topic 6: A Quick Intro To C

The Compilation Process

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

Functions. Cedric Saule

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

A Crash Course in C. Steven Reeves

CMPSC 311- Introduction to Systems Programming Module: Build Processing

The C Preprocessor (and more)!

C Formatting Guidelines

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

Modifiers. int foo(int x) { static int y=0; /* value of y is saved */ y = x + y + 7; /* across invocations of foo */ return y; }

Compiler, Assembler, and Linker

C Formatted IO. Day16.C. Young W. Lim. December 9, 2017

Programming. Projects with Multiple Files

The C Preprocessor Compiling and Linking Using MAKE

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

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

CMPE-013/L. Expressions and Control

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

CMPSC 311- Introduction to Systems Programming Module: Build Processing

PROGRAMMAZIONE I A.A. 2017/2018


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

The New C Standard (Excerpted material)

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

CSE 333 Autumn 2014 Midterm Key

Embedded Systems and Software

CS113: Lecture 7. Topics: The C Preprocessor. I/O, Streams, Files

Chapter IV Introduction to C for Java programmers

Figure 1 Common Sub Expression Optimization Example

Operating Systems History and Overview

CSE 333 Midterm Exam 2/12/16. Name UW ID#

CS3157: Advanced Programming. Outline

This homework is due by 11:59:59 PM on Thursday, October 12, 2017.

0x0d2C May your signals all trap May your references be bounded All memory aligned Floats to ints round. remember...

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

An Overview of ROMS Code. Kate Hedstrom, ARSC January 2011

Chapter 11 Introduction to Programming in C

Friday, September 16, Lab Notes. Command line arguments More pre-processor options Programs: Finish Program 1, begin Program 2 due next week

4.2 Machine-Independent Macro Processor Features

Compiler Theory. (GCC the GNU Compiler Collection) Sandro Spina 2009

Chapter 11 Introduction to Programming in C

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

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

Practical C Programming

Software Engineering /48

Definition Checklist for Source Statement Counts

Operator overloading

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

make and makefile CS 211

1 You seek performance 3

COMP322 - Introduction to C++

CSci 4061 Introduction to Operating Systems. Programs in C/Unix

CSCI 123 Introduction to Programming Concepts in C++

Transcription:

Conditional Compilation printf() statements cab be inserted code for the purpose of displaying debug information during program testing. Once the program is debugged and accepted as "working'', it is desirable to remove these debug statements. Of course, if later an undetected bug appears during program use, we would like to put some or all debug statements back in the code to pinpoint and fix the bug.

Conditional Compilation One approach to this is to simply "comment out'' the debug statements; i.e. surround them with comment markers, so that if they are needed again, they can be "uncommented''. This is a vast improvement over removing them and later having to type them back. However, this approach does require going through the entire source file(s) to find all of the debug statements and comment or uncomment them

Conditional Compilation Use conditional compilation to select particular sections of code to compile, while excluding other sections. Conditional compilation statements are designed to run during compile time, not at run time.

Conditional Compilation A pre-processor directive in C is identified by the presence of a #symbol before the statement. The pre-processor substitutes language code for all the directives, and passes the newly inflated file to the compiler. We would expect, using conditional compilation, that some code would be left out, but this is not always the case

Cause the preprocessor to conditionally suppress the compilation of portions of source code. These directives test a constant expression or an identifier to determine which tokens the preprocessor should pass on to the compiler and which tokens should be bypassed during preprocessing.

The directives are: #if and #elif - compare the value of constant_expression to zero: #ifdef - checks for the existence of macro definitions #else - If the condition specified in the #if, #ifdef, or #ifndef directive evaluates to 0, and the conditional compilation directive contains a preprocessor #else directive, the lines of code located between the preprocessor #else directive and the preprocessor #endif directive is selected by the preprocessor to be passed on to the compiler.

The directives are: #ifndef - checks whether a macro is not defined #endif - ends the conditional compilation directive

The preprocessor conditional compilation directive spans several lines: The condition specification line (beginning with #if, #ifdef, or #ifndef) Lines containing code that the preprocessor passes on to the compiler if the condition evaluates to a nonzero value (optional) The #elif line (optional) The preprocessor #endif directive For each #if, #ifdef, and #ifndef directive, there are zero or more #elif directives, zero or one #else directive, and one matching #endif directive

Example: Suppose we want to create a project with a debug build, by using conditional compilation to include statements for debug output: #define a debug macro at the start of the file, and then test for it before evaluating debug statements. If this technique spans multiple files, it would probably be useful to define a compiler flag -DDEBUG, rather than have to include the #define statement at the start of each file.

Once defined, we could then use this debug flag as follows: #ifdef DEBUG printf("x is %d\n", x); #endif

Preventing Multiple #includes using Conditional Compilation To avoid conflicts that occur when the pre-processor #includes the same header file twice, and there are duplicate definitions of certain constants, macros or functions, the following code can be used: #ifndef HEADER_H #include "header.h" #define HEADER_H #endif