-2

Currently, I am using:

#define p printf(

In order to reduce the amount of characters I use within printf's throughout the program, e.g, instead of:

printf("Hello, World.");

I can do:

p"Hello, World.");

My question is, is it possible to further shorten this? E.g, adding the ending brace into the define? Something like (pseudo-code) :

#define p printf()

Or some such? Or even a shorter way of doing the #define?

Edit: I'd better clarify before I get downvoted into oblivion. The point of this is not to save keystrokes because I am lazy, or because I want to make it unreadable. I have already written the program in question (which is quite readable and hopefully won't get me put in programmers hell), and I am curious as to how low I can get the char count. This question would assist in doing so.

Bernie
  • 1,489
  • 1
  • 16
  • 27

4 Answers4

3

You can use macros with variadic arguments:

#define p(...) printf(__VA_ARGS__)

Even though a name such as p is not a good idea, this syntax can very well be used with conditional logging. For example:

#ifndef NDEBUG
# define log(...) fprintf(stderr, __VA_ARGS__)
#else
# define log(...) ((void)0)
#endif

Sometimes, you may want to do something specific with the format specifier. For that, you can take advantage of a gcc extension (and possibly other compilers) for example such as the following:

#define log(fmt, ...) fprintf(stderr, "Log: "fmt"\n", ##__VA_ARGS__)

Note that ##__VA_ARGS__ is not standard. See this answer also.

Community
  • 1
  • 1
Shahbaz
  • 46,337
  • 19
  • 116
  • 182
  • I was thinking the same thing, but don't you need to pass the format specifier? I think it would be #define p(f, ...) printf(f, __VA_ARGS__); Is this right? – Josh Petitt Feb 23 '13 at 03:54
  • 1
    @JoshPetitt, not really. The format specifier is one of the variadic arguments. This works correctly. I will however write on what you have in mind in a sec. – Shahbaz Feb 23 '13 at 03:54
  • @JoshPetitt, indeed if you define the macro with `fmt, ...` as arguments, you won't be able to write the macro with only the format string and no other arguments, unless you use a non-standard extension. This is because C requires `...` in macros to contain at least one argument. – Shahbaz Feb 23 '13 at 03:59
  • I was thinking about functions and va_arg. You have to pass at least one variable before the argument list so you can know how many arguments to expect. I was confusing macros and functions, sorry for the confusion and thanks for the answer. – Josh Petitt Feb 23 '13 at 04:01
  • @JoshPetitt, exactly. It would have made more sense if they allowed zero args in `...` in C. The one argument before is not really necessary, since the macro gets expanded and if it expands to a function, then the compiler (not the preprocessor) takes care of whether the syntax is correct or not. With this restriction, such a useful macro is not possible: `#define CALL(f, ...) do { if (f(__VA_ARGS__)) goto exit_##f##_failed; } while (0)` – Shahbaz Feb 23 '13 at 04:06
0

Yes your macro can take an argument

http://gcc.gnu.org/onlinedocs/cpp/Macro-Arguments.html

Eric J.
  • 147,927
  • 63
  • 340
  • 553
0

That is evil. Stop trying to save keystrokes and improve your typing skill instead. It is not an appropriate use of macros to "simplify" code in this manner, because you will simply make your code harder to read for ordinary C programmers. Always keep readability in mind.

Be happy you don't have to write System.out.println instead.

nneonneo
  • 171,345
  • 36
  • 312
  • 383
0

Based on your demonstrated need, consider puts

This would be safer/better to macro-ize, since it doesn't take a variable number of arguments. You've taken printf and limited it use. Also, a user will type p("%d") and the program will blow up.

Josh Petitt
  • 9,371
  • 12
  • 56
  • 104