01: // operatore logico AND, operatore relazionale < e espressione semanticamente non corretta
02: #include<stdio.h>
03: #include<stdlib.h>
04: 
05: int main(int argc, char **argv){
06: 
07:   int a = 15;
08: 
09:   int val1 = 0 < a < 10;            // FIXME the compiler gives us a warning, not an error!
10:   int val2 = 0 < a && a < 10;
11: 
12:   printf("(0 < a < 10)      con a = %3d is evaluated as: %d\n", a, val1);
13:   printf("(0 < a && a < 10) con a = %3d is evaluated as: %d\n", a, val2);
14: 
15:   a = -10;
16: 
17:   val1 = 0 < a < 10;            // FIXME the compiler gives us a warning, not an error!
18:   val2 = 0 < a && a < 10;
19: 
20:   printf("(0 < a < 10)      con a = %3d is evaluated as: %d\n", a, val1);
21:   printf("(0 < a && a < 10) con a = %3d is evaluated as: %d\n", a, val2);
22: 
23:   a = 5;
24: 
25:   val1 = 0 < a < 10;            // FIXME the compiler gives us a warning, not an error!
26:   val2 = 0 < a && a < 10;
27: 
28:   printf("(0 < a < 10)      con a = %3d is evaluated as: %d\n", a, val1);
29:   printf("(0 < a && a < 10) con a = %3d is evaluated as: %d\n", a, val2);
30: 
31:   return 0;
32: }
33: 
34: /* OUTPUT
35: 
36: (0 < a < 10)      con a =  15 is evaluated as: 1
37: (0 < a && a < 10) con a =  15 is evaluated as: 0
38: (0 < a < 10)      con a = -10 is evaluated as: 1
39: (0 < a && a < 10) con a = -10 is evaluated as: 0
40: (0 < a < 10)      con a =   5 is evaluated as: 1
41: (0 < a && a < 10) con a =   5 is evaluated as: 1
42: 
43: The underlying reason why "0 < a < 15" is not working is that
44: the system split the evaluation of this expression as ((0 < a) < 15)
45: Namely, the expression is evaluated starting from the left, the first operator it finds
46: is the left "<" then "0 < a" is evaluated. When a == 15 this means "true" namely "1".
47: The resulting "1" is then used for evaluating the other part of the expression as
48: "1 < 10" that is obviously true and therefore 1.
49: This also is true when a == -10. In such case the first part of the expression is
50: "0" and not "1", but "0 < 10" is anyway true as well.
51: 
52: */
53: 
54: 


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