Created
January 7, 2019 18:57
-
-
Save markd2/76c8076ffed43887e5d71f320e578751 to your computer and use it in GitHub Desktop.
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
// vararg.m -- use varargs to sum a list of numbers | |
// clang -g -Wall -o vararg vararg.m | |
#import <stdio.h> | |
#import <stdarg.h> | |
// sum all the integers passed in. Stopping if it's zero | |
static int addemUp (int firstNum, ...) { | |
va_list args; | |
int sum = firstNum; | |
int number; | |
va_start (args, firstNum); | |
while (1) { | |
number = va_arg (args, int); | |
sum += number; | |
if (number == 0) { | |
break; | |
} | |
} | |
va_end (args); | |
return sum; | |
} // addemUp | |
int main (void) { | |
int sumbody; | |
sumbody = addemUp (1, 2, 3, 4, 5, 6, 7, 8, 9, 0); | |
printf ("sum of 1..9 is %d\n", sumbody); | |
sumbody = addemUp (1, 3, 5, 7, 9, 11, 0); | |
printf ("sum of odds from 1..11 is %d\n", sumbody); | |
return 0; | |
} // main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment