Quantcast
Channel: How does work vararg function calling - Stack Overflow
Viewing all articles
Browse latest Browse all 3

How does work vararg function calling

$
0
0

I'm trying to understand how a variadic function does work. I'm read man stdarg and I'm write 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 running this i have the following input:

First=0,Second=134513840,Third=4, Fourth=3, Fifth=2.

I don't understand this. I'm expected that First=2, Second=3, Third=4 and Fourth/Fifth has indefined value because after function invocation an arguments pushed to stack from right to left and va_arg(lst, int) just return a pointer to an element which deeper lies in stack.


Viewing all articles
Browse latest Browse all 3

Latest Images

Trending Articles



Latest Images