Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   #if (defined(__STDC__) && !defined(NO_PROTOTYPE)) || defined(__cplusplus) (http://www.velocityreviews.com/forums/t439562-if-defined-__stdc__-and-and-defined-no_prototype-defined-__cplusplus.html)

Oodini 09-27-2005 07:18 PM

#if (defined(__STDC__) && !defined(NO_PROTOTYPE)) || defined(__cplusplus)
 
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 !!

Keith Thompson 09-27-2005 07:58 PM

Re: #if (defined(__STDC__) && !defined(NO_PROTOTYPE)) ||defined(__cplusplus)
 
Oodini <svdbg_nospam@free.fr> 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) kst-u@mib.org <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.


All times are GMT. The time now is 04:58 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.