C Preprocessor(CPP).

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

Compilation and Debugging 101. Compilation in C/C++ hello.c Preprocessor Compiler stdio.h tmpXQ.i (C code) hello.o (object file)
Files in C Rohit Khokher.
Chapter 13 & 14 C Preprocessor and Other C Topics Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education.
CSc 352 An Introduction to the C Preprocessor Saumya Debray Dept. of Computer Science The University of Arizona, Tucson
 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.
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
Basic Elements of C++ Chapter 2.
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.
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.
Introduction to C Programming CE Lecture 7 Compiler options and makefiles.
Lecture #5 Introduction to C++
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.
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.
Program in Multiple Files. l all C++ statements are divided into executable and non-executable l executable - some corresponding machine code is generated.
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.
 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.
13 C Preprocessor.
Chapter Topics The Basics of a C++ Program Data Types
What Is? function predefined, programmer-defined
Computer Science 210 Computer Organization
CISC105 – General Computer Science
INC 161 , CPE 100 Computer Programming
Separate Compilation and Namespaces
Basic Elements of C++.
Chapter 13 - The Preprocessor
Functions Separate Compilation
14. THE PREPROCESSOR.
Pre-processor Directives
Basic Elements of C++ Chapter 2.
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,
Preprocessor.
Programming in C Miscellaneous Topics.
CSc 352 An Introduction to the C Preprocessor
Programming in C Miscellaneous Topics.
C++ Compilation Model C++ is a compiled language
C Preprocessor Seema Chandak.
Lab guide # 12 Review C Preprocessor And Q&A.
C Programming Language
C++ Programming Basics
What Is? function predefined, programmer-defined
Conditional Compilation
Presentation transcript:

C Preprocessor(CPP)

CPP The C preprocessor (cpp) is the preprocessor for the C and C++ computer programming languages. The preprocessor handles directives for source file inclusion (#include), macro definitions (#define), and conditional inclusion (#if) Preprocessor directives are lines included in the code of our programs that are not program statements but directives for the preprocessor. These lines are always preceded by a hash sign (#)

Facilities provided The C preprocessor provides four separate facilities: Inclusion of header files. These are files of declarations that can be substituted into your program. Macro expansion. You can define macros. Conditional compilation. You can include or exclude parts of the program according to various conditions. Line control. If you use a program to combine or rearrange source files into an intermediate file which is then compiled, you can use line control to inform the compiler of where each source line originally came from.

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

Cont.. Usage Loading header files Programs with multiple source 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

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

Cont.. Advantages Disadvantages const variables preferred Takes no memory Disadvantages Name not be seen by debugger (only replacement text) Do not have specific data type const variables preferred

The #define Preprocessor Directive: Macros A macro is a sort of abbreviation which you can define once and then use later. Simple Macros It is a name which stands for a fragment of code. Before you can use a macro, you must define it explicitly with the `#define' directive. `#define' is followed by the name of the macro and then the code it should be an abbreviation for. For example,  #define BUFFER_SIZE 1020 - defines a macro named `BUFFER_SIZE' as an abbreviation for the text `1020'. Eg:  foo = (char *) xmalloc (BUFFER_SIZE); then the C preprocessor will recognize and expand the macro `BUFFER_SIZE', resulting in foo = (char *) xmalloc (1020);

Conditional Compilation A conditional in the C preprocessor begins with a conditional directive: `#if', `#ifdef' or `#ifndef'. The `#if' Directive - Basic conditionals using `#if' and `#endif'. The `#else' Directive - Including some text if the condition fails. The `#elif' Directive - Testing several alternative possibilities.

Forms of directives The `#if' Directive #if expression controlled text #endif /* expression */ The `#else' Directive The `#else' directive can be added to a conditional to provide alternative text to be used if the condition is false.   #if expression text-if-true #else /* Not expression */ text-if-false #endif /* Not expression */

The `#error‘ Directives The directive `#error' causes the preprocessor to report a fatal error. The rest of the line that follows `#error' is used as the error message. The line must consist of complete tokens. Tokens are groups of characters separated by spaces Eg: #error 1 - Out of range error

The `#warning‘ directive The directive `#warning' is like the directive `#error', but causes the preprocessor to issue a warning and continue preprocessing. The rest of the line that follows `#warning' is used as the warning message.

Line Numbers One of the jobs of the C preprocessor is to inform the C compiler of where each line of C code came from: which source file and which line number. `#line' is a directive that specifies the original line number and source file name for subsequent input in the current preprocessor input file.  `#line' has three variants: #line linenum #line linenum filename #line anything else - anything else is checked for macro calls, which are expanded. The result should be a decimal integer constant

Assertions Assertions are a more systematic alternative to macros in writing conditionals to test what sort of computer or system the compiled program will run on. Assertions are usually predefined, but you can define them with preprocessing directives or command-line options.