0% found this document useful (0 votes)
223 views3 pages

How To Use - VA - ARGS - Inside A C Function Instead of Macro - Stack Overflow

The document discusses how to use variable arguments (__VA_ARGS__) inside a C function instead of a macro. It provides several responses: 1) Variable argument functions use va_list, va_start, va_end from stdarg.h to access arguments, not __VA_ARGS__. A minimum of one fixed argument is needed for va_start. 2) vprintf can be used to call printf with a va_list instead of directly passing __VA_ARGS__. 3) __VA_ARGS__ is only for macros, not functions. Functions need to use va_list and related functions to access variable arguments.

Uploaded by

aamya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
223 views3 pages

How To Use - VA - ARGS - Inside A C Function Instead of Macro - Stack Overflow

The document discusses how to use variable arguments (__VA_ARGS__) inside a C function instead of a macro. It provides several responses: 1) Variable argument functions use va_list, va_start, va_end from stdarg.h to access arguments, not __VA_ARGS__. A minimum of one fixed argument is needed for va_start. 2) vprintf can be used to call printf with a va_list instead of directly passing __VA_ARGS__. 3) __VA_ARGS__ is only for macros, not functions. Functions need to use va_list and related functions to access variable arguments.

Uploaded by

aamya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

12/3/2016

Howtouse__VA_ARGS__insideaCfunctioninsteadofmacro?StackOverflow

signup

login

tour

help

xDismiss

JointheStackOverflowCommunity
Stack Overflow is a community of 6.4 million
programmers, just like you, helping each other.
Join them it only takes a minute:
Signup

Howtouse__VA_ARGS__insideaCfunctioninsteadofmacro?[duplicate]

PossibleDuplicate:
C/C++:Passingvariablenumberofargumentsaround
I'mcurrentlyusingthefollowingmacrodeclaredonmyCfile.
#defineCOMMON_Print(...)printf(__VA_ARGS__)

Nowthatcallworksjustfine,butturnsoutthatIneedtobeabletocreateaCfunctionthatlookssomethinglikethis:
voidCOMMON_Print(...)
{
printf(__VA_ARGS__);
}

Sothatfunctiondoesn'twork,Igetanerror
"Error:undefinedidentifier__VA_ARGS__"
Thecomplexityofmyprojectrequirestohaveafunctionsinceit'saninterface...SohowcanIgettheparameters...andpassthemtotheprintf
function?OrbetterwhatamIdoingwrong?
Thanks!
c
askedDec2'10at20:16

Jona
6,549

60

106

markedasduplicatebyKos ,abelenky ,Let_Me_Be,nos ,JohnKugelmanDec2'10at20:25


Thisquestionhasbeenaskedbeforeandalreadyhasananswer.Ifthoseanswersdonotfullyaddressyourquestion,pleaseaskanewquestion.

1 Seethisstackoverflow.com/questions/205529/Kos Dec2'10at20:19

4Answers

Eachofthe ?printf functionshasacorresponding


thingbuttakesa va_list ,avariableargumentlist.

v?printf

functionwhichdoesthesame

#include<stdio.h>
#include<stdarg.h>
voidCOMMON_Print(char*format,...)

http://stackoverflow.com/questions/4339412/howtousevaargsinsideacfunctioninsteadofmacro

1/3

12/3/2016

Howtouse__VA_ARGS__insideaCfunctioninsteadofmacro?StackOverflow

{
va_listargs;
va_start(args,format);
vprintf(format,args);
va_end(args);
}

Sidenote:Noticethat va_start takesthenameofthelastargumentbeforethe ... asa


parameter. va_start isamacrowhichdoessomestackbasedmagictoretrievethevariable
argumentsanditneedstheaddressofthelastfixedargumenttodothis.
Asaconsequenceofthis,therehastobeatleastonefixedargumentsoyoucanpassitto
va_start .ThisiswhyIaddedthe format argumentinsteadofstickingwiththesimpler
COMMON_Print(...) declaration.
See:http://www.cplusplus.com/reference/clibrary/cstdio/vprintf/
editedDec2'10at20:25

answeredDec2'10at20:19

JohnKugelman
178k

36

310

388

Ifyouusemacros,youneednoprecedingarg.Considerthisexample: #defineLOGI(...)
((void)__android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)) Thisobviouslyisvery

convenientfromausageviewpoint.ArcaneEngineerJul28'15at11:39

__VA_ARGS__
va_start

isonlyformacrosvariadicfunctionsareratherdifferent.Youneedtouse
and va_end from stdarg.h tohandlethem.

va_arg

First,yourfunctionneedsatleastonenamedparameter,e.g.:
voidCOMMON_Print(constchar*fmt,...)

Thenyoucandefinea

va_list

anduse

va_start

tosetit:

va_listargs;
va_start(args,fmt);
//yourcodehere
va_end(args);

Nowyoucanuse args toaccesstheargumentscalling va_arg(args,TYPE) willreturnthenext


variadicargument,soyoucanjustkeepcallingthatuntilyou'vereadallthearguments.
Ifyou'rejusttryingtocall
directly:

printf

,there'saprintfvariantcalled

vprintf

thattakesthe

va_list

vprintf(fmt,args);

Thereisnoeasywaytocallonevariadicfunctionfromanotheryouneedsomethinglike
vprintf thattakesthewhole va_list
editedDec2'10at21:07

answeredDec2'10at20:21

MichaelMrozek
74k

11

125

138

Thanksforyourexplanation.Thanksforthehelpandexplanationstheyreallyhelped!:) Jona Dec2'10

at20:33
'va_arg(args,TYPE)'willreturnNULLonlyifNULLwaspassedbythecaller.Thereisnodefaultend

conditionforva_arg.Youmusteitherpassasentinelvalueorknowtheexactnumberofparameters,passed
byaparameterorbycountingthenumberof%inaprintfformatstring.PatrickSchlterDec2'10at21:05
@tristopiaOh,rightI'minthehabitofpassingNULLattheendwhennecessary.Fixed MichaelMrozek

Dec2'10at21:08

__VA_ARGS__

isformacrosonly.

Chainingthevariablenumberofargumenttoanotherfunctioncan'tbedonedirectly.Insteadyou
havetopassava_list,andthefunctionyou'recallinghavetotakeava_list.Luckilythere'sa
variationofprintfthatdoesthis,yourfunctionhavetobewrittenthisway:
voidCOMMON_Print(char*format,...)
{
va_listargs;
va_begin(args,format);
vsprintf(format,args);

http://stackoverflow.com/questions/4339412/howtousevaargsinsideacfunctioninsteadofmacro

2/3

12/3/2016

Howtouse__VA_ARGS__insideaCfunctioninsteadofmacro?StackOverflow

va_end(args);
}
editedDec1'15at14:43

answeredDec2'10at20:22

nos
143k

33

241

378

1 Thanksalotforyourresponse!Itactuallyworks!ButtherearesomeformattingissuesgoingonthatIneed
toresolveonmyendIbelieve.FYI,I'mworkingonanembeddeddevice. Jona Dec2'10at20:32
Whatisvaprintf?Orisitjustatypo? VincentFourmond Dec1'15at14:42

WhatyouarelookingforistheEllipsis operator.
editedMay20'14at18:06

answeredDec2'10at20:22

Dima
29.5k

11

54

98

http://stackoverflow.com/questions/4339412/howtousevaargsinsideacfunctioninsteadofmacro

3/3

You might also like