01: // bugged code with added fprintf() and conditional compilation with #ifdef
02: #include <stdio.h>
03: 
04: #define DEBUG // comment this out to remove debug messages or, rather, to fully remove the code for debug messages
05: 
06: int main(int argc, char **argv){
07: 
08:   int sum=0, val, num=0;
09:   double ave;
10: 
11:   while (scanf("%d\n",&val) == 1) 
12:   {
13:     sum += val;
14:     num++;
15: #ifdef DEBUG
16:     fprintf(stderr, "val = %d sum = %d num = %d\n", val, sum, num); // stderr output is not buffered
17: #endif
18:   }
19: 
20: #ifdef DEBUG
21:   fprintf(stderr, "After while() val = %d sum = %d num = %d\n", val, sum, num);
22: #endif
23: 
24:   if(num > 0) 
25:   {
26:     ave = sum/num;
27: #ifdef DEBUG
28:     fprintf(stderr, "sum = %d num = %d sum/num = %f\n", sum, num, sum/num); //we have a warning here....
29: #endif
30:     printf("Average is %f\n", ave);
31:   }
32: }
33: 


Se avete commenti o osservazioni su questa pagina
mandate un messaggio di posta elettronica a bertoƶƶi@ce.unipr.it