Conditional Compilation
Completed
Add #define VERBOSE. Read n numbers and print their sum. Inside #ifdef VERBOSE also print adding X for each number before the sum.
Input
Line 1: n. Line 2: n integers.
Output
The trace lines, then the sum.
Example
Input
3 4 5 6
Output
adding 4 adding 5 adding 6 15
Editor
C
Hints
Code between #ifdef NAME and #endif is only compiled when NAME is defined.
Solution explanation
Conditional compilation is used for debug logging and platform-specific code.
Show solution
#include <stdio.h>
#define VERBOSE
int main() {
int n, x, sum = 0;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &x);
#ifdef VERBOSE
printf("adding %d\n", x);
#endif
sum += x;
}
printf("%d\n", sum);
return 0;
}