the preprocessor #include #define n 10 c program preprocessor modified c program preprocessor object...

20
The Preprocessor #include <stdio.h> #define N 10 C program Preprocessor Modified C program Preprocessor Object codes

Upload: gordon-horton

Post on 24-Dec-2015

212 views

Category:

Documents


0 download

TRANSCRIPT

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

The Preprocessor

#include <stdio.h>#define N 10

C program

Preprocessor

Modified C program

Preprocessor

Object codes

Page 2: 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.

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

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.

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

Macro DefinitionsSimple Macros:#define identifier replacement-list

#define N = 100….int a[N];

#define N 100;….int a[N];

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

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 3.14159#define CR ‘\r’#define EOS ‘\0’#define MEM_ERR “Error: not enough memory”

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

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

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

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))

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

Parameterized Macros---advantages

•The program may be slightly faster

•Macros are “generic”

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

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-checkedIt’s not possible to have a pointer to a macroA macro may evaluate its arguments more than oncen = MAX(i++, j);n = ((i++) > (j) ? (i++) : (j))

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

Conditional Compilation

The #if and #endif Directives

#define DEBUG 1

#if DEBUGprintf(“Value of i: %d\n”, i);printf(“Value of i: %d\n”, i);#endif

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

Writing Large Programs

Source file **.cHeader file **.h

#include <filename>: 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 */

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

Pointer as Argumentsboolean.h#define BOOL int#define TRUE 1#define FALSE 0

src1.c#include “boolean.h”

src2.c#include “boolean.h”

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

Sharing Function Prototypesstack.hvoid make_empty(void);int is_empty(void);int is_full(void);void push(int i);int pop(void);

stack.cint 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(); ……..}

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

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.

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

Be carefulWhen 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;

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

Protecting Header Files

Header1.h

#include “Header1.h” #include “Header1.h”

Header2.h Header3.h

#include “Header2.h”

#include “Header3.h”

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

Protecting Header Filesstack.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

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

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

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

Makefilesjustify: justify.o word.o line.o

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

gcc –c justify.cword.o: word.c word.h

gcc –c word.cline.o: line.c line.h

gcc –c line.c

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

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.

http://www.makelinux.net/make3/make3-CHP-1-SECT-1.html