본문 바로가기
기술자료/C C++

[賢彬] _STDC_, _P() 의 의미.

by 알 수 없는 사용자 2009. 8. 11.
728x90
반응형
Q 어떤 코드를 보면 함수의 프로토타입을 선언할 때 __P() 매크로를 사
용하는 경우가 있는데요, 이것의 의미는 무엇인가요? 
A 커널 소스의 아주 일부분에서는 함수를 선언할 때

void zfree __P((void *, void *ptr));

처럼 인자를 __P() 매크로에 넣어둔 것을 볼 수 있습니다. 이 __P() 매크
로의 정의를 살펴보면 include/linux/ppp_defs.h에서

#ifndef __P
#ifdef __STDC__
#define __P(x) x
#else
#define __P(x) ()
#endif
#endif

이 정의에서 __STDC__는 standard C 즉 ANSI C를 말합니다. 컴파일러가 
ANSI C로 컴파일을 하는 경우에는 __STDC__가 정의된 상태에서 컴파일이 
이루어집니다. 
ANSI C 이전의 C에서는 함수를 정의할 때 다음과 같이 했습니다.

int function()
int a, b
{
}

여기서는 함수의 프로토타입 안에 바로 인자를 기록하지 않고, 함수를 부
를 때에도 전달하는 인자가 올바른 타입(type)인지 검사하지 않습니다. 그
러나 ANSI C에서는 함수의 인자를 명확하게 정의를 해야 하고, 함수를 호
출할 때 전달하는 인자가 올바른 타입(type)인지 검사를 합니다. 즉 위의 
함수는 다음과 같이 됩니다. 
int function(int a, int b)

지금은 당연히 ANSI C를 사용하기 때문에 이렇게 사용하지만, ANSI C 이전
의 컴파일러를 사용하는 곳에서는 이전 버전의 선언방법을 따르는 경우도 
있습니다. __P() 매크로는 ANSI C인 경우(즉, __STDC__가 정의된 경우)에
는 전달받을 인자들을 명확히 기록을 하고, 그렇지 않은 경우는 인자를 생
략하도록 합니다. 이는 ANSI C가 아닌 K&R C로 컴파일할 수 있도록 하기 
위한 것 같습니다. 지금은 대부분 ANSI C를 지원하므로 굳이 이렇게 할 필
요가 없습니다. 이러한 형태는 일반 C 라이브러리에서도 볼 수 있습니다.

ANSI(American National Standard Institude)에 의하여 정의된 상수

Predefined Macros

Names the predefined ANSI C and Microsoft C++ implementation macros.

The compiler recognizes predefined ANSI C macros and the Microsoft C++ implementation provides several more. These macros take no arguments and cannot be redefined. Some of the predefined macros listed below are defined with multiple values. See the following tables for more information.

ANSI-Compliant Predefined Macros
MacroDescription

__DATE__

The compilation date of the current source file. The date is a string literal of the form Mmm dd yyyy. The month name Mmm is the same as for dates generated by the library function asctime declared in TIME.H.

__FILE__

The name of the current source file. __FILE__/FC (Full Path of Source Code File in Diagnostics). expands to a string surrounded by double quotation marks. To ensure that the full path to the file is displayed, use

You can create your own wide string version of __FILE__ as follows:

#include <stdio.h>
#define WIDEN2(x) L ## x
#define WIDEN(x) WIDEN2(x)
#define __WFILE__ WIDEN(__FILE__)
wchar_t *pwsz = __WFILE__;

int main() {}

__LINE__

The line number in the current source file. The line number is a decimal integer constant. It can be altered with a #line directive.

__STDC__

Indicates full conformance with the ANSI C standard. Defined as the integer constant 1 only if the /Za compiler option is given and you are not compiling C++ code; otherwise is undefined.

__TIME__

The most recent compilation time of the current source file. The time is a string literal of the form hh:mm:ss.

__TIMESTAMP__

The date and time of the last modification of the current source file, expressed as a string literal in the form Ddd Mmm Date hh:mm:ss yyyy, where Ddd is the abbreviated day of the week and Date is an integer from 1 to 31.


예제

#include <stdio.h>
#define __STDC__ 1

int main(void)
{
printf("\n\nCurrently at lin %d",__LINE__);

printf("\n\nThe value of __DATE__ is : ");
printf(__DATE__);

printf("\n\nThe value of __TIME__ is : ");
printf(__TIME__);

printf("\n\nThe value of __LINE__ is %d",__LINE__);

printf("\n\nThe value of __STDC__ is 1 if ANSI compatibility is on");
(__STDC__==1) ? printf("\nANSI on") : printf("\nANSI off");

printf("\n\nThe value of __FILE__ is : ");
printf(__FILE__);

return 0;
}
--------------------------------------------------------------------------------
출력 결과
--------------------------------------------------------------------------------
Currently at lin 6

The value of __DATE__ is : Feb 26 2007

The value of __TIME__ is : 00:38:36

The value of __LINE__ is 14

The value of __STDC__ is 1 if ANSI compatibility is on
ANSI on

The value of __FILE__ is : D:\WinAPI\PrePro\PrePro.c

728x90