We could define eprintf like this instead define

Info icon This preview shows pages 24–26. Sign up to view the full content.

View Full Document Right Arrow Icon
You can have named arguments as well as variable arguments in a variadic macro. We could define eprintf like this, instead: #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", ); 7→ 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") 7→ 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 eprintf macro is used, then the comma before 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") 7→ 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 __VA_ARGS__ can appear is in the re- placement list of 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 (‘ args... ’, not ‘ ... ’ and __VA_ARGS__ ). If you are concerned with 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__ . Previous versions of CPP implemented the comma-deletion extension much more gener- ally. We have restricted it in this release to minimize the differences from C99. To get the same effect with both this and previous versions of GCC, the token preceding the special ## ’ must be a comma, and there must be white space between that comma and whatever comes immediately before it: #define eprintf(format, args...) fprintf (stderr, format , ##args) See Section 11.4 [Differences from previous versions], page 55 , for the gory details.
Image of page 24

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Chapter 3: Macros 21 3.7 Predefined Macros Several object-like macros are predefined; you use them without supplying their definitions.
Image of page 25
Image of page 26
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern