C언어 8강 | 전처리기 Preprocessors
preprocessor
#include #define이 대표적
use of #include
#include <stdio.h>
#include <stdlib.h>
#include "filename"
: 직접 만든 것 (파일 경로 일치해야 함)
use of #define
#define identifier token_string
#define SECONDS_PER_DAY (60*60*24)
#define PI 3.141592
#define C 299792.458
#define EOF -1
#define MAXINT 2147483647
macros with arguments
#define SQ(x) ((x)*(x))
#define SQ(x) x*x
#define SQ(x) (x) * (x)
셋 다 다른 표현임 주의
#define min(x, y) (((x)<(y))?(x):(y))
#define min4(a,b,c,d) min(min(a,b),min(c,d))
매크로를 재귀적으로 쓸 수도 있다.
#define fractional_part (x-(int)x)
#define random_char() (rand()%26+'a')
#define random_float() (rand()%100/10.0)
macros in stdio.h and ctype.h
stdio.h
getchar(), putchar(c)
ctype.h
갱장히 다양하게 있다.
TRUE/FALSE : isalpha(c), isupper(c), islower(c), isdigit(c), isalnum(c), isxdigit(c), isspace(c), ispunct(c), isprint(c), isgraph(c), iscntrl(c), isascii(c) /*(이때 c는 integral type)*/
RETURN : toupper(c), tolower(c), toascii(c)
시험에서 toascii 필요한 상황 나오면, toascii 쓰기~!
conditional compilation
조건적인 컴파일
#if
#define DEBUG 1
#if DEBUG
printf("debug : a="%d\n", a);
#endif
#include "everything.h"
#undef PIE
#define PIE "I like apple"
#if defined(HP9000)||define(SUN4)&&!denfined(VAX)
#endif
#define DEBUG
#ifdef DEBUG
printf("debug : a="%d\n", a);
#endif
#ifdef
#ifndef
#endif
#undef identifier
predefined macros
__DATE__ : current date
__TIME__ : current time
__STDC__ : ANSI standard C를 따르는 경우, nonzero(true)
__FILE__ : 소스 파일 이름
__LINE__ : 현재 라인 넘버
assert() macro
#define assert(expr)
if (!(expr)){
printf("\n%s%s\n%s%s\n%s%d\n\n",
"Assertion failed:", #expr,
"in file", __FILE__,
"at line", __LINE__);
abort();
}
# 단항 연산자는 string으로 만드는 연산자?