C language + The Preprocessor. + Introduction The preprocessor is a program that processes that source code before it passes through the compiler. It.

Slides:



Advertisements
Similar presentations
Compilation and Debugging 101. Compilation in C/C++ hello.c Preprocessor Compiler stdio.h tmpXQ.i (C code) hello.o (object file)
Advertisements

Macro Processor.
Lecture 2 Introduction to C Programming
Introduction to C Programming
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Introduction to C Programming
CSc 352 An Introduction to the C Preprocessor Saumya Debray Dept. of Computer Science The University of Arizona, Tucson
COMP205 Comparative Programming Languages Part 1: Introduction to programming languages Lecture 3: Managing and reducing complexity, program processing.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
1 Chapter 4 The Fundamentals of VBA, Macros, and Command Bars.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 17 - The Preprocessor Outline 17.1Introduction 17.2The #include Preprocessor Directive 17.3The.
CS1061 C Programming Lecture 10: Macros, Casting and Intro. to Standard Library A. O’Riordan, 2004.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 13 - The Preprocessor Outline 13.1Introduction.
More about the C Preprocessor CS-2303, C-Term More about the C Preprocessor CS-2303 System Programming Concepts (Slides include materials from The.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 13 - The Preprocessor Outline 13.1Introduction 13.2The #include Preprocessor Directive 13.3The.
Introduction to C Programming
QUOTATION This chapter teaches you about a unique feature of the shell programming language: the way it interprets quote characters. Basically, the shell.
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.
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)
Chapter 4 Functions and Program Structure Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering.
Macros. There are three basic phases for C programming. preprocessing, compiling, and linking. C input file is first passed to a preprocessing program.
The Structure of a C++ Program. Outline 1. Separate Compilation 2. The # Preprocessor 3. Declarations and Definitions 4. Organizing Decls & Defs into.
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Introduction to C Programming Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010 Fall.
Introduction to Exception Handling and Defensive Programming.
Chapter 13 C Preprocessor C How to Program, 8/e ©2016 by Pearson Education, Inc., Hoboken, NJ. All Rights Reserved.
Computer Programming I Hour 2 - Writing Your First C Program.
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.
Engineering Computing I Chapter 4 Functions and Program Structure.
Computer Engineering Rabie A. Ramadan Lecture 5.
C How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Preprocessing Lecture 12 April 7, 2005.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
 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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
THE PREPROCESSOR
The Preprocessor Directives Introduction Preprocessing – Occurs before program compiled Inclusion of external files Definition of symbolic constants.
 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
C PREPROCESSOR. Introduction  It is a program that processes our source program before it is passed to the compiler.  Preprocessor commands (often known.
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
13 C Preprocessor.
CSC201: Computer Programming
Chapter 2 - Introduction to C Programming
INC 161 , CPE 100 Computer Programming
Chapter 13 - The Preprocessor
14. THE PREPROCESSOR.
Chapter 2 - Introduction to C Programming
Pre-processor Directives
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
C Preprocessor(CPP).
Preprocessor.
CSc 352 An Introduction to the C Preprocessor
Chapter 2 - Introduction to C Programming
C Preprocessor Seema Chandak.
Chapter 2 - Introduction to C Programming
Chapter 11: The Preprocessor
What Is? function predefined, programmer-defined
Conditional Compilation
Presentation transcript:

C language + The Preprocessor

+ Introduction The preprocessor is a program that processes that source code before it passes through the compiler. It operates under the control of directives. Directives are placed before the main lines. Directives begin with the symbol #. Preporecessor Compiler Linker Binary code DirectiveFunction #defineDefine a macro substituion #undefUndefines a macro #includeSpecifies the files to be include #ifdefTest for a macro definition #endifSpecifies the end of #if #ifndefTests whether a macro is not defined #ifTest a compile-time condition #elseSpecifies alternatives when #if test fails

+ Macro substituion Macro substitution is a process where an identifies in a program is replaced by predefined string composed of one or more tokens. #define identifier string The string may be any test The identifies must be a valid C name. Forms of macro substitution Simple macro substitution Argumented macro substitution Nested macro substitution.

+ Simple Macro substitution Simple string replacement is commonly used to define constants A definition, such as will replace all occurrences of M width 5 unchanged

+ Simple Macro substitution A macro definition can include more than simple constants value. Consider the evaluation of the equation Where D and A are macros defined as follows The result of the preprocessor’s substitution for D and A is Correct results can be obtained by using parentheses around the strings as

+ Simple Macro substitution The preprocessor preforms a literal text substitution, whenever the defined name occurs. Thus we can use a macro to define almost anything. Is this valid C program?

+ Simple Macro substitution The preprocessor preforms a literal text substitution, whenever the defined name occurs. Thus we can use a macro to define almost anything.

+ Macros with arguments The preprocessor permits us to define complex and more useful form of replacements. #define identifier(f1, f2,…,fn)string Example

+ Macros with arguments: Commonly used macros The argument supplied to a macro can be any series of characters. For example, the definition Can be called-in by The preprocessor will expand this as

+ Nesting Macros We can also use one macro in the definition of another macro. The preprocessor expands each #define macro, until no more macros appear in the text.

+ File inclusion An external file containing functions or macro definitions can be included as a part of a program so that we need no rewrite those functions or macro definitions. #include “filename” where filename is the name of the file containing the required definitions of functions. Double quotes specifies to search of the file first in the current directory and then in the standard directories. #include The file is searched only in the standard directories

+ File inclusion Let us assume we have created the following three files: syntax.ccontains syntax definitions. stat.ccontains statistical functions test.ccontains test functions We can make use of a definition or functions contained in any of these files by including them in the program as

+ Compiler control directives While developing large program, you may face one or more of the following situations 1. You have included a file containing some macro definitions. It is not known whether a particular macro (say, TEST) has been defined in that head file. However, you want to be certain that TEST is defined (or not defined). 2. Suppose a customer has two different types of computers and you are required to write a program that will run on both systems. You want to use the same program, although certain lines of code must be different for each system.

+ Compiler control directives 3. You are developing a program for selling in the open market. Some customers may insist on having certain additional features. However, you would like to have a single program that would satisfy both types of customers. 4. Suppose you are in the process of testing your program which is rather a large one. You would like to have print calls inserted in certain places to display intermediate results and messages in order to trace the flow of execution and errors, if any. You want these statements to be ‘active’ only when you decide so.

+ Situation 1 We want to ensure that the macro TEST is always defined, irrespective of whether it has been define in the header file or not. define.h is the header file that is supposed to contain the definition of TEST macro. The directive Searches for the definition of TEST in the header file and if not defined, then all the lines between the #ifndef and the coresponding #endif are left ‘active’ in the program.

+ Situation 1 In case, the TEST has been defined in the header file, #ifndef condition becomes false, therefore the directive #define TEST is ignored. Remember, you cannot simply write #define TEST 1 Because if TEST is already define, an error will occur. Similar is the case when we want the macro TEST never to be defined

+ Situation 2 The main concern here is to make the program portable. This can be achieved as follows: If we want the program to run on Windows, we include the directive

+ Situation 3 This is similar to previous situation and therefore the control directive take the following form Group-A lines are included if the customer ABC is defined. Otherwise, group-B lines are included.

+ Situation 4 Debugging and testing are done to detect errors in the program. The process of error detection and isolation begins with the testing of the program with known set of test data. The program is divided down and printf statements are placed in different parts to see intermediate results. (debugging statements). Once the errors are isolated and corrected we don’t need them. Thus, they can be deleted or alternatively make them inactive using control directive as