I am trying to understand how a variational function works. I am reading man stdargand I am writing the following code:
#include <stdarg.h>
#include <stdio.h>
int sum(int count, ...){
va_list lst;
va_start(lst, count);
printf("First=%i,Second=%i,Third=%i, Fourth=%i, Fifth=%i\n",va_arg(lst,int),va_arg(lst,int),va_arg(lst,int),va_arg(lst,int),va_arg(lst,int));
}
int main(){
sum(1,2,3,4);
}
After compiling and working, I have the following input:
First=0,Second=134513840,Third=4, Fourth=3, Fifth=2.
I do not understand this. I expect First=2, Second=3, Third=4Fourth / Fifth to have an undefined value, because after calling the function, the arguments are pushed onto the stack from right to left and va_arg(lst, int)just return a pointer to an element that goes deeper on the stack.
user2889159
source
share