/docs/MyDocs

To get this branch, use:
bzr branch http://darksoft.org/webbzr/docs/MyDocs

« back to all changes in this revision

Viewing changes to Development/languages/C/Core/macros.txt

  • Committer: Suren A. Chilingaryan
  • Date: 2009-04-09 03:21:08 UTC
  • Revision ID: csa@dside.dyndns.org-20090409032108-w4edamdh4adrgdu3
import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
The specific syntax being used is GCC specific. ANSI C (1999 standard)
 
2
has variable argument macros with a different syntax (which modern GCC
 
3
supports).
 
4
 
 
5
Old GCC syntax...
 
6
#define P_VAR(output, string, args...) \
 
7
fprintf (output, "This is "string"\n", ##args)
 
8
 
 
9
The "##" in the above macro is GCCs method of dealing with the case where
 
10
the variable part of the macro is empty. It has the effect of deleting the
 
11
comma just prior to the "##" when args is empty.
 
12
 
 
13
 
 
14
ANSI C and new GCC syntax
 
15
#define P_VAR(output, ...) \
 
16
fprintf (output, "This is "string"\n", __VA_ARGS__)
 
17
 
 
18
This definition of P_VAR has the identical expansion as the previous
 
19
definition
 
20
of P_VAR. Both require a minimum of 2 parameters when being expanded.
 
21
 
 
22
The Old GCC version has the advantage that it's closer to what you expect
 
23
from experience with printf() and company, but suffers from the kludge
 
24
needed when the variable part of the macro is empty.
 
25
 
 
26
The ANSI C version doesn't have the "empty kludge", but requires one more
 
27
parameter than the number of named parameters. For example let's say that
 
28
I want to create a "log" macro that is used in the exact same fashion as
 
29
printf() except it sends all output to log_file instead of stdout. The
 
30
desired macro would look like:
 
31
 
 
32
#define log(...) fprintf(log_file, __VA_ARGS__)
 
33
 
 
34
And yes, the log macro would require at least 1 parameter in order to be
 
35
legal just as printf() requires at least 1 parameter.
 
36
 
 
37
Using the old GCC style the macro would be
 
38
 
 
39
#define log(format, args...) fprintf(log_file, format, ##args)
 
40
 
 
41
I would suggest that you use the new ANSI method of variable argument macros
 
42
instead of the old GCC style since it is likely to be supported by more
 
43
compilers in the future