COM

[C언어] #error , #line 지시자

Weistern 2008. 4. 7. 23:53

#error
전처리기가 전처리 지시자 #error 를 만나면, 컴파일 오류를 발생시키며, 지시자 뒤의 문자열을 화면에 출력한다.


예.
#ifndef __STDC__
     #error None Standard C
#endif

#include<stdio.h>

int
main(void) {
     printf("Standard C\n");
     return 0;
}


__STDC__가 정의되어 있지 않은상태에서, 컴파일을 시도하면 다음과 같이 오류메시지가 뜬다.

--------------------Configuration: temp - Win32 Debug--------------------
Compiling...
temp.c
d:\temp\temp.c(2) : fatal error C1189: #error :  ANSI C is not the default.
Error executing cl.exe.

temp.exe - 1 error(s), 0 warning(s)

-----------------------------------------------------------------------------------------------------

#line

The #line directive tells the preprocessor to change the compiler's internally stored line number and filename to a given line number and filename.

#line integral_constant  "filename"

컴파일러로 하여금 다음 행의 행번호를 주어진 번호( 즉, 윗 구문의 integral_constant 정수상수) 로 바꾸게 하고, 파일이름을 주어진 파일명으로 믿게만든다.

여기서 filename 은 옵션이다. (표시하기 귀찮아서 옵션표시생략했다.)

__FILE__ 과 __LINE__ 를 사용하는 코드를 작성후, 해당 코드 이전에 #line 150 "newname.txt" 를 써보면 출력결과가 달라지는 것을 확인할 수 있다.

---------------------------------------------------------------------------------------------------


#pragma 지시자는 나중에 따로 자세히 다루기로 한다.