I came upon a competitive question asking the output of the following (please overlook the software engineering issues of the problem, if any, as this is an academic example, however if it is relevant to the problem, please elaborate it):
#include <stdarg.h>
#include <stdio.h>
int ripple(int, ...);
int main()
{
int num;
num = ripple(3,5,7); // Shouldn't he pass 3 args after `3`, if arg count is 3?
printf("%d", num);
return 0;
}
int ripple(int n, ...)
{
int i, j=1, k=0;
va_list p;
va_start(p, n);
for(; j<n; ++j)
{
i = va_arg(p, int);
for(;i;i&=i-1)// Didn't understand the usage
++k;
}
return k;
}
I really didn't get how this code works or what it does (perhaps because of my near to nil experience with stdarg.h
). Any help is most welcome.
Thanks.