#include <stdarg.h>
#include <stdio.h>
void varfun(int n,...){
va_list ptr;
int num;
va_start(ptr,n);
num=va_arg(ptr,int);
printf("\n%d",num);
}
int main(int argc, char **argv){
varfun(3,7.5,-11.2,0.66);
return 0;
}
Look at the above code, i expect the output to be the first variable parameter value casted to int i.e 7.5 casted to int, i.e. 7. But the output is 0.
What is wrong in this?
The
va_argdoes not convert the argument. It interprets it as the indicated type. And if the types don’t match, you invoke Undefined Behaviour.Also note the cast isn’t really needed in your snippet. The compiler will convert automatically