Knowledge (XXG)

Variadic macro in the C preprocessor

Source 📝

123:
No means is provided to access individual arguments in the variable argument list, nor to find out how many were passed. However, macros can be written to count the number of arguments that have been passed.
570:. Some compilers (e.g., Visual Studio when not using the new standard-conformant preprocessor) will silently eliminate the trailing comma. Other compilers (e.g.: GCC) support putting 829:
Before the existence of variable-arguments in C99, it was quite common to use doubly nested parentheses to exploit the variable number of arguments that could be supplied to the
147:
macro is replaced by its argument when arguments are present, and omitted otherwise. Common compilers also permit passing zero arguments before this addition, however.
154:
from expanding recursively. It is possible to work around this limitation up to an arbitrary fixed number of recursive expansions, however.
798:# define MYLOG(FormatLiteral, ...) fprintf (stderr, "%s(%u): " FormatLiteral "\n", __FILE__, __LINE__, ##__VA_ARGS__) 585:# define MYLOG(FormatLiteral, ...) fprintf (stderr, "%s(%u): " FormatLiteral "\n", __FILE__, __LINE__, __VA_ARGS__) 294:
Due to limitations of the variadic macro support in C++11 the following straightforward solution can fail and should thus be avoided:
1147: 980: 105:" is used to indicate that one or more arguments must be passed. During macro expansion each occurrence of the special identifier 47: 230:
were desired, which would take the file and line number from which it was called as arguments, the following solution applies.
1102: 1035:
Clang source code change that mentions __VA_ARGS__ support (2006-07-29), note that Clang was open-sourced in 2007.
186:(formerly Sun Studio) Forte Developer 6 update 2 (C++ version 5.3). GCC also supports such macros when compiling 812: 82: 62: 43: 198: 167: 996: 922: 183: 1036: 566:
There is a portability issue with generating a trailing comma with empty args for variadic macros in
35: 910: 1013: 992: 206: 175: 1132: 1127: 94: 1048: 947: 984: 967: 1152: 39: 1141: 1060: 1024: 179: 988: 187: 390:
circumvent the abovementioned incompatibilities. This is tricky but portable.
224: 77:
language standard. Support for variadic macros with no arguments was added in
17: 98: 1081: 549: 163: 136: 132: 78: 70: 554: 388:
By using the 'cformat' string as part of the variadic arguments we can
359:
where the comma before the closing brace will result in a syntax error.
395:#define dbgprintf(...) realdbgprintf (__FILE__, __LINE__, __VA_ARGS__) 544: 382:
realdbgprintf (__FILE__, __LINE__, cformat __VA_OPT__(,) __VA_ARGS__)
219: 1037:
http://llvm.org/viewvc/llvm-project?view=revision&revision=38770
166:
support variable-argument macros when compiling C and C++ code: the
109:
in the macro replacement list is replaced by the passed arguments.
74: 202: 171: 911:
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2004/n1653.htm
548:
is not directly possible. The standard workaround is to use the
150:
The C preprocessor rules prevent macro names in the argument of
112:
Additionally, regular macro arguments may be listed before the
567: 128: 58: 909:
Working draft changes for C99 preprocessor synchronization –
362:
GNU C++ supports a non-portable extension which solves this.
1049:
http://developers.sun.com/sunstudio/support/CCcompare.html
369:
realdbgprintf (__FILE__, __LINE__, cformat, ##__VA_ARGS__)
968:
Variadic Macros – Using the GNU Compiler Collection (GCC)
302:
realdbgprintf (__FILE__, __LINE__, cformat, __VA_ARGS__)
53:
Variable-argument macros were introduced in 1999 in the
948:"WG14 - N3033 : Comma omission and comma deletion" 793:
GCC supports the following (non-portable) extension:
116:, but regular arguments may not be listed after the 843: 830: 571: 552:functionality of C/C++, and have the function call 403: 227: 135:standards require at least one argument, but since 197:macro to support zero arguments has been added in 375:C++20 eventually supports the following syntax. 46:may be declared to accept a varying number of 542:Without variadic macros, writing wrappers to 93:The declaration syntax is similar to that of 8: 139:this limitation has been lifted through the 27:Macro taking a varying number of arguments 790:which generates a syntax error with GCC. 942: 940: 902: 1025:Recursive macros with C++20 __VA_OPT__ 1009: 1007: 1005: 803:which removes the trailing comma when 963: 961: 959: 957: 7: 838:#define dbgprintf(x) realdbgprintf x 923:"Comma omission and comma deletion" 815:solves this problem by introducing 687:"%s(%u): Too many balloons %u 65:language standard, and in 2011 in 25: 379:#define dbgprintf(cformat, ...) \ 366:#define dbgprintf(cformat, ...) \ 299:#define dbgprintf(cformat, ...) \ 1103:"MSVC new preprocessor overview" 1047:Sun Studio feature comparison – 635:"Too many balloons %u" 601:"Too many balloons %u" 590:The following application works 719:But look at this application: 34:is a feature of some computer 1: 979:Laurent Deniau (2006-01-16). 882:"Hello, world %d" 857:"Hello, world %d" 1169: 846:could then be called as: 235:Our implemented function 1148:C (programming language) 873: 848: 835: 795: 740: 721: 672: 617: 592: 582: 516:"%d + %d = %d" 495: 467:"%d + %d = %d" 458: 448:"Hello, world" 427: 417:"Hello, world" 408: 406:could then be called as 392: 376: 363: 328: 309: 296: 236: 670:which is equivalent to 199:GNU Compiler Collection 168:GNU Compiler Collection 1082:"C++ Support in Clang" 1061:"C++2a Support in GCC" 758:"Attention!" 730:"Attention!" 143:functional macro. The 97:: a sequence of three 1014:Variadic Macros (C++) 184:Oracle Solaris Studio 81:and will be added in 36:programming languages 1105:. September 10, 2020 755:"%s(%u): " 632:"%s(%u): " 456:Another example is 307:The reason is that 871:which expands to: 207:Visual Studio 2019 176:Visual Studio 2005 95:variadic functions 89:Declaration syntax 73:) revision of the 67:ISO/IEC 14882:2011 61:) revision of the 1133:Variadic template 1128:Variadic function 738:which expands to 615:which expands to 493:which expands to 425:which expands to 349:"Hallo" 326:gets expanded to 318:"Hallo" 55:ISO/IEC 9899:1999 38:, especially the 16:(Redirected from 1160: 1115: 1114: 1112: 1110: 1099: 1093: 1092: 1090: 1088: 1078: 1072: 1071: 1069: 1067: 1057: 1051: 1045: 1039: 1033: 1027: 1022: 1016: 1011: 1000: 999: 976: 970: 965: 952: 951: 944: 935: 934: 932: 930: 919: 913: 907: 892: 889: 886: 883: 880: 877: 867: 864: 861: 858: 855: 852: 845: 839: 832: 820: 808: 799: 786: 783: 780: 777: 774: 771: 768: 765: 762: 759: 756: 753: 750: 747: 744: 734: 731: 728: 725: 715: 712: 709: 706: 703: 700: 697: 694: 691: 688: 685: 682: 679: 676: 666: 663: 660: 657: 654: 651: 648: 645: 642: 639: 636: 633: 630: 627: 624: 621: 611: 608: 605: 602: 599: 596: 586: 579: 573: 557: 547: 538: 535: 532: 529: 526: 523: 520: 517: 514: 511: 508: 505: 502: 499: 489: 486: 483: 480: 477: 474: 471: 468: 465: 462: 452: 449: 446: 443: 440: 437: 434: 431: 421: 418: 415: 412: 405: 396: 383: 380: 370: 367: 356: 353: 350: 347: 344: 341: 338: 335: 332: 322: 319: 316: 313: 303: 300: 288: 285: 282: 279: 276: 273: 270: 267: 264: 261: 258: 255: 252: 249: 246: 243: 240: 229: 222: 193:Support for the 174:(all versions), 119: 115: 21: 1168: 1167: 1163: 1162: 1161: 1159: 1158: 1157: 1138: 1137: 1124: 1119: 1118: 1108: 1106: 1101: 1100: 1096: 1086: 1084: 1080: 1079: 1075: 1065: 1063: 1059: 1058: 1054: 1046: 1042: 1034: 1030: 1023: 1019: 1012: 1003: 978: 977: 973: 966: 955: 946: 945: 938: 928: 926: 925:. June 18, 2017 921: 920: 916: 908: 904: 899: 894: 893: 890: 887: 884: 881: 878: 875: 869: 868: 865: 862: 859: 856: 853: 850: 841: 840: 837: 827: 819: 816: 807: 804: 801: 800: 797: 788: 787: 784: 781: 778: 775: 772: 769: 766: 763: 760: 757: 754: 751: 748: 745: 742: 736: 735: 732: 729: 726: 723: 717: 716: 713: 710: 707: 704: 701: 698: 695: 692: 689: 686: 683: 680: 677: 674: 668: 667: 664: 661: 658: 655: 652: 649: 646: 643: 640: 637: 634: 631: 628: 625: 622: 619: 613: 612: 609: 606: 603: 600: 597: 594: 588: 587: 584: 578: 575: 564: 553: 543: 540: 539: 536: 533: 530: 527: 524: 521: 518: 515: 512: 509: 506: 503: 500: 497: 491: 490: 487: 484: 481: 478: 475: 472: 469: 466: 463: 460: 454: 453: 450: 447: 444: 441: 438: 435: 432: 429: 423: 422: 419: 416: 413: 410: 401: 398: 397: 394: 385: 384: 381: 378: 372: 371: 368: 365: 358: 357: 354: 351: 348: 345: 342: 339: 336: 333: 330: 324: 323: 320: 317: 314: 311: 305: 304: 301: 298: 290: 289: 286: 283: 280: 277: 274: 271: 268: 265: 262: 259: 256: 253: 250: 247: 244: 241: 238: 218: 215: 196: 160: 153: 146: 142: 117: 113: 108: 104: 91: 28: 23: 22: 15: 12: 11: 5: 1166: 1164: 1156: 1155: 1150: 1140: 1139: 1136: 1135: 1130: 1123: 1120: 1117: 1116: 1094: 1073: 1052: 1040: 1028: 1017: 1001: 971: 953: 936: 914: 901: 900: 898: 895: 874: 849: 836: 826: 823: 817: 805: 796: 741: 722: 673: 618: 593: 583: 576: 563: 562:Trailing comma 560: 496: 459: 428: 409: 400: 399: 393: 386: 377: 373: 364: 360: 329: 310: 297: 291: 257:SourceFilename 237: 232: 214: 211: 194: 159: 156: 151: 144: 140: 106: 102: 90: 87: 40:C preprocessor 32:variadic macro 26: 24: 18:Variadic macro 14: 13: 10: 9: 6: 4: 3: 2: 1165: 1154: 1151: 1149: 1146: 1145: 1143: 1134: 1131: 1129: 1126: 1125: 1121: 1104: 1098: 1095: 1083: 1077: 1074: 1062: 1056: 1053: 1050: 1044: 1041: 1038: 1032: 1029: 1026: 1021: 1018: 1015: 1010: 1008: 1006: 1002: 997: 994: 990: 986: 982: 981:"__VA_NARG__" 975: 972: 969: 964: 962: 960: 958: 954: 950:. 2022-07-20. 949: 943: 941: 937: 924: 918: 915: 912: 906: 903: 896: 876:realdbgprintf 872: 847: 834: 824: 822: 814: 810: 794: 791: 739: 720: 671: 616: 591: 581: 569: 561: 559: 556: 551: 546: 498:realdbgprintf 494: 457: 430:realdbgprintf 426: 407: 391: 387: 374: 361: 331:realdbgprintf 327: 308: 295: 292: 281:CFormatString 242:realdbgprintf 234: 233: 231: 226: 221: 212: 210: 208: 204: 200: 191: 189: 185: 181: 177: 173: 169: 165: 157: 155: 148: 138: 134: 130: 125: 121: 110: 100: 96: 88: 86: 84: 80: 76: 72: 68: 64: 60: 56: 51: 49: 45: 41: 37: 33: 19: 1107:. Retrieved 1097: 1085:. Retrieved 1076: 1064:. Retrieved 1055: 1043: 1031: 1020: 974: 929:December 24, 927:. Retrieved 917: 905: 870: 842: 828: 825:Alternatives 811: 802: 792: 789: 737: 718: 669: 614: 589: 574:in front of 565: 541: 492: 455: 424: 402: 389: 325: 306: 293: 266:SourceLineno 216: 192: 161: 149: 126: 122: 111: 92: 66: 54: 52: 42:, whereby a 31: 29: 1109:December 8, 844:dbgprintf() 806:__VA_ARGS__ 577:__VA_ARGS__ 404:dbgprintf() 228:dbgprintf() 188:Objective-C 107:__VA_ARGS__ 1142:Categories 989:comp.std.c 897:References 833:function: 821:like C++. 818:__VA_OPT__ 809:is empty. 195:__VA_OPT__ 182:2006, and 180:C++Builder 152:__VA_OPT__ 145:__VA_OPT__ 141:__VA_OPT__ 99:full stops 985:Newsgroup 851:dbgprintf 558:instead. 461:dbgprintf 411:dbgprintf 312:dbgprintf 164:compilers 127:Both the 48:arguments 1122:See also 1087:June 14, 1066:June 14, 831:printf() 779:__LINE__ 773:__FILE__ 705:__LINE__ 699:__FILE__ 656:__LINE__ 650:__FILE__ 510:__LINE__ 504:__FILE__ 442:__LINE__ 436:__FILE__ 343:__LINE__ 337:__FILE__ 225:function 162:Several 993:Usenet: 987::  743:fprintf 675:fprintf 620:fprintf 555:vprintf 550:stdargs 213:Example 205:6, and 158:Support 995:  767:" 761:" 749:stderr 693:" 681:stderr 644:" 638:" 626:stderr 545:printf 223:-like 220:printf 724:MYLOG 595:MYLOG 287:...); 272:const 248:const 217:If a 203:Clang 172:Clang 170:3.0, 137:C++20 133:C++11 79:C++20 71:C++11 44:macro 1111:2020 1089:2018 1068:2018 931:2022 275:char 251:char 239:void 131:and 1153:C++ 866:)); 813:C23 568:C99 263:int 201:8, 129:C99 118:... 114:... 103:... 83:C23 75:C++ 59:C99 50:. 1144:: 1004:^ 991:. 983:. 956:^ 939:^ 891:); 888:27 863:27 854:(( 785:); 764:\n 733:); 714:); 711:42 690:\n 665:); 662:42 641:\n 610:); 607:42 580:. 572:## 537:); 488:); 451:); 420:); 209:. 190:. 178:, 120:. 85:. 30:A 1113:. 1091:. 1070:. 998:. 933:. 885:, 879:( 860:, 782:, 776:, 770:, 752:, 746:( 727:( 708:, 702:, 696:, 684:, 678:( 659:, 653:, 647:, 629:, 623:( 604:, 598:( 534:5 531:, 528:2 525:, 522:2 519:, 513:, 507:, 501:( 485:5 482:, 479:2 476:, 473:2 470:, 464:( 445:, 439:, 433:( 414:( 355:) 352:, 346:, 340:, 334:( 321:) 315:( 284:, 278:* 269:, 260:, 254:* 245:( 101:" 69:( 63:C 57:( 20:)

Index

Variadic macro
programming languages
C preprocessor
macro
arguments
C99
C
C++11
C++
C++20
C23
variadic functions
full stops
C99
C++11
C++20
compilers
GNU Compiler Collection
Clang
Visual Studio 2005
C++Builder
Oracle Solaris Studio
Objective-C
GNU Compiler Collection
Clang
Visual Studio 2019
printf
function
printf
stdargs

Text is available under the Creative Commons Attribution-ShareAlike License. Additional terms may apply.