Created
August 23, 2015 05:45
-
-
Save githubutilities/d69906a25dfde6d13143 to your computer and use it in GitHub Desktop.
c infinite parameter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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