12 주 강의 the preprocessor. the use of #include #include #include “ filename ” ::: current...
TRANSCRIPT
12 주 강의
The preprocessor
The use of #include
• #include <stdio.h>• #include “filename” ::: current directory syst
em-dependent places
• #include <filename> ::: system dependent places unix :: /usr/include
#define
• #define identifier token_string • #define PI 3.14159• #define SECONDS_PER_DAYS (60*60*24)
• Syntactic sugar– #define EQ ==– #define do /* blank */– while (I EQ 1) do { ….
Macros with arguments
• #define identifier(identifier, …, identifier) token_stringopt
• #define SQ(x) ((x)*(x))– SQ(7+w) ((7+w)*(7+w))– SQ(SQ(*p)) ((((*p) * (*p))) * (((*p) * (*
p))))• #define SQ(x) x*x
– SQ(a+b) a + b * a + b
Micros(Cont.)
• #define SQ(x) (x) * (x)– 4/SQ(2) 4/(2)*(2)
• #define SQ (x) ((x) * (x))– SQ(7) (x) ((x) * (x)) (7)
• #define SQ(x) ((x) * (x); /* error */– x=SQ(y); x = ((y)*(y));;– if (x ==2) x=SQ(y); else ++x;
syntax error
Micros(cont.)
• #define min(x,y) (((x) < (y)) ? (x) : (y))• m=min(u,v) ;• #define SQ(x) ((x) * (x))• #define CUBE(x) (SQ(x) * (x))• #define F_POW(x) sqrt(sqrt(CUBE(x)))• #undef identifier
– Undef F_POW
Type definitions and stddef.h
• typedef char uppercase;• stddef.h
– typedef int ptrdiff_t;– typedef short wchar_t;– typedef unsigned size_t;
• #define NULL ((void *) 0)
Sorting with qsort()
• stdlib.h– void qsort(void *array, size_t n_els, size_t
el_size, int compare(const void *, const void *));
– int compare(const void *, const void *)
Macros with arguments
• 377 page 설명
The Macros in stdio.h and ctype.h
• <stdio.h>– #define getchar() getc(stdin)– #define putchar(c) put((c), stdout)
<ctype.h>Macro Nonzero (true) is returned if:
isalpha(c ) c is a letter
isupper(c ) c is an uppercase letter
islower (c ) c is a lowercase letter
isdigit (c ) c is a digit
isalnum (c ) c is a letter or digit
isxdigit (c ) c is a hexadecimal digit
isspace (c ) c is a white space character
ispunct (c ) c is a punctuation character
isprint (c ) c is printable character
isgraph (c ) c is printable, but not a space
iscntrl (c ) c is a control character
isascii(c ) c is an ASCII code
<ctype.h> (cont.)Call to the function
or macroValue returned
toupper(c ) corresponding uppercase value or c
tolower(c ) corresponding lowercase value or c
toascii(c ) corresponding ASCII value
Conditional compilation
• #if constant_integral_expression• #ifdef identifier• #ifndef identifier• #endif• #undef identifier• defined identifier – defined(identifier)
예• #if defined(HP9000) || defined(SUN4) && !defined(VAX) ….
/*machine-dependent code */ #endif• #define DEBUG 1• #if DEBUG printf(“debug: a = %d\n”, a); #endif
예 2
• #define DEBUG• #ifdef DEBUG ….. #endif• #include “everything.h” #undef PIE #define PIE “I like apple.”
#elif, #else
• #elif• #endif• #if #elif #elif #else #endif
Predefined macros
Predefined Value
_DATE_ A string containing the current date
_FILE_ A string containing the file name
_LINE_An integer representing the current line number
_STDC_If the implementation follows ANSI Standard C,then the value the is a nonzero integer
_TIME_ A string containing the current time
# (ANSI C)
• # stringization – #define message_for(a,b) \ printf(#a “ and ” #b “: We love you!\n”) int main(void) { message_for(Carole, Debra); return 0; }
## (ANSI C)
• Merge tokens• #define X(i) x##i X(1) = X(2) = X(3); x1 = x2 = x3
assert()
• assert(p != NULL); assert(n>0 && n<7);• 389 page 설명
#error, #pragma
• #if A_SIZE < B_SIZE #error “Incompatible sizes” #endif
• #pragma tokens
Line Numbers, etc.
• #line integral_constant “filename”
• #undefine isalpha /* access the function, not the macro */
Quicksort
• 간단히 설명• 모르면 넘어간다
숙제• 학교 1, 6, 8• 집 5, 7, 13, 16, 20, 21• 30 을 해결하면 추가 점수 부여