C PREPROCESSOR. Introduction  It is a program that processes our source program before it is passed to the compiler.  Preprocessor commands (often known.

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

Programming In C++ Spring Semester 2013 Lecture 11 Programming In C++, Lecture 11 By Umer Rana.
Files in C Rohit Khokher.
Programming III SPRING 2015 School of Computer and Information Sciences Francisco R. Ortega, Ph.D. McKnight Fellow and GAANN Fellow LECTURE #3 Control.
Introduction to C Programming
CSc 352 An Introduction to the C Preprocessor Saumya Debray Dept. of Computer Science The University of Arizona, Tucson
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 12 Separate Compilation Namespaces Simple Make Files (Ignore all class references.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
 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.
CS 202 Computer Science II Lab Fall 2009 October 1.
 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
1 CSE 303 Lecture 13b The C preprocessor reading: Programming in C Ch. 13 slides created by Marty Stepp
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.
21 August (B.V.Patel institute of BMC & IT) STRUCTURE AND UNIONS.
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.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 CMPE-013/L Pre-Processor Commands Gabriel Hugh Elkaim Spring 2013.
1 CSC103: Introduction to Computer and Programming Lecture No 26.
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.
Chapter 13 C Preprocessor C How to Program, 8/e ©2016 by Pearson Education, Inc., Hoboken, NJ. All Rights Reserved.
C Hints and Tips The preprocessor and other fun toys.
Engineering Computing I Chapter 4 Functions and Program Structure.
CSCI 171 Presentation 8 Built-in Functions, Preprocessor Directives, and Macros.
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.
Created by Harry H. Cheng,  2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach Chapter 7: Preprocessing.
Dr. Mark L. HornickCS-1030 Dr. Mark Hornick 1 C++ Global functions Declarations & Definitions Preprocessor Directives.
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 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.
Programming in C Project Organization Compiler Directives.
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.
Unit 10 Code Reuse. Key Concepts Abstraction Header files Implementation files Storage classes Exit function Conditional compilation Command-line arguments.
COM S 326X Deep C Programming for the 21st Century Prof. Rozier
13 C Preprocessor.
CISC105 – General Computer Science
INC 161 , CPE 100 Computer Programming
Separate Compilation and Namespaces
Chapter 13 - The Preprocessor
14. THE PREPROCESSOR.
KALINGA INSTITUTE OF INDUSTRIAL TECHNOLOGY
Pre-processor Directives
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.
C Preprocessor Seema Chandak.
C Programming Language
Chapter 11: The Preprocessor
Conditional Compilation
Preprocessor Directives and Macros Chapter 21
Presentation transcript:

C PREPROCESSOR

Introduction  It is a program that processes our source program before it is passed to the compiler.  Preprocessor commands (often known as directives)  Preprocessor directives begin with a # symbol  Directives can be placed anywhere in a program  Often placed at the beginning of a program

Preprocessor Directives  Macro expansion  File inclusion  Conditional compilation  Miscellaneous directives

Macro Expansion #define UPPER 25 main( ) { int i ; for ( i = 1 ; i <= UPPER ; i++ ) printf ( "\n%d", i ) ; } MACRO DEFINITION During preprocessing, the preprocessor replaces every occurrence of UPPER in the program with 25 MACRO EXPANSIONS’

Example #define AND && #define ARANGE ( a > 25 AND a < 50 ) main( ) { int a = 30 ; if ( ARANGE ) printf ( "within range" ) ; else printf ( "out of range" ) ; }

Macros with Arguments #define AREA(x) ( 3.14 * x * x ) main( ) { float r1 = 6.25, r2 = 2.5, a ; a = AREA ( r1 ) ; printf ( "\nArea of circle = %f", a ) ; a = AREA ( r2 ) ; printf ( "\nArea of circle = %f", a ) ; } output of the program... Area of circle = Area of circle = output of the program... Area of circle = Area of circle =

Guess the output #define SQUARE(n) n * n main( ) { int j ; j = 64 / SQUARE ( 4 ) ; printf ( "j = %d", j ) ; }

Macros can be split into multiple lines #define HLINE for ( i = 0 ; i < 79 ; i++ ) \ printf ( "%c", 196 ) ; #define VLINE( X, Y ) {\ gotoxy ( X, Y ) ; \ printf ( "%c", 179 ) ; \ }

Macro Vs Function MACROFUNCTION In a macro call the preprocessor replaces the macro template with its macro expansion, In a function call the control is passed to a function along with certain arguments, some calculations are performed in the function and a useful value is returned back from the function. Usually macros make the program run faster but increase the program size functions make the program smaller and compact

File Inclusion #include "filename“ Entire contents of filename to be inserted into the source code  #include "goto.c"  This command would look for the file goto.c in the current directory as well as the specified list of directories as mentioned in the include search path that might have been set up.  #include  This command would look for the file goto.c in the specified list of directories only.

Conditional Compilation #ifdef macroname statement 1 ; statement 2 ; statement 3 ; #endif

Conditional Compilation /* myfile.h */ #ifndef __myfile_h #define __myfile_h myfunc( ) { /* some code */ } #endif First time the file ‘myfile.h’ gets included the preprocessor checks whether a macro called __myfile_h has been defined or not. If it has not been then it gets defined and the rest of the code gets included. Next time we attempt to include the same file, the inclusion is prevented since __myfile_h already stands defined.

#if and #elif Directives  The #if directive can be used to test whether an expression evaluates to a nonzero value or not.  If the result of the expression is nonzero, then subsequent lines upto a #else, #elif or #endif are compiled, otherwise they are skipped.

Example #define TEST 6 main( ) { #if TEST <= 5 statement 1 ; statement 2 ; statement 3 ; #else statement 4 ; statement 5 ; statement 6 ; #endif }

Miscellaneous Directives  #undef Directive  On some occasions it may be desirable to cause a defined name to become ‘undefined’.  #undef PENTIUM would cause the definition of PENTIUM to be removed from the system.

Miscellaneous Directives  #pragma Directive  can use to turn on or off certain features  Pragmas vary from one compiler to another  #pragma startup and #pragma exit  These directives allow us to specify functions that are called upon program startup (before main( )) or program exit (just before the program terminates).

Miscellaneous Directives void fun1( ) ; void fun2( ) ; #pragma startup fun1 #pragma exit fun2 main( ) { printf ( "\nInside maim" ) ; } Inside fun1 Inside main Inside fun2 Inside fun1 Inside main Inside fun2 void fun1( ) { printf ( "\nInside fun1" ) ; } void fun2( ) { printf ( "\nInside fun2" ) ; }

Miscellaneous Directives  #pragma warn  This directive tells the compiler whether or not we want to suppress a specific warning #pragma warn –rvl /* return value */ #pragma warn –par /* parameter not used */ #pragma warn –rch /* unreachable code */ int f1( ) { int a = 5 ; } void f2 ( int x ) { printf ( "\nInside f2" ) ; } int f3( ) { int x = 6 ; return x ; x++ ; } void main( ) { f1( ) ; f2 ( 7 ) ; f3( ) ; }

Quiz 1) What is a preprocessor directive A) a message from compiler to the programmer B) a message from compiler to the linker C) a message from programmer to the preprocessor D) a message from programmer to the microprocessor

Quiz 2) Which of the following are correctly formed A) #define statements: #define INCH PER FEET 12 B) #define SQR (X) ( X * X ) C) #define SQR(X) X * X D) #define SQR(X) ( X * X )

Quiz 3) State True or False: 1. A macro must always be written in capital letters. 2. A macro should always be accommodated in a single line. 3. After preprocessing when the program is sent for compilation the macros are removed from the expanded source code. 4. Macros with arguments are not allowed. 5. Nested macros are allowed. 6. In a macro call the control is passed to the macro.

Quiz 4) A header file is: A) A file that contains standard library functions B) A file that contains definitions and macros C) A file that contains user - defined functions D) A file that is present in current working directory

Quiz 5) Which of the following is not a preprocessor directive A) #if B) #elseif C) #undef D) #pragma

Quiz 6) All macro substitutions in a program are done A) Before compilation of the program B) After compilation C) During execution D) None of the above

Quiz 7) In a program the statement: #include "filename" is replaced by the contents of the file “filename” A) Before compilation B) After Compilation C) During execution D) None of the above