Windows Programming Lecture 05. Preprocessor Preprocessor Directives Preprocessor directives are instructions for compiler.

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.
Lecture 2 Introduction to C Programming
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
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.
CS1061 C Programming Lecture 10: Macros, Casting and Intro. to Standard Library A. O’Riordan, 2004.
© 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
1 CSE 303 Lecture 13b The C preprocessor reading: Programming in C Ch. 13 slides created by Marty Stepp
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.
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.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 CMPE-013/L Pre-Processor Commands Gabriel Hugh Elkaim Spring 2013.
 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.
C Programming Lecture 4 : Variables , Data Types
Sales person receive RM200/week plus 9% of their gross sales for that week. Write an algorithms to calculate the sales person’s earning from the input.
CMSC 104, Version 9/011 Introduction to C Topics Compilation Using the gcc Compiler The Anatomy of a C Program 104 C Programming Standards and Indentation.
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.
Structure of a C program Preprocessor directive (header file) Program statement } Preprocessor directive Global variable declaration Comments Local variable.
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.
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.
1 A simple C++ program // ======================================================= // File:helloworld.cpp // Author:Vana Doufexi // Date:1/4/2006 // Description:Displays.
Created by Harry H. Cheng,  2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach Chapter 7: Preprocessing.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Preprocessor Midterm Review Lecture 7 Feb 17, 2004.
Programming Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types.
Compiler Directives. The C Preprocessor u The C preprocessor (cpp) changes your source code based on instructions, or preprocessor directives, embedded.
Operating System Discussion Section. The Basics of C Reference: Lecture note 2 and 3 notes.html.
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.
Windows Programming Lecture 06. Data Types Classification Data types are classified in two categories that is, – those data types which stores decimal.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Template, Preprocessing Lecture 9 November 2, 2004.
COM S 326X Deep C Programming for the 21st Century Prof. Rozier
13 C Preprocessor.
Chapter Topics The Basics of a C++ Program Data Types
ANNOUNCEMENT The missed lecture will be made up this Monday evening in the Tech PC classroom (MG51). A tentative time interval is 6:30-8:00. The exact.
INC 161 , CPE 100 Computer Programming
Chapter 2, Part I Introduction to C Programming
Basic Elements of C++.
Chapter 13 - The Preprocessor
14. THE PREPROCESSOR.
Pre-processor Directives
Basic Elements of C++ Chapter 2.
C Preprocessor(CPP).
Preprocessor.
CSc 352 An Introduction to the C Preprocessor
C Preprocessor Seema Chandak.
C Programming Language
Macro A fragment of code which has been given a name.
Conditional Compilation
Preprocessor Directives and Macros Chapter 21
Presentation transcript:

Windows Programming Lecture 05

Preprocessor

Preprocessor Directives Preprocessor directives are instructions for compiler.

Preprocesor Directives In C Preprocessor commands are called directives. All preprocessor directives start with #.

Examples of Preprocessor Directives #define#error#undef#include #if#else#elif#endif #ifdef#line#ifndef#pragma

#define directive #define directive defines an identifier. #define ARRAY_SIZE 10 Preprocessor directive identifier token string / value

#define ARRAY_SIZE 10 Before Preprocessing char array[ARRAY_SIZE]; After Preprocessing char array[10];

identifier is not replaced if it appears in a comment within a string, or as part of a longer identifier

#define directive If token-string is omitted, identifier is removed from source file. #define ARRAY_SIZE

#define directive #define WIDTH 10 #define HEIGHT WIDTH+25 HEIGHT WIDTH is replaced with

#define directive identifier remains defined and can be tested It can be checked using #if and #ifdef directives. #if defined is same as #ifdef.

Code Example #include #define ARRAY_SIZE 10 // preprocessor is defined main() { #ifdef ARRAY_SIZE // Testing preprocessor is defined or not cout<<ARRAY_SIZE; #endif getch(); }

#if, #elif, #else, and #endif #if VERSION > 5 #define SHIPMENT 1 #if MORE_MEM == 1 #define SIZE 200 #else #define SIZE 100 #endif #else #define SHIPMENT 0 #if MORE_MEM == 1 #define SIZE 100 #else #define SIZE 50 #endif #endif

#if, #elif, #else #elif is same as #else #if

Preprocessor Operators The defined is a preprocessor operator. #if defined MAX is equivalent to #ifdef MAX Preprocessor operator Preprocessor Directive

Preprocessor Operators #if !defined MAX is equivalent to #ifndef MAX

#error directive 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. You would use #error inside of a conditional statment that detects a combination of parameters which you know the program does not properly support.

#error directive #if !defined(__cplusplus) #error Must use C++ language #endif

