Saturday, 5 July 2014

Expressions ( Part2 )

Expressions (C-FAQ's)



Q1: Do you know in which order Relational, Logical, Arithmetic and Assignment operators get evaluated?

 

  A.     Relational, Logical, Arithmetic, Assignment

  B.      Arithmetic, Relational, Logical, Assignment

  C.      Arithmetic, Logical, Relational, Assignment



           


            Output:        
                                B.        Arithmetic, Relational, Logical , Assignment.

                       


Q2: What is the output of the following code?

          int max(int iA, int iB)

            {

                        iA>iB? return (iA): return(iB);

            }

            int main()

            {

                        printf(" max = %d\n ", max(5,10) );

                        return 0;

            }

 

                   


            Output:        
                        Compiler will return an error because of not properly using return.
                        Expression should be written as :-
                                    return ( iA > iB ? iA: iB ) ;
                       

Q3: What is the output of the following code?

         

            #include<stdio.h>

            int main()

            {

                        int iNum = ( -1, 0, 2, 3, 5 );

                               

                        printf(" value of iNum = %d\n", iNum );

                        return 0;

            }

 

         



            Output:
           value of iNum = 5
                        Because comma operator has left to right associativity. The left operand is always evaluated first, the result of evaluation is discarded before the right operand.
                        Here right most is operand is 5.
                       

Q4: What is the output of the following code?


            int main()

            {

                        int i=3, j=-2, k=0, a, b, c, d;

                        a=i || j || k;

                        b= i && j && k;

                        c=i || j++  &&  k;

                        d= i && j || --k;

                        printf("%d %d %d %d\n ", a ,b ,c ,d);

                                               

                        return 0;

            }

 

            A.        1 0 1 1

            B.        1 1 0 1

            C.        1 1 1 1

            D.        Output may vary from compiler to compiler.

               

           


            Output:
           1 0 1 1




<< PREV               [C FAQ's]            NEXT >>


No comments:

Post a Comment