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

O

Oodini

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 !!
 
K

Keith Thompson

Oodini said:
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

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,831
Latest member
HealthSmartketoReviews

Latest Threads

Top