Skip to content

Instantly share code, notes, and snippets.

@githubutilities
Created August 23, 2015 05:45
Show Gist options
  • Select an option

  • Save githubutilities/d69906a25dfde6d13143 to your computer and use it in GitHub Desktop.

Select an option

Save githubutilities/d69906a25dfde6d13143 to your computer and use it in GitHub Desktop.
c infinite parameter
#include <stdio.h>
#include <stdarg.h>
// calculate the sum until -1 is met
int sum( int first, ... ) {
int ret = 0, val = first;
va_list marker;
va_start(marker, first);
while ( -1 != val ) {
ret += val;
val = va_arg(marker, int);
}
va_end(marker);
return ret;
}
int main() {
printf("%d", sum(1, 2, 3, 4, 5, 6, 7, 8, 9, -1));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment