The Preprocessor.

Slides:



Advertisements
Similar presentations
Files in C Rohit Khokher.
Advertisements

The Preprocessor Underlying C Language Features Copyright © 2012 by Yong-Gu Lee
CSc 352 An Introduction to the C Preprocessor Saumya Debray Dept. of Computer Science The University of Arizona, Tucson
Structure of a C program
The C Preprocessor Yongjoon Lee April 22, What is it? Things with # –#include Processes the C source files BEFORE handing it to compiler. –`Pre`-process.
The Preprocessor Chapter 8 in ABC. #define PI #define C /* speed of light */ #define EOF (-1) #define MAXINT #define ITERS.
 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.
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.
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)
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
12 주 강의 The preprocessor. The use of #include #include #include “ filename ” ::: current directory  system-dependent places #include ::: system dependent.
Chapter 13 C Preprocessor C How to Program, 8/e ©2016 by Pearson Education, Inc., Hoboken, NJ. All Rights Reserved.
Computational Methods of Scientific Programming Lecture 8 Today’s lecture Start C/C++ Basic language features Web page
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.
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.
CGS 3460 Preprocessor n All preprocessor directives or commands begin with a #. lE.g. #include C program → Modified C program → Object Code n Can appear.
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.
UNIX and POSIX Standards
 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.
Lecture-3 Functions and Recursion. C Preprocessor Includes header files like stdio.h Expands macros defined Handles conditional compilations PreprocessorProcessor.
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
C language + The Preprocessor. + Introduction The preprocessor is a program that processes that source code before it passes through the compiler. It.
13 C Preprocessor.
CSCE 206 Structured Programming in C
What Is? function predefined, programmer-defined
Computer Science 210 Computer Organization
Predefined Macros (examples)
CISC105 – General Computer Science
INC 161 , CPE 100 Computer Programming
Chapter 13 - The Preprocessor
Advanced UNIX progamming
Introduction to C CSE 2031 Fall /3/ :33 AM.
Functions Separate Compilation
14. THE PREPROCESSOR.
A First Book of ANSI C Fourth Edition
Enumerations.
Pre-processor Directives
Computer Science 210 Computer Organization
Introduction to Programming
Learning Objectives What else in C++ Bitwise operator
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.
Homework Applied for cs240? (If not, keep at it!) 8/10 Done with HW1?
C Preprocessor Seema Chandak.
Chapter 11: The Preprocessor
What Is? function predefined, programmer-defined
Week 2 - Friday CS222.
Preprocessor Directives and Macros Chapter 21
Presentation transcript:

The Preprocessor

Preprocessor C lang. uses the preprocessor to expend its power and notation preprocessor directives lines beginning with a # communicates with the preprocessor #include #define

Use of #include #include <stdio.h> #include <stdlib.h> The preprocessor looks for the file only in other places and not in the current directory. Where the standard header files are stored is system-dependent. #include “filename” Search is made first in the current directory, and then in other system-dependent places

Use of #define The use of simple #define can improve program clarity #define identifier token_stringopt The preprocessor replaces every occurrence of identifier by token_string in the remainder of the file, except in quoted string. #define SECONDS_PER-DAY (60*60*24) #define PI 3.14159 #define C 299792.458 /*speed of light in km/sec */ #define EOF (-1) /*typical end-of-file value */ #define MAXINT 2147483647 /*largest 4-byte integer */ The use of simple #define can improve program clarity Program portability

Use of #define Syntactic sugar alter the syntax of C toward users’ preference #define EQ == while (i EQ 1) { …  while (i == 1) { …

Macros with Arguments #define can be used to write macro definitions with parameters #define identifier(identifier, …, identifier) token_stringopt #define SQ(x) ((x) * (x)) SQ(7 + w) SQ(SQ(*p)) #define SQ(x) x * x SQ(a + b) #define SQ(x) (x) * (x) 4 / SQ(2)  ((7 + w) * (7 + w))  ((((*p) * (*p))) * (((*p) * (*p)))) a + b * a + b  ((a + b) * (a + b))   4 / (2) * (2)  4 / ((2) * (2))

Macros with Arguments    #define SQ (x) ((x) * (x)) SQ(7) #define SQ(x) ((x) * (x)); /* a common error */ x = SQ(y); if (x == 2) else ++x;  (x) ((x) * (x)) (7)  x = ((y) * (y));;  x = ((y) * (y));;

Macros with Arguments Macros are frequently used to replace function calls by inline code More Efficient !!! #define min(x, y) (((x) < (y)) ? (x) : (y)) m = min(u, v); #define min4(a, b, c, d) min(min(a, b), min(c, d)) A macro definition can use both functions and macros in its body. #define SQ(x) ((x) * (x)) #define CUBE(x) (SQ(x) * (x)) #define F_POW(x) sqrt(sqrt(CUBE(x))) /* fractional power: 3/4 */  m = (((u) < (v)) ? (u) : (v));

Macros in stdio.h and ctype.h #define getchar() getc(stdin) #define putchar() putc((c), stdout) [ctype.h]

Macros in stdio.h and ctype.h c is a variable of integral type, such as char or int. The value of c stored in memory does not get changed

Conditional Compilation #define DEBUG 1 #if DEBUG printf(“debug: a=“%d”\n”, a); #endif #define DEBUG #ifdef DEBUG

Conditional Compilation #if #ifdef #ifndef #endif #undef identifier

Conditional Compilation #if defined(HP9000)||defined(SUN4) && !defined(VAX) /* machine-dependent code */ #endif     __linux__       Defined on Linux     __sun           Defined on Solaris     __FreeBSD__     Defined on FreeBSD     __NetBSD__      Defined on NetBSD     __OpenBSD__     Defined on OpenBSD     __APPLE__       Defined on Mac OS X     __hpux          Defined on HP-UX     __osf__         Defined on Tru64 UNIX (formerly DEC OSF1)     __sgi           Defined on Irix     _AIX            Defined on AIX

Predefined Macros (ANSI C) __DATE__ 매크로가 치환되는 순간의 날짜 (문자열) __TIME__ 매크로가 치환되는 순간의 시간 (문자열) __STDC__ ANSI 표준을 따르는지 여부 (0 또는 1) __FILE__ 매크로가 포함된 source file __LINE__ source file에서 line number current file name current line number