Created
November 13, 2014 17:05
-
-
Save apfohl/490d8898f3b53362b30d to your computer and use it in GitHub Desktop.
va_arg c example
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> | |
| int sum(int argc, ...) | |
| { | |
| int res = 0; | |
| va_list ap; | |
| va_start(ap, argc); | |
| for (int i = 0; i < argc; i++) { | |
| res += va_arg(ap, int); | |
| } | |
| va_end(ap); | |
| return res; | |
| } | |
| int main(void) | |
| { | |
| fprintf(stderr, "%d\n", sum(4, 1, 2, 3, 4)); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment