Page 42 - 6437
P. 42

Example
                          Try the following example to understand operator precedence in C:

                 #include <stdio.h>



                 main()

                 {
                    int a = 20;

                    int b = 10;
                    int c = 15;

                    int d = 5;
                    int e;



                    e = (a + b) * c / d;            // ( 30 * 15 ) / 5

                    printf("Value of (a + b) * c / d is : %d\n",  e );



                    e = ((a + b) * c) / d;          // (30 * 15 ) / 5
                    printf("Value of ((a + b) * c) / d is  : %d\n" ,  e );



                    e = (a + b) * (c / d);         // (30) * (15/5)

                    printf("Value of (a + b) * (c / d) is  : %d\n",  e );



                    e = a + (b * c) / d;           //  20 + (150/5)
                    printf("Value of a + (b * c) / d is  : %d\n" ,  e );



                    return 0;

                 }

                          When you compile and execute the above program, it produces the following result:
                            Value of (a + b) * c / d is : 90

                            Value of ((a + b) * c) / d is : 90
                            Value of (a + b) * (c / d) is : 90

                            Value of a + (b * c) / d is  : 50




                                                                                                      44
   37   38   39   40   41   42   43   44   45   46   47