Pre-processor Directives

Slides:



Advertisements
Similar presentations
Files in C Rohit Khokher.
Advertisements

Lecture 2 Introduction to C Programming
Introduction to C Programming
CSc 352 An Introduction to the C Preprocessor Saumya Debray Dept. of Computer Science The University of Arizona, Tucson
 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.
 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
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.
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.
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.
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.
CSCI 171 Presentation 8 Built-in Functions, Preprocessor Directives, and Macros.
L function n predefined, programmer-defined l arguments, (formal) parameters l return value l function call, function invocation l function definition.
1 Preprocessor –How it works File Inclusion: #include Macro Definition: #define Predefined macros –__LINE__, __FILE__, __DATE__, __TIME__ Conditional Compilation.
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.
 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.
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.
What Is? function predefined, programmer-defined
User-Written Functions
Chapter 2 - Introduction to C Programming
INC 161 , CPE 100 Computer Programming
Separate Compilation and Namespaces
Chapter 13 - The Preprocessor
Functions Separate Compilation
14. THE PREPROCESSOR.
Chapter 2 - Introduction to C Programming
User-Defined Functions
Preprocessor C program → Modified C program → Object Code
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
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
Chapter 2 - Introduction to C Programming
Programming in C Miscellaneous Topics.
C Preprocessor Seema Chandak.
Chapter 2 - Introduction to C Programming
C Programming Language
Chapter 11: The Preprocessor
What Is? function predefined, programmer-defined
CSE 341 Lecture 22 Macros; extending Scheme's syntax
Introduction to C Programming
Conditional Compilation
Preprocessor Directives and Macros Chapter 21
Presentation transcript:

Pre-processor Directives Shatrughan Modi

Introduction Before a C program is compiled in a compiler, source code is processed by a program called preprocessor. This process is called preprocessing. The C preprocessor modifies a source code file before handing it over to the compiler. There are essentially three uses of the preprocessor directives constants macros Directives always begin with a sharp sign (#).

Introduction Sr. No. Preprocessor Syntax Description 1 Macro #define This macro defines constant value and can be any of the basic data types. 2 Header file inclusion #include <file_name> The source code of the file “file_name” is included in the main program at the specified place 3 Conditional compilation #ifdef, #endif, #if, #else,  #ifndef Set of commands are included or excluded in source program before compilation with respect to the condition 4 Other directives #undef, #pragma #undef is used to undefine a defined macro variable. #Pragma is used to call a function before and after main function in a C program

#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 predesignated directories #include "filename" Searches in current directory Normally used for programmer-defined files

#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 predesignated directories #include "filename" Searches in current directory Normally used for programmer-defined files Usage Loading header files #include <iostream> Programs with multiple source files

The #define Preprocessor Directive: Symbolic Constants Constants represented as symbols When program compiled, all occurrences replaced Format #define identifier replacement-text E.g.: #define PI 3.14159 Advantages Takes no memory Disadvantages Name not be seen by debugger (only replacement text) Do not have specific data type

The #define Preprocessor Directive: Symbolic Constants #undef Undefines symbolic constant or macro Can later be redefined If you are defining a constant in terms of a mathematical expression, it is wise to surround the entire value in parentheses: #define PI_PLUS_ONE (3.14 + 1) By doing so, you avoid the possibility that an order of operations issue will destroy the meaning of your constant: x = PI_PLUS_ONE * 5; Without parentheses, the above would be converted to x = 3.14 + 1 * 5;

The #define Preprocessor Directive: Macros A macro definition is usually of the following form: #define MACRO_NAME(arg1, arg2, ...) [code to expand to] For instance, a simple increment macro might look like this: #define INCREMENT(x) x++ They look a lot like function calls, but they're not It is inlined directly into the code, so there isn't any function call overhead

The #define Preprocessor Directive: Macros Remember that the exact text of the macro argument is "pasted in" to the macro. For instance, if you wrote something like this: #define MULT(x, y) x * y and then wrote int z = MULT(3 + 2, 4 + 2); what value do you expect z to end up with?

The #define Preprocessor Directive: Macros The obvious answer, 30, is wrong! That's because what happens when the macro MULT expands is that it looks like this: int z = 3 + 2 * 4 + 2; // 2 * 4 will be evaluated first! So z would end up with the value 13! The way to avoid it is to force the arguments themselves to be evaluated before the rest of the macro body. #define MULT(x, y) (x) * (y) // now MULT(3 + 2, 4 + 2) will expand to (3 + 2) * (4 + 2)

The #define Preprocessor Directive: Multiline macros By using a the "\" to indicate a line continuation, we can write our macros across multiple lines to make them a bit more readable. For instance, we could rewrite swap as #define SWAP(a, b) { \ int temp = b; \ b = a; \ a = temp; \ }

The #define Preprocessor Directive: Example #include <stdio.h> #define height 100 #define PI 3.14 #define letter 'A' #define Circle_Area(r) (PI*r*r) void main() { printf("value of height : %d \n", height ); printf("value of PI : %f \n", PI); printf("value of letter : %c \n", letter ); printf(“Circle’s area with radius 5: %f\n”, Circle_Area(5)); } value of height : 100 value of PI : 3.140000 value of letter : A Circle’s area with radius 5: 78.50000

Conditional Compilation There are a whole set of options that can be used to determine whether the preprocessor will remove lines of code before handing the file to the compiler. The #if directive takes a numerical argument that evaluates to true if it's non-zero. If its argument is false, then code until the closing #else, #elif, or #endif will be excluded.

Conditional Compilation: Example #include <stdio.h> #define a 100 int main() { #if (a==100) printf(“This line will be added in this C file if a = 100\n"); #elif (a==50) printf(“This line will be added in this C file if a = 50\n"); #else printf(“This line will be added in this C file if a is neither equal to100 nor 50 \n"); #endif return 0; } This line will be added in this C file if a = 100

Conditional Compilation: Commenting out Code The code inside #if 0 and #endif will not be included in C file for compilation. #if 0 // Enter the code which you want to comment out #endif

Include Guards Another common problem is that a header file is required in multiple other header files that are later included into a source code file, with the result often being that variables, structures, classes or functions appear to be defined multiple times. This can result in a lot of compile-time headaches. The preprocessor provides an easy technique for ensuring that any given file is included once and only once. By using the #ifndef directive, you can include a block of text only if a particular expression is undefined; then, within the header file, you can define the expression. This ensures that the code in the #ifndef is included only the first time the file is loaded.

Include Guards The above code can be used in defining header files. So when we include a header file it will get included only once. #ifndef _FILE_NAME_H_ #define _FILE_NAME_H_ /* code */ #endif

Include Guards: Another Example #include <stdio.h> #define RAJU 100 int main() { #ifndef SELVA { printf("SELVA is not defined. So, now we are going to " \ "define here\n"); #define SELVA 300 } #else printf("SELVA is already defined in the program”); #endif return 0; } SELVA is not defined. So, now we are going to define here

#pragma directive Pragma is used to call a function before and after main function in a C program. Sr. No. Pragma command Description 1 #Pragma startup <function_name_1> This directive executes function named “function_name_1” before 2 #Pragma exit <function_name_2> This directive executes function named “function_name_2” just before termination of the program 3 #pragma warn – rvl If function doesn’t return a value, then warnings are suppressed by this directive while compiling 4 #pragma warn – par If function doesn’t use passed function parameter , then warnings are suppressed 5 #pragma warn – rch If a non reachable code is written inside a program, such warnings are suppressed by this directive.

Include Guards: Another Example #include <stdio.h> void function1( ); void function2(); #pragma startup function1 #pragma exit function2 int main( ) { printf ( "\n Now we are in main function" ); return 0; } void function1( ) { printf("\nFunction1 is called before main function call"); void function2( ) { printf ( "\nFunction2 is called just before end of main function" ) ;“ Function1 is called before main function call Now we are in main function Function2 is called just before end of main function