The Preprocessor #include #define N 10 C program Preprocessor Modified C program Preprocessor Object codes.

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

THE PREPROCESSOR. Preprocessing is (apparently) done before actual compilation begins. The preprocessor doesnt know (very much) C. Major kinds of preprocessor.
Compilation and Debugging 101. Compilation in C/C++ hello.c Preprocessor Compiler stdio.h tmpXQ.i (C code) hello.o (object file)
Makefiles  Provide a way for separate compilation.  Describe the dependencies among the project files.  The make utility.
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.
Programming C/C++ on Eclipe Trình bày: Ths HungNM C/C++ Training.
Guide To UNIX Using Linux Third Edition
 2000 Prentice Hall, Inc. All rights reserved. Chapter 13 - The Preprocessor Outline 13.1Introduction 13.2The #include Preprocessor Directive 13.3The.
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.
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 15: Writing Large Programs Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 15 Writing Large Programs.
Copyright 2001 Oxford Consulting, Ltd1 January Storage Classes, Scope and Linkage Overview Focus is on the structure of a C++ program with –Multiple.
Macros. There are three basic phases for C programming. preprocessing, compiling, and linking. C input file is first passed to a preprocessing program.
Introduction to C Programming CE Lecture 7 Compiler options and makefiles.
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.
Engineering Computing I Chapter 4 Functions and Program Structure.
Compilation & Linking Computer Organization I 1 November 2009 © McQuain, Feng & Ribbens The Preprocessor When a C compiler is invoked, the.
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.
Writing a Run Time DLL The application loads the DLL using LoadLibrary() or LoadLibraryEx(). The standard search sequence is used by the operating system.
CSE 251 Dr. Charles B. Owen Programming in C1 Compilation and Makefiles.
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.
15. WRITING LARGE PROGRAMS. Source Files A program may be divided into any number of source files. Source files have the extension.c by convention. Source.
 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.
C code organization CSE 2451 Rong Shi. Topics C code organization Linking Header files Makefiles.
Chapter 14: The Preprocessor Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 14 The Preprocessor.
 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.
Problem Session 4 Header Files and Unix. Where are they? You may need to examine the header file to determine what the data structures and functions provided.
Chapter 15: Writing Large Programs Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 15 Writing Large Programs.
L071 Introduction to C Topics Compilation Using the gcc Compiler The Anatomy of a C Program Reading Sections
Programming in C Project Organization Compiler Directives.
COM S 326X Deep C Programming for the 21st Century Prof. Rozier
13 C Preprocessor.
What Is? function predefined, programmer-defined
Computer Science 210 Computer Organization
INC 161 , CPE 100 Computer Programming
Chapter 13 - The Preprocessor
14. THE PREPROCESSOR.
Writing Large Programs
The Preprocessor Based on Chapter 1 in C++ for Java Programmers by Weiss When a C compiler is invoked, the first thing that happens is that the code is.
Computer Science 210 Computer Organization
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,
Computer Science 210 Computer Organization
C Preprocessor(CPP).
Exam 4 review Copyright © 2008 W. W. Norton & Company.
Programming in C Miscellaneous Topics.
CSc 352 An Introduction to the C Preprocessor
Programming in C Miscellaneous Topics.
Writing Large Programs
C Preprocessor Seema Chandak.
What Is? function predefined, programmer-defined
Compiler vs linker The compiler translates one .c file into a .o file
Conditional Compilation
SPL – PS1 Introduction to C++.
Presentation transcript:

The Preprocessor #include #define N 10 C program Preprocessor Modified C program Preprocessor Object codes

Caution The preprocessor has only a limited knowledge of C. As a result, it’s quite capable of creating illegal programs as it executes directives.

Preprocessing Directives Macro definition: #define to defines a macro; the #undefine to removes a macro definition. File inclusion: #include causes the content of a specified file to be included in a program. Conditional compilatoin: #if, #ifdef, #ifndef, #elif, #else and #endif allow blocks of text to be either included in or excluded from a program, depending on conditions that can be tested by the preprocessor.

