#if (defined(__STDC__) && !defined(NO_PROTOTYPE)) || defined(__cplusplus)

Discussion in 'C Programming' started by Oodini, Sep 27, 2005.

  1. Oodini

    Oodini Guest

    Hello,

    Could anyone explain me the following preprocessor sentences:

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

    #if (defined(__STDC__) && !defined(NO_PROTOTYPE)) || defined(__cplusplus)
    # define _USING_PROTOTYPES_ 1
    # define _ANSI_ARGS_(x) x
    # define CONST const
    # ifdef __cplusplus
    # define VARARGS (...)
    # else
    # define VARARGS ()
    # endif
    #else
    # define _ANSI_ARGS_(x) ()
    # define CONST
    #endif

    #undef EXTERN
    #ifdef __cplusplus
    # define EXTERN extern "C"
    #else
    # define EXTERN extern
    #endif

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

    I know that __STDC__ means: "the compiler is C compatible"
    I know that __cplusplus means: "the compiler does C++ compilation"

    But I wonder what are NO_PROTOTYPE and _USING_PROTOTYPES_.
    Do they refer to the prototypes put in the .h ???
    Are there compilers which doesn't accept prototypes ?
    How do they do, then ?

    Where do I find these preprocessor on the web ? No luck on Google...

    So I underdstand:
    if the compiler is ANSI C and does accept prototypes, or if the compiler
    is C++...
    let's use prototypes !
    replace _ANSI_ARGS_(x) with x
    ....

    About VARARGS...
    Why is there a condition ? I believed that, for functions with an
    undetermined number of parameters, the syntax was the same in C and C++
    (both use dot-dot-dot): function(parameter,...)

    # define _ANSI_ARGS_(x) () ?????????

    Well, if I have a prototype like
    EXTERN void
    surface_set_shader _ANSI_ARGS_((Surface *surface,
    void *surf_desc,
    Shader *shader));
    I have to remplace it with
    EXTERN void surface_set_shader(); ???
    What about parameters ??


    THANKS A LOT FOR HELP !!
     
    Oodini, Sep 27, 2005
    #1
    1. Advertising

  2. Re: #if (defined(__STDC__) && !defined(NO_PROTOTYPE)) ||defined(__cplusplus)

    Oodini <> writes:
    > Could anyone explain me the following preprocessor sentences:
    >
    > ----------------------------------------------------------------
    >
    > #if (defined(__STDC__) && !defined(NO_PROTOTYPE)) || defined(__cplusplus)
    > # define _USING_PROTOTYPES_ 1
    > # define _ANSI_ARGS_(x) x
    > # define CONST const
    > # ifdef __cplusplus
    > # define VARARGS (...)
    > # else
    > # define VARARGS ()
    > # endif
    > #else
    > # define _ANSI_ARGS_(x) ()
    > # define CONST
    > #endif
    >
    > #undef EXTERN
    > #ifdef __cplusplus
    > # define EXTERN extern "C"
    > #else
    > # define EXTERN extern
    > #endif
    >>

    > -----------------------------------------------------------------
    >
    > I know that __STDC__ means: "the compiler is C compatible"
    > I know that __cplusplus means: "the compiler does C++ compilation"


    Right. (As far as this newsgroup is concerned, __cplusplus is a macro
    that the implementation is not allowed to define.)

    > But I wonder what are NO_PROTOTYPE and _USING_PROTOTYPES_.
    > Do they refer to the prototypes put in the .h ???
    > Are there compilers which doesn't accept prototypes ?
    > How do they do, then ?


    Those are non-standard macros. They must be defined in some
    application-specific header, or possibly by your compiler.

    It looks like this code was designed to work around problems with very
    old non-conforming implementations. Today, you're very unlikely to
    run across a C compiler that doesn't support prototypes, or that
    doesn't conform at least to the C89/C90 standard. I'd be surprised to
    see *any* compiler that defines __STDC__ but doesn't support
    prototypes.

    Note that testing __STDC__ doesn't necessarily guarantee that your
    compiler is conforming; it merely indicates that it *claims* to be
    conforming. The standard requires any conforming implementation to
    define __STDC__ (the required value changed from C89/C90 to C99), but
    obviously it can't place any requirements on non-conforming compilers.
    A non-conforming compiler doesn't become any more non-conforming
    because it defines __STDC__.

    As for the VARARGS macro, the <stdarg.h> header and the "..." syntax
    have been standard C since C89/C90, and again, you're not likely to
    run into a compiler that doesn't support it. But this feature was, I
    believe, an invention of the ANSI C committee. The usual way to
    define a printf-like function before that was to use the (now
    obsolete) <stdargs.h> header.

    It's very likely that you can safely rip out most of these tests, and
    unconditionally use prototypes, "...", and <stdarg.h>. But you should
    check what platforms you need to support before trying it -- *and* you
    need to decide whether cleaning up the code is worth the effort and
    the risk of accidentaly breaking something. The old forms are still
    supported, so you might just want to leave things alone; in fact I'd
    recommend it unless you have a specific reason to fix the code.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Sep 27, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Arturo

    Help with __cplusplus

    Arturo, Jun 9, 2004, in forum: C Programming
    Replies:
    12
    Views:
    30,471
    barun
    May 9, 2008
  2. Walter Roberson

    Re: #ifdef __cplusplus

    Walter Roberson, Apr 9, 2008, in forum: C Programming
    Replies:
    8
    Views:
    1,757
    Keith Thompson
    Apr 10, 2008
  3. Antoninus Twink

    Re: #ifdef __cplusplus

    Antoninus Twink, Apr 9, 2008, in forum: C Programming
    Replies:
    5
    Views:
    520
    Keith Thompson
    Apr 9, 2008
  4. Philip Potter

    Re: #ifdef __cplusplus

    Philip Potter, Apr 9, 2008, in forum: C Programming
    Replies:
    4
    Views:
    575
    Ian Collins
    Apr 9, 2008
  5. Flash Gordon

    Re: #ifdef __cplusplus

    Flash Gordon, Apr 9, 2008, in forum: C Programming
    Replies:
    10
    Views:
    673
    Flash Gordon
    Apr 11, 2008
Loading...

Share This Page