Global vs Local
Completed
A global int calls = 0; counts how many times add(a, b) is called. Read n pairs, print each sum, then calls=N. Inside main, a local variable named total must not clash with anything global.
Input
n, then n lines a b.
Output
n + 1 lines.
Example
Input
3 1 2 3 4 5 6
Output
3 7 11 calls=3
Editor
C
Hints
Globals are visible in every function; locals only in their block.
Solution explanation
Use globals sparingly - passing values as parameters keeps functions independent.
Show solution
#include <stdio.h>
int calls = 0;
int add(int a, int b) {
calls++;
return a + b;
}
int main() {
int n, a, b;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d %d", &a, &b);
int total = add(a, b);
printf("%d\n", total);
}
printf("calls=%d\n", calls);
return 0;
}