12 주 강의 The preprocessor. The use of #include #include #include “ filename ” ::: current...
-
Upload
sophia-gabriella-lane -
Category
Documents
-
view
249 -
download
4
Transcript of 12 주 강의 The preprocessor. The use of #include #include #include “ filename ” ::: current...
![Page 1: 12 주 강의 The preprocessor. The use of #include #include #include “ filename ” ::: current directory system-dependent places #include ::: system dependent.](https://reader035.fdocuments.net/reader035/viewer/2022062314/56649e925503460f94b9789e/html5/thumbnails/1.jpg)
12 주 강의
The preprocessor
![Page 2: 12 주 강의 The preprocessor. The use of #include #include #include “ filename ” ::: current directory system-dependent places #include ::: system dependent.](https://reader035.fdocuments.net/reader035/viewer/2022062314/56649e925503460f94b9789e/html5/thumbnails/2.jpg)
The use of #include
• #include <stdio.h>• #include “filename” ::: current directory syst
em-dependent places
• #include <filename> ::: system dependent places unix :: /usr/include
![Page 3: 12 주 강의 The preprocessor. The use of #include #include #include “ filename ” ::: current directory system-dependent places #include ::: system dependent.](https://reader035.fdocuments.net/reader035/viewer/2022062314/56649e925503460f94b9789e/html5/thumbnails/3.jpg)
#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 { ….
![Page 4: 12 주 강의 The preprocessor. The use of #include #include #include “ filename ” ::: current directory system-dependent places #include ::: system dependent.](https://reader035.fdocuments.net/reader035/viewer/2022062314/56649e925503460f94b9789e/html5/thumbnails/4.jpg)
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
![Page 5: 12 주 강의 The preprocessor. The use of #include #include #include “ filename ” ::: current directory system-dependent places #include ::: system dependent.](https://reader035.fdocuments.net/reader035/viewer/2022062314/56649e925503460f94b9789e/html5/thumbnails/5.jpg)
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
![Page 6: 12 주 강의 The preprocessor. The use of #include #include #include “ filename ” ::: current directory system-dependent places #include ::: system dependent.](https://reader035.fdocuments.net/reader035/viewer/2022062314/56649e925503460f94b9789e/html5/thumbnails/6.jpg)
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
![Page 7: 12 주 강의 The preprocessor. The use of #include #include #include “ filename ” ::: current directory system-dependent places #include ::: system dependent.](https://reader035.fdocuments.net/reader035/viewer/2022062314/56649e925503460f94b9789e/html5/thumbnails/7.jpg)
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)
![Page 8: 12 주 강의 The preprocessor. The use of #include #include #include “ filename ” ::: current directory system-dependent places #include ::: system dependent.](https://reader035.fdocuments.net/reader035/viewer/2022062314/56649e925503460f94b9789e/html5/thumbnails/8.jpg)
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 *)
![Page 9: 12 주 강의 The preprocessor. The use of #include #include #include “ filename ” ::: current directory system-dependent places #include ::: system dependent.](https://reader035.fdocuments.net/reader035/viewer/2022062314/56649e925503460f94b9789e/html5/thumbnails/9.jpg)
Macros with arguments
• 377 page 설명
![Page 10: 12 주 강의 The preprocessor. The use of #include #include #include “ filename ” ::: current directory system-dependent places #include ::: system dependent.](https://reader035.fdocuments.net/reader035/viewer/2022062314/56649e925503460f94b9789e/html5/thumbnails/10.jpg)
The Macros in stdio.h and ctype.h
• <stdio.h>– #define getchar() getc(stdin)– #define putchar(c) put((c), stdout)
![Page 11: 12 주 강의 The preprocessor. The use of #include #include #include “ filename ” ::: current directory system-dependent places #include ::: system dependent.](https://reader035.fdocuments.net/reader035/viewer/2022062314/56649e925503460f94b9789e/html5/thumbnails/11.jpg)
<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
![Page 12: 12 주 강의 The preprocessor. The use of #include #include #include “ filename ” ::: current directory system-dependent places #include ::: system dependent.](https://reader035.fdocuments.net/reader035/viewer/2022062314/56649e925503460f94b9789e/html5/thumbnails/12.jpg)
<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
![Page 13: 12 주 강의 The preprocessor. The use of #include #include #include “ filename ” ::: current directory system-dependent places #include ::: system dependent.](https://reader035.fdocuments.net/reader035/viewer/2022062314/56649e925503460f94b9789e/html5/thumbnails/13.jpg)
Conditional compilation
• #if constant_integral_expression• #ifdef identifier• #ifndef identifier• #endif• #undef identifier• defined identifier – defined(identifier)
![Page 14: 12 주 강의 The preprocessor. The use of #include #include #include “ filename ” ::: current directory system-dependent places #include ::: system dependent.](https://reader035.fdocuments.net/reader035/viewer/2022062314/56649e925503460f94b9789e/html5/thumbnails/14.jpg)
예• #if defined(HP9000) || defined(SUN4) && !defined(VAX) ….
/*machine-dependent code */ #endif• #define DEBUG 1• #if DEBUG printf(“debug: a = %d\n”, a); #endif
![Page 15: 12 주 강의 The preprocessor. The use of #include #include #include “ filename ” ::: current directory system-dependent places #include ::: system dependent.](https://reader035.fdocuments.net/reader035/viewer/2022062314/56649e925503460f94b9789e/html5/thumbnails/15.jpg)
예 2
• #define DEBUG• #ifdef DEBUG ….. #endif• #include “everything.h” #undef PIE #define PIE “I like apple.”
![Page 16: 12 주 강의 The preprocessor. The use of #include #include #include “ filename ” ::: current directory system-dependent places #include ::: system dependent.](https://reader035.fdocuments.net/reader035/viewer/2022062314/56649e925503460f94b9789e/html5/thumbnails/16.jpg)
#elif, #else
• #elif• #endif• #if #elif #elif #else #endif
![Page 17: 12 주 강의 The preprocessor. The use of #include #include #include “ filename ” ::: current directory system-dependent places #include ::: system dependent.](https://reader035.fdocuments.net/reader035/viewer/2022062314/56649e925503460f94b9789e/html5/thumbnails/17.jpg)
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
![Page 18: 12 주 강의 The preprocessor. The use of #include #include #include “ filename ” ::: current directory system-dependent places #include ::: system dependent.](https://reader035.fdocuments.net/reader035/viewer/2022062314/56649e925503460f94b9789e/html5/thumbnails/18.jpg)
# (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; }
![Page 19: 12 주 강의 The preprocessor. The use of #include #include #include “ filename ” ::: current directory system-dependent places #include ::: system dependent.](https://reader035.fdocuments.net/reader035/viewer/2022062314/56649e925503460f94b9789e/html5/thumbnails/19.jpg)
## (ANSI C)
• Merge tokens• #define X(i) x##i X(1) = X(2) = X(3); x1 = x2 = x3
![Page 20: 12 주 강의 The preprocessor. The use of #include #include #include “ filename ” ::: current directory system-dependent places #include ::: system dependent.](https://reader035.fdocuments.net/reader035/viewer/2022062314/56649e925503460f94b9789e/html5/thumbnails/20.jpg)
assert()
• assert(p != NULL); assert(n>0 && n<7);• 389 page 설명
![Page 21: 12 주 강의 The preprocessor. The use of #include #include #include “ filename ” ::: current directory system-dependent places #include ::: system dependent.](https://reader035.fdocuments.net/reader035/viewer/2022062314/56649e925503460f94b9789e/html5/thumbnails/21.jpg)
#error, #pragma
• #if A_SIZE < B_SIZE #error “Incompatible sizes” #endif
• #pragma tokens
![Page 22: 12 주 강의 The preprocessor. The use of #include #include #include “ filename ” ::: current directory system-dependent places #include ::: system dependent.](https://reader035.fdocuments.net/reader035/viewer/2022062314/56649e925503460f94b9789e/html5/thumbnails/22.jpg)
Line Numbers, etc.
• #line integral_constant “filename”
• #undefine isalpha /* access the function, not the macro */
![Page 23: 12 주 강의 The preprocessor. The use of #include #include #include “ filename ” ::: current directory system-dependent places #include ::: system dependent.](https://reader035.fdocuments.net/reader035/viewer/2022062314/56649e925503460f94b9789e/html5/thumbnails/23.jpg)
Quicksort
• 간단히 설명• 모르면 넘어간다
![Page 24: 12 주 강의 The preprocessor. The use of #include #include #include “ filename ” ::: current directory system-dependent places #include ::: system dependent.](https://reader035.fdocuments.net/reader035/viewer/2022062314/56649e925503460f94b9789e/html5/thumbnails/24.jpg)
숙제• 학교 1, 6, 8• 집 5, 7, 13, 16, 20, 21• 30 을 해결하면 추가 점수 부여