Macro Definitions Simple Macros: #define identifier replacement-list #define N = 100 …. int a[N]; #define N 100; …. int a[N];

Simple Macro Simple macros are primarily used for defining “manifest constants”: giving names to numeric, character, and string values: #define STR_LEN 80 #define TRUE 1 #define FALSE 0 #define PI #define CR ‘\r’ #define EOS ‘\0’ #define MEM_ERR “Error: not enough memory”

Simple Macro---advantages It makes programs easier to read It makes programs easier to modify It helps avoid inconsistencies and typographical errors Making minor changes to the syntax of C Renaming types: #define BOOL int Controlling conditional compilation

Parameterized Macros #define identifier(x1, x2, …, xn ) replacement-list #define MAX(x, y) ((x) > (y) ? (x) : (y)) #define IS_EVEN(n) ((n)%2 == 0) #define TOUPPER(c) (‘a’<=(c)&&(c)<=‘z’?(c) –’a’+’A’:(c))

Parameterized Macros--- advantages The program may be slightly faster Macros are “generic”

Parameterized Macros--- disadvantages The compiled code will often be larger n = MAX(I, MAX(j, k)); n = ((i)>(((j)>(k)?(j):(k)))?(i):(((j)>(k)?(j):(k)))); Arguments aren’t type-checked It’s not possible to have a pointer to a macro A macro may evaluate its arguments more than once n = MAX(i++, j); n = ((i++) > (j) ? (i++) : (j))

Conditional Compilation The #if and #endif Directives #define DEBUG 1 #if DEBUG printf(“Value of i: %d\n”, i); #endif

Writing Large Programs Source file **.c Header file **.h #include : search the directory in which system header files reside(on UNIX, system header files are kept in the directory /usr/include #include “filename”: search the current directory #include “c:\cprogs\utils.h” /* windows path */ #include “/cprogs/utils.h” /* UNIX path */

Pointer as Arguments boolean.h #define BOOL int #define TRUE 1 #define FALSE 0 src1.c #include “boolean.h” src2.c #include “boolean.h”

Sharing Function Prototypes stack.h void make_empty(void); int is_empty(void); int is_full(void); void push(int i); int pop(void); stack.c int contents[100]; Int top = 0; void make_empty(void) { ……. } int is_empty(void); { ……. } int is_full(void); void push(int i); int pop(void); cal.c #include “stack.h” int main(void) { make_empty(); …….. }

Sharing Variable Declaration External variables can be shared among files in much the same way functions are. We can put its definition in one source file, then put declarations in other files that need to use the variable. int i; /* declares i and defines it as well */ extern int i; /* declares i without defining it */ extern works with variables of all types.

Be careful When declarations of the same variable appear in different files, the compiler can’t check that the declaration match the variable’s definition. int i; extern long i;

Protecting Header Files Header1.h #include “Header1.h” Header2.h Header3.h #include “Header2.h” #include “Header3.h”

Protecting Header Files stack.h #ifndef STACK_H #define STACK_H void make_empty(void); int is_empty(void); int is_full(void); void push(int i); int pop(void); #endif

Building a Multiple-File Program Compiling: each source file in the program must be compiled separately. the compiler generates a file containing object code:.o in UNIX or.obj in Windows Linking: the linker conbines the object files to produce an executable file. Most compiler supports a simpler way: gcc –o justify justify.c line.c word.c

Makefiles justify: justify.o word.o line.o gcc –o justify justify.o word.o line.o justify.o: justify.c word.h line.h gcc –c justify.c word.o: word.c word.h gcc –c word.c line.o: line.c line.h gcc –c line.c

Makefiles Each command in a makefile must be preceded by a tab character, not a space. A makefile is normally stored in a file called Makefile (or makefile) To invoke make, use the command make target If no target is specified when make is invoked, it will build the target of the first rule.