I have been trying to pass variable arguments to other function in C but it is producing inconsistent result in different runtime environment as well as in different runs in same environment:
int main()
{
int result = myprintf("Something \n %d", 9);
return result;
}
int myprintf(const char *format, ...){
printf("Something \n %d", 9);
printf("\n");
va_list args;
va_start(args, format);
int result = printf(format,args);
printf("\n");
va_end(args);
return result;
}
And the result produced is:
WWW.FIRMCODES.COM
9
WWW.FIRMCODES.COM
438656664
I could not find the reason for "438656664".
args
only to functions that takeva_args
as argument. These have av
in their name:vprintf
,vfprintf
,vsnprintf
. – M Oehm Apr 27 '16 at 6:06va_list
to thev
family of functions is the way to go. You probably shouldn't manipulate the format string, though, at least not the format specifiers themselves. (You can get many compilers to check whether format string and arguments agree; gcc/clang do this via attributes, VC++ does this via SAL annotations. If you tamper with the format string, such checks will be pointless.) – M Oehm Apr 27 '16 at 6:24