#undef Directive If an identifier ceases to be useful, it may be undefined with the #undef directive. #undef takes a single argument, the name of the identifier to undefine. It is an error if anything appears on the line after the identifier name. #undef has no effect if the name is not an identifier. #define FOO 4 x = FOO; ==> x = 4; #undef FOO x = FOO; ==> x = FOO;

#undef Directive Once an identifier has been undefined, that identifier may be redefined by a subsequent #define directive. The new definition need not have any resemblance to the old definition.

#undef Directive #define SPEED ………… some code here…………… … … … … … … … #undef SPEED ifndef(SPEED) #error Speed not defined #endif

# Null Directive The null directive consists of a # followed by a Newline, with only white space (including comments) in between. A null directive is understood as a preprocessing directive but has no effect on the preprocessor output. The primary significance of the existence of the null directive is that an input line consisting of just a # will produce no output, rather than a line of output containing just a #. Supposedly some old C programs contain such lines.

# Null Directive # empty line

Macros What is a macro ? A simple macro always stands for exactly the same text, each time it is used. Macros can be more flexible when they accept arguments. Arguments are fragments of code that you supply each time the macro is used. These fragments are included in the expansion of the macro according to the directions in the macro definition.

Macros To define a macro that takes arguments, you use the #define command with a list of parameters in parentheses after the name of the macro. The parameters may be any valid C identifiers separated by commas at the top level (that is, commas that aren't within parentheses) and, optionally, by white-space characters. The left parenthesis must follow the macro name immediately, with no space in between. For example, here's a macro that computes the minimum of two numeric values: #define min(X, Y) ((X)<(Y) ? (X):(Y))

Macros #define SUM(a, b) a+b int total; total = 3*SUM(10, 20) total = 3*10+20 #define SUM(a, b) (a+b) #define rect_area(a, b) a*b rect_area(10, 20) rect_area(5+5, 10+10)  5+5*10+10 #define rect_area(a, b) (a)*(b) #define rect_area(a, b) ((a)*(b))

Standard Predefined Macros The standard predefined macros are specified by the C language standards, so they are available with all compilers that implement those standards. Older compilers may not provide all of them. Their names all start with double underscores.

Standard Predefined Macros __FILE__ This macro expands to the name of the current input file, in the form of a C string constant. This is the path by which the preprocessor opened the file, not the short name specified in #include or as the input file name argument. For example, "/usr/local/include/myheader.h" is a possible expansion of this macro. __LINE__ This macro expands to the current input line number, in the form of a decimal integer constant. While we call it a predefined macro, it's a pretty strange macro, since its "definition" changes with each new line of source code.

Standard Predefined Macros __FILE__ name of the current source file. String surrounded by quoutes char ch[]= __FILE__ ; char ch[]= “Polygons.c”; __LINE__ current line number in the source file, decimal integer constant. int a=__LINE__; int a=582;

Conditional Compilation Conditional compilation is useful for things like machine-dependencies, debugging, and for setting certain options at compile-time.

Conditional Compilation #if defined TESTING if(i > 0) { displayMessages(); } #else writeMessageToFile(); #endif

#include Both user and system header files are included using the preprocessing directive #include. It has two variants: #include This variant is used for system header files. It searches for a file named file in a list of directories specified by you, then in a standard list of system directories. The parsing of this form of #include is slightly special because comments are not recognized within the. Thus, in #include the /* does not start a comment and the directive specifies inclusion of a system header file named x/*y. The argument file may not contain a > character. It may, however, contain a < character.

#include #include "file" –This variant is used for header files of your own program. It searches for a file named file first in the current directory, then in the same directories used for system header files. The current directory is the directory of the current input file. It is tried first because it is presumed to be the location of the files that the current input file refers to. If backslashes occur within file, they are considered ordinary text characters, not escape characters. None of the character escape sequences appropriate to string constants in C are processed. Thus, #include "x\n\\y" specifies a filename containing three backslashes. It is not clear why this behavior is ever useful, but the ANSI standard specifies it.

#pragma Directive This #pragma directive allows a directive to be defined. Its effects are implementation-defined. If the pragma is not supported, then it is ignored. The #pragma directive is the method specified by the C standard for providing additional information to the compiler, beyond what is conveyed in the language itself.

#pragma Directive It has the general form: #pragma charatcetr_sequence where character_sequence is a series of characters giving a specific compiler instruction and arguments, if any. The character_sequence on a pragma is not subject to macro substitutions. More than one pragma construct can be specified on a single #pragma directive. The compiler ignores unrecognized pragmas.

#pragma Directive #pragma warning(disable:4001) Disbale warning number 4001

#pragma Directive #pragma warning( once:4385 ) Issue warning 4385 only once #pragma warning( error:164 ) Report warning 164 as an error