Red Hat ENTERPRISE LINUX 3 - USING CPP Using Instructions page 24

Using cpp, the c preprocessor
Hide thumbs Also See for ENTERPRISE LINUX 3 - USING CPP:
Table of Contents

Advertisement

20
using this extension. You cannot use
You can have named arguments as well as variable arguments in a variadic macro. We could define
like this, instead:
eprintf
#define eprintf(format, ...) fprintf (stderr, format, __VA_ARGS__)
This formulation looks more descriptive, but unfortunately it is less flexible: you must now supply at
least one argument after the format string. In standard C, you cannot omit the comma separating the
named argument from the variable arguments. Furthermore, if you leave the variable argument empty,
you will get a syntax error, because there will be an extra comma after the format string.
eprintf("success!\n", );
==> fprintf(stderr, "success!\n", );
GNU CPP has a pair of extensions which deal with this problem. First, you are allowed to leave the
variable argument out entirely:
eprintf ("success!\n")
==> fprintf(stderr, "success!\n", );
Second, the
token paste operator has a special meaning when placed between a comma and a
##
variable argument. If you write
#define eprintf(format, ...) fprintf (stderr, format, ##__VA_ARGS__)
and the variable argument is left out when the
will be deleted. This does not happen if you pass an empty argument, nor does it happen if the token
preceding
is anything other than a comma.
##
eprintf ("success!\n")
==> fprintf(stderr, "success!\n");
The above explanation is ambiguous about the case where the only macro parameter is a variable
arguments parameter, as it is meaningless to try to distinguish whether no argument at all is an empty
argument or a missing argument. In this case the C99 standard is clear that the comma must remain,
however the existing GCC extension used to swallow the comma. So CPP retains the comma when
conforming to a specific C standard, and drops it otherwise.
C99 mandates that the only place the identifier
a variadic macro. It may not be used as a macro name, macro argument name, or within a different
type of macro. It may also be forbidden in open text; the standard is ambiguous. We recommend you
avoid using it except for its defined purpose.
Variadic macros are a new feature in C99. GNU CPP has supported them for a long time, but only
with a named variable argument (
portability to previous versions of GCC, you should use only named variable arguments. On the other
hand, if you are concerned with portability to other conforming implementations of C99, you should
use only
__VA_ARGS__
__VA_ARGS__
, not
args...
.
and this extension in the same macro.
macro is used, then the comma before the
eprintf
can appear is in the replacement list of
__VA_ARGS__
and
...
__VA_ARGS__
Chapter 3. Macros
). If you are concerned with
##

Advertisement

Table of Contents
loading

This manual is also suitable for:

Enterprise linux 3

Table of Contents