C Preprocessor Seema Chandak.

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

Files in C Rohit Khokher.
The Preprocessor Underlying C Language Features Copyright © 2012 by Yong-Gu Lee
Chapter 13 & 14 C Preprocessor and Other C Topics Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education.
. Compilation / Pointers Debugging 101. Compilation in C/C++ hello.c Preprocessor Compiler stdio.h tmpXQ.i (C code) hello.o (object file)
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 17 - The Preprocessor Outline 17.1Introduction 17.2The #include Preprocessor Directive 17.3The.
© 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.
1 CSE 303 Lecture 13b The C preprocessor reading: Programming in C Ch. 13 slides created by Marty Stepp
The Preprocessor #include #define N 10 C program Preprocessor Modified C program Preprocessor Object codes.
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.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 CMPE-013/L Pre-Processor Commands Gabriel Hugh Elkaim Spring 2013.
Object-Oriented Programming in C++
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)
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.
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 C Preprocessor C How to Program, 8/e ©2016 by Pearson Education, Inc., Hoboken, NJ. All Rights Reserved.
Programming Practice (6) - Sorting Algorithms
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.
L function n predefined, programmer-defined l arguments, (formal) parameters l return value l function call, function invocation l function definition.
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.
C Programming Day 3. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Storage Class Specifiers Every variable in a C.
 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.
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
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.
C language + The Preprocessor. + Introduction The preprocessor is a program that processes that source code before it passes through the compiler. It.
Pre-Compiler Directives for TTCN-3 Ina Schieferdecker in response to CR5089 and 5090.
1 Building a program in C: Preprocessor, Compilation and Linkage.
Programming in C Project Organization Compiler Directives.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Template, Preprocessing Lecture 9 November 2, 2004.
Silberschatz and Galvin  C Programming Language Kingdom of Saudi Arabia Ministry of Higher Education Al-Majma’ah University College of Education.
COM S 326X Deep C Programming for the 21st Century Prof. Rozier
13 C Preprocessor.
What Is? function predefined, programmer-defined
CISC105 – General Computer Science
INC 161 , CPE 100 Computer Programming
Separate Compilation and Namespaces
Chapter 13 - The Preprocessor
Functions Separate Compilation
14. THE PREPROCESSOR.
Programmer-Defined Functions, Call-by-Value, Multiple Files Lab 5
Pre-processor Directives
Introduction to Programming
Preprocessor C program → Modified C program → Object Code
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).
Preprocessor.
Programming in C Miscellaneous Topics.
CSc 352 An Introduction to the C Preprocessor
Programming in C Miscellaneous Topics.
Lab guide # 12 Review C Preprocessor And Q&A.
C Programming Language
Chapter 11: The Preprocessor
What Is? function predefined, programmer-defined
Conditional Compilation
Preprocessor Directives and Macros Chapter 21
Presentation transcript:

C Preprocessor Seema Chandak

What is PREPROCESSING ? A separate compilation step before the compiler tries to “understand” your program What is PREPROCESSOR Statement Simple text substitution “On the fly”

Features of C preprocessor Preprocessing Occurs before program compiled Inclusion of external files Definition of symbolic constants Macros Conditional compilation Conditional execution All directives begin with # Can only have whitespace before directives Directives not C statements Do not end with ;

The #include Preprocessor Directive #include directive Puts copy of file in place of directive Seen many times in example code Two forms #include <filename> For standard library header files Searches predestinated directories #include "filename" Searches in current directory Normally used for programmer-defined files

The #include Preprocessor Directive Usage Loading header files #include <iostream> Programs with multiple source files Header file Has common declarations and definitions Classes, structures, enumerations, function prototypes Extract commonality of multiple program files

#define The #define Preprocessor Directive: Symbolic Constants Constants represented as symbols When program compiled, all occurrences replaced Format #define identifier replacement-text #define PI 3.14159 Everything to right of identifier replaces text #define PI=3.14159 Replaces PI with "=3.14159" Probably an error Cannot redefine symbolic constants

The #define Preprocessor Directive: Symbolic Constants Advantages Takes no memory Disadvantages Name not be seen by debugger (only replacement text) Do not have specific data type const variables preferred

Macro The #define Preprocessor Directive: Macros Operation specified in #define Intended for legacy C programs Macro without arguments Treated like a symbolic constant Macro with arguments Arguments substituted for replacement text Macro expanded Performs a text substitution No data type checking

Example The #define Preprocessor Directive: Macros Declaration of macro #define CIRCLE_AREA( x ) ( PI * ( x ) * ( x ) ) Calling macro area = CIRCLE_AREA( 4 ); Becomes area = ( 3.14159 * ( 4 ) * ( 4 ) ); Use parentheses very important Without them #define CIRCLE_AREA( x ) PI * x * x Call will area = CIRCLE_AREA( c + 2 ); area = 3.14159 * c + 2 * c + 2; Which evaluates incorrectly

Example # define M(x) x*x main() { printf(“%d”,M(2+3)); } What is the output ? Why ?

((x+1) > (y-2) ? (x+1) : (y-2)) Macro Substitution with Arguments #define NAME(A, B) repl_text with A & B Defines a macro in which text of argument A is inserted into repl_text wherever parameter A occurs text of argument B is inserted into repl_text wherever parameter B occurs Example:– #define max(A, B) ((A) > (B) ? (A) : (B)) Replace max(x+1, y-2) with ((x+1) > (y-2) ? (x+1) : (y-2))

Multiple arguments : #undef The #define Preprocessor Directive: Macros #define RECTANGLE_AREA( x, y ) ( ( x ) * ( y ) ) rectArea = RECTANGLE_AREA( a + 4, b + 7 ); Becomes rectArea = ( ( a + 4 ) * ( b + 7 ) ); #undef Undefined symbolic constant or macro Can later be redefined

Control preprocessor directives and compilation Conditional Compilation Control preprocessor directives and compilation Cannot evaluate cast expressions, sizeof, enumeration constants Structure similar to if #if !defined( NULL ) #define NULL 0 #endif Determines if symbolic constant NULL defined If NULL defined, defined( NULL ) evaluates to 1 #define statement skipped Otherwise #define statement used Every #if ends with #endif

Can use else Abbreviations Conditional Compilation #else #elif is "else if" Abbreviations #ifdef short for #if defined(name) #ifndef short for #if !defined(name)

Examples :: #include<stdio.h> #define a 10 main() { } int a = 50; printf(“\n\t %d”,a); } #incldue <stdio.h> #define square(X) X*X main() { int y ; y= 30/square(4); printf(“\n\t %d “, y); }