[C언어] _PROTOTYPE 쓰는 방법

babbab의 이미지

코드 부분
....

#if 0
_PROTOTYPE(int main, (int argc, char **argv));
_PROTOTYPE(void count, (FILE *f));
_PROTOTYPE(void usage, (void));
#endif
 
int main (int argc, char **argv);
void count (FILE *f);
void usage (void);
 
int main(argc, argv)
int argc;
char *argv[];
{
int k;
char *cp;
int tflag, files;

....
더 많은 코드

그리고 위의 코드는 컴파일 됩니다.

두 질문이 있는데요... function prototype은 ansi 스타일, function definition은 k&r 스타일로 서로 스타일이 맞지 않는데 왜 컴파일이 됩니까?

그리고 _PROTOTYPE macro는 어떻게 사용합니까?

위으 코드 #if 0 와 #endif 는 제가 ansi 스타일 function prototype을 쓰면 컴파일이 되나 볼려고 무시 처리 했습니다.
int main (int argc, char **argv);
void count (FILE *f);
void usage (void);
부분도 제가 더했습니다.

위의 코드는 상당히 오래된 코드로 제가 다시 코딩하고 싶은데 _PROTOTYPE macro 사용법은 이해 되지않아 이렇게 올립니다.

klara의 이미지

결론부터 말씀드리자면 컴파일러가 그것을 허용하기 때문이지 그것이 표준이기 때문이 아닙니다.
ANSI로 컴파일하게 옵션을 주면 컴파일 안될겁니다.
가끔은 표준대로만 하기엔 불편할때가 있기 때문에 보통 컴파일러는 아무런 옵션이 없으면 표준을 엄격하게 지켜서 컴파일하지 않습니다.

역사적으로 두 가지가 섞여쓰이던 시대가 존재한 듯합니다.
그래서 지금도 예전 코드에 대한 호환성을 위해서 지원하고 있는 듯 하네요.

익명 사용자의 이미지

재밌는건 ansi-c only 옵션을 해도 컴파일이 됩니다.
아마도 ansi에서 허용을 하는듯...

ymir의 이미지

compiler 또는 compiler 옵션에 따라 ANSI 또는 K&R style 로 prototype 선언을 맞춰주는 매크로인듯 합니다.
이 매크로가 필요한 특정 compiler 를 써야 하거나, K&R style 을 고수해야 하는 상황이 아니라면..
그냥 무시하고 버리셔도 될 것 같네요.

http://stackoverflow.com/questions/2607853/why-prototype-is-used-header-files

되면 한다! / feel no sorrow, feel no pain, feel no hurt, there's nothing gained.. only love will then remain.. 『 Mizz 』

익명 사용자의 이미지

고전 스타일(not prototype-format parameter type) 함수 선언 및 정의 방식도 현재의 표준안에 들어가 있습니다.
그러니 당연히 표준(ansi c, C90, C99)을 따르는 컴파일러라면 잘 되지요.

하지만 C99 기준으로 obsolescent로 지정되어 있기 때문에 차후 표준에는 얼마든지 삭제가 가능합니다.

이 케케묵은 고전 스타일을 뭐 좋다고들 저렇게 마르고 닳도록 써대는지는 잘 모르겠습니다.

2012년인 지금에 굳이 저런 스타일을 배려해서 전처리 지시자를 주렁주렁 달 필요가 있을까요?

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.