I couldn't figure out how the program works so I ran it on the computer and got the output:
the sum of 5 to 4 is 10
I do not understand how nMax
is passed into the sumInts
function (empty parameters) during function call however the value of n
is taken from the global variable. That's the only way it n
could increment into 5 and sum into 10.
thanks in advance
#include <stdio.h>
#include <stdlib.h>
void sumInts();
int n=0;
int nMax = 0;
int sum = 0;
int main(int argc, char* argv[]) {
if (argc<2) {
printf("Usage: ex2 7 \n");
exit(-1);
}
nMax = atoi(argv[1]);
for (n=1;n<nMax;n++) {
sumInts();
printf("The sum from %d to %d is %d \n" , n , nMax, sum);
}
return 0;
}
void sumInts() {
while (n<= nMax) {
sum = sum+n;
n++;
}
}