Compiler Directives. The C Preprocessor u The C preprocessor (cpp) changes your source code based on instructions, or preprocessor directives, embedded.

Slides:



Advertisements
Similar presentations
CS 11 C track: lecture 7 Last week: structs, typedef, linked lists This week: hash tables more on the C preprocessor extern const.
Advertisements

Pemrograman C Risanuri Hidayat. Why C  Compact, fast, and powerful  “Mid-level” Language  Standard for program development (wide acceptance)  It is.
Introduction to C Programming
CSc 352 An Introduction to the C Preprocessor Saumya Debray Dept. of Computer Science The University of Arizona, Tucson
Writing and Testing Programs Drivers and Stubs Supplement to text.
CS1061 C Programming Lecture 2: A Few Simple Programs A. O’Riordan, 2004.
 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.
CS1061 C Programming Lecture 10: Macros, Casting and Intro. to Standard Library A. O’Riordan, 2004.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 13 - The Preprocessor Outline 13.1Introduction.
1. 2 FUNCTION INLINE FUNCTION DIFFERENCE BETWEEN FUNCTION AND INLINE FUNCTION CONCLUSION 3.
 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
Basic Elements of C++ Chapter 2.
The Preprocessor #include #define N 10 C program Preprocessor Modified C program Preprocessor Object codes.
C Programming Lecture 5. Precedence and Associativity of Operators b Rules of associativity and precedence of operators determine precisely how expressions.
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 preprocessor and the compilation process COP3275 – PROGRAMMING USING C DIEGO J. RIVERA-GUTIERREZ.
Fortran- Subprograms Chapters 6, 7 in your Fortran book.
Windows Programming Lecture 05. Preprocessor Preprocessor Directives Preprocessor directives are instructions for compiler.
C++ for Engineers and Scientists Third Edition
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single.
Chapter 6: Modularity Using Functions. In this chapter, you will learn about: – Function and parameter declarations – Returning a single value – Returning.
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.
Week 2 - Friday.  What did we talk about last time?  Base systems  C literals  Representations in memory.
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.
Engineering Computing I Chapter 4 Functions and Program Structure.
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
CSCI 171 Presentation 8 Built-in Functions, Preprocessor Directives, and Macros.
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.
Dr. Mark L. HornickCS-1030 Dr. Mark Hornick 1 C++ Global functions Declarations & Definitions Preprocessor Directives.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Preprocessor Midterm Review Lecture 7 Feb 17, 2004.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
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.
2. C FUNDAMENTALS. Example: Printing a Message /* Illustrates comments, strings, and the printf function */ #include int main(void) { printf("To C, or.
Pointers: Basics. 2 Address vs. Value Each memory cell has an address associated with it
COM S 326X Deep C Programming for the 21st Century Prof. Rozier
13 C Preprocessor.
INC 161 , CPE 100 Computer Programming
Chapter 13 - The Preprocessor
Functions Separate Compilation
14. THE PREPROCESSOR.
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.
File Input and Output.
CSc 352 An Introduction to the C Preprocessor
C Preprocessor Seema Chandak.
Chapter 1 c++ structure C++ Input / Output
Conditional Compilation
Week 2 - Friday CS222.
Preprocessor Directives and Macros Chapter 21
Presentation transcript:

Compiler Directives

The C Preprocessor u The C preprocessor (cpp) changes your source code based on instructions, or preprocessor directives, embedded in the source code. u The preprocessor creates a “new” version of your program and it is this new program that actually gets compiled. –Normally, you do not see these “new” versions on the hard disk, as they are deleted after compilation. –You can force the compiler to keep them to see the results. u Each preprocessor directive appears in the source code proceeded by a ‘#’ sign.

The #define Directive u Simple substitution Macros #define text1 text2 u This tells the compiler to find all occurrences of “text1” in the source code and substitute “text2”. u Usually used for constants: #define MAX 1000 –Generally use upper case letters (by convention). –Always separate by white space. –No trailing semi-colon (think about it...) u An example: –#define PRINT printf PRINT(“hello, world”);

Function Macros u You can also define more complex macros: #define max(a,b) ( (a) > (b) ? (a) : (b) ) …… printf("%d", 2 * max(3+3, 7)); /* is equivalent to */ printf("%d", 2 * ( (3+3) > (7) ? (3+3) : (7) ); u The parentheses are important! For example: #define max(a,b) a>b?a:b printf("%d", 2 * max(3+3, 7)); /*is equivalent to */ printf("%d", 2 * 3+3 > 7 ? 3+3 : 7 );

Function Macros Should be Used with Care #define max(x,y) ((x)>(y)?(x):(y)) …… int n, i=4, j=3; n= max( i++, j); /* Same as n= (( i++ )>( j )?( i++ ):( j )) */ printf("%d,%d,%d", n, i, j); u The output is: –5, 6, 3 u If max was a function, the output would have been: –4, 5, 3

Conditional Compilation (1) u The pre-processor directives #if, #elif, #else, and #endif tell the compiler is the enclosed source code should be compiled u Can create more efficient and more portable code. –Compiled to match the environment it is compiled for. u Structure: #if condition_1 statement_block_1 #elif condition_2 statement_block_2... #elif condition_n statement_block_n #else default_statement_block #endif Any Constant Expression non-zero is true compile statement_block_1 zero is false don't compile statement_block_1

Conditional Compilation (2) u For the most part, the only things that can be tested are the things that can be defined by #define statements. u An example: #define ENGLAND 0 #define FRANCE 1 #define ITALY 0 #if ENGLAND #include "england.h" #elif FRANCE #include "france.h" #elif ITALY #include "italy.h" #else #include "canada.h" #endif

Conditional Compilation (3) u Conditional compilation can also be very useful for including “debugging code” –When you are debugging your code you probably print out some information during the running of your program. –However, you may not need want these extra print outs when you release your program. So, you need to go back through your code and delete them. u Instead, you can use #if #endif to save you time: #define DEBUG 1 …… #if DEBUG printf("Debug reporting at function my_sort()!\n"); #endif ……

Conditional Compilation (4) u Usually people use a preprocessor function as the condition of compilation: defined ( NAME ) v Returns true if NAME has been defined; else false u An example: #define DEBUG #if defined ( DEBUG ) printf("debug report at function my_sort() \n"); #endif u Note: This only depends on if DEBUG has been defined. But has nothing to do with which value DEBUG is defined to. u Can also use the notation #ifdef NAME instead.

Conditional Compilation (5) u The #undef … directive makes sure that defined( …) evaluates to false. u An example: –Suppose at the first part of a source file, you want DEBUG to be defined. At the last part of the file, however, you want DEBUG to be undefined… u A directive can also be set on the Unix command line at compile time: cc –DDEBUG myprog.c v Compiles myprog.c with the symbol DEBUG defined as if #define DEBUG was in written at the top of myprog.c.

The #include Directive u We've seen lots of these already. u This directive causes all of the code in the included file to be inserted at the point in the text where #include appears. u The included files can contain other #include directive. –Usually limited to 10 levels of nesting u tell the compiler to look in the standard include directories first. u " " tells the compiler to treat this as a Unix filename. –Relative to directory containing file if a relative pathname. –Relative to root with an absolute pathname. –But most compilers also search for the standard include directory if it cannot find the file at the specified path.

Inline Functions (1) u Recall the two different ways to compute the maximum number between two integers: –#define max(a,b) ((a)>(b)? (a):(b)) –int max(int a, int b) { return a>b?a:b; } u Function calls need to jump to another part of your program and jump back when done. This needs to: –Save current registers. –Allocate memory on the stack for the local variables in the function that is called. –Other overhead …… u Therefore, the macro approach is often more efficient, since it does not have function call overhead. –But, this approach can be dangerous, as we saw earlier.

Inline Functions (2) u Modern C and C++ compilers provide “inline” functions to solve the problem: –Put the inline keyword before the function header. inline int max(int a, int b) { return a>b?a:b; } u You then use it as a normal function in your source code. –printf( "%d", max( x, y) ); u When the compiler compiles your program, it will not compile it as a function. Rather, it just integrates the necessary code in the line that max() is called in to avoid an actual function call. –The above printf(…) is compiled to be something like: –printf("%d", x>y?x:y);

Inline Functions (3) u Writing the small but often-used functions as inline functions can improve the speed of your program. u A small problem in doing so is that you have to include the inline function definition before you use it in a file. –For normal functions, only the function prototypes are needed. u Therefore, inline functions are often defined in header (.h) files. –Once you include the header file, you can use v Inline functions whose definitions are in that header file. v Normal functions whose prototypes are in that header file. u Another small problem is that some debuggers get confused when handling inline functions -- sometimes it is best to inline functions after debugging is finished.