Saturday, 5 July 2014

Expressions ( Part1 )

Expressions (C-FAQ's)



Q1: Can you explain

                   iNum = iNum++ * iNum++      expression result is undefined , but

                   iNum = iNum++ || iNum++    result is always same?


           


            Output:        
                                According to the C standard an object's stored value can be modified only once between two sequence points.

                        Sequence points occurs at following places:-
                                    1)        at the end of full expression( which is not a sub expression ).
                                    2)        at && , || and ?: operators.
                                    3)        at a function call, after evaluation of all the arguments, just before the actual call.
                       
                        So first expression is being modified twice between sequence points, that is why behavior is undefined. But the second expression is getting modified once before sequence point || and once after that.


Q2: Can you rewrite the following expression?

          iNum1 <= 50 ? iNum2=100 : iNum2=200;

         


            Output:        
                        The above expression can be rewrite as :-
                                    iNum2 = iNum1 <= 50 ? 100:200 ;
                       

Q3: Can you rewrite the following expression?

          iNum1 <=50 ? iNum2=200: iNum3=200;


         



            Output:
                        The above expression can be rewrite as:-
                                    *( ( iNum1 <= 50 ) ? &iNum2 : &iNum3 ) = 200 ;

Q4: What is the output of the following code?


            int main()

            {

                        int iNum=2;

                        printf("%d %d\n ", ++iNum, ++iNum );

                                               

                        return 0;

            }

 

            A.        3 4

            B.        4 3

            C.        4 4

            D.        Output may vary from compiler to compiler.

               

           


            Output:
                        D.        Output may vary from compiler to compiler
      4 4 (In GCC Compiler)      




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

Control Instructions ( Part1 )

Control Instructions (C-FAQ's)



Q1: How many times while loop will get executed in the following code ?


            #include<stdio.h>

            int main()

            {

                        int iCtr=0;

                        while( iCtr<= 300);

                        {

                                    printf(" iCtr  = %d\n ",  iCtr );

                        }                 

                        return 0;

            }


            A.        Infinite Times

            B.        300 times

            C.        301 times

            D         Only Once;

 


            Output:        
                                A. Infinite Times , because the value of iCtr is not getting changed so the while loop condition will always be true.


Q2: How many times will the message "Hello World" get printed ?

          #include<stdio.h>

            int main()

            {

                   int iCtr;

                        for( iCtr=0; iCtr <= 20 ; iCtr++ )

                        {

                                    if ( iCtr < 3)

                                                continue;

                                    else

                                                break;

                                    printf("Hello World\n");

                        }

            return 0;

            }

 

            A.        Infinite Times

            B.        20 times

            C.        21 times

            D.        0 times



            Output:        
                        D.        0 times

Q3: What is the output of the following code?

          #include<stdio.h>

            int main()

            {

                        int i = 2,  j = 3;

                        for( ; j ; printf("%d %d\n",i,j) )

                                    j= i++ <=6;

                        return 0;

            }



            Output:        
        3 1
        4 1
        5 1
        6 1
        7 1
        8 0


Q4: What is the output of the following code?


            int main()

            {

                        char loop=1;

                        while ( loop<= 255 )

                        {

                                    printf("%d ", loop++ );

                        }                     

                        return 0;

            }

           


            Output:
      1 2 3 ... 126 127 -128 -127 ... -2 -1 0 1 2 3 ... 127 -128 -127 ... -2 -1 .... Infinite Times 




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

Control Instructions ( Part2 )

Control Instructions (C-FAQ's)



Q1: What is the output for the program given below?


            #include<stdio.h>

            int main()

            {

                        int x=0, y=10, z=20;

                        *( (x) ? &y: &z) = x ? y : z ;

                        printf(" %d %d %d\n ", x, y, z );

                                 

                        return 0;

            }


           

            Output:        
                0 10 20
                               


Q2: Predict the output for the following code ?

          #include<stdio.h>

            int main()

            {

                   int a=2, b=5;

                        if( a%2 = b%5 )

                                    printf("Hello World\n");

                       

            return 0;

            }

 

            A.        Error: " Invalid Rvalue assignment "

            B.        Error: "Expression Syntax"

            C.        Error: "Invalid Lvalue assignment"

            D.        Hello World



            Output:        
                        C.        Error: "Invalid lvalue assignment"



Q3: What is the output of the following code?

          #include<stdio.h>

            int main()

            {

                        int i = 5;

                        float j = 5.0 ;

                        if (  i == j )

                                    printf(" Equal \n");

                        else

                                    printf("Not Equal \n");

                        return 0;

            }



            Output:        
                Equal   
 


Q4: What is the output of the following code?


            int main()

            {

                        int x=0, y=5;

                        y == 5 ? x=10 : x=20 ;

                        if( x )

                        {

                                    printf("%d ", x );

                        }                     

                        return 0;

            }

 

            A.        10

            B.        20

            C.        No output

            D.        Invalid lvalue is assignment

               

           


            Output:
                        D.        Invalid lvalue is assignment




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

Control Instructions ( Part3 )

Control Instructions (C-FAQ's)



Q1: What is the output for the program given below?


            #include<stdio.h>

            int main()

            {

                        int x= 10, y= 50;

                        if ( ! ( !x ) && x )

                                    printf("x= %d\n ", x );

                        else

                                    printf("y= %d\n",y);

                                 

                        return 0;

            }

 

            A.        y=50

            B.        x=10

            C.        x=0

            D.        x=-10


           

            Output:        
                B.      x=10
                               


Q2: Predict the output for the following code ?

          #include<stdio.h>

            void func()

            {         

                        start:

                                    printf("In func() \n");

               

            };

            int main()

            {

                        int iCtr=1;

                        while( iCtr< 5)

                        {

                                printf("Hello World\n");

                                    goto start;

                        }

                       

            return 0;

            }

 

           


            Output:        
           In function 'main':
               Line 14: error: label 'start' used but not defined

                        goto cannot take control to a function other than the one in which it is defined. goto label should be defined within the same function.



Q3: What is the output of the following code?

          #include<stdio.h>

            int main()

            {

                        int i = 0;

                        if (  i = printf("") )

                                    printf(" Always comes here \n");

                        else

                                    printf(" Oops !!! \n");

                        return 0;

            }



            Output:        
                Opps !!!       
 
                        Because printf() returns the number of characters printed. Here it returns 0, because we are trying to print an empty string.

Q4: What is the output of the following code?


            int main()

            {

                        int x=1;

                        switch( x )

                        {

                                    printf(" Hello \n");

                             default :

                                                printf(" Bye \n");

                                    case 1:

                                                printf(" World \n");

                                                break;

                        }                     

                        return 0;

            }

 

            A.        Hello

                        Bye

                        World


            B.        Bye

                        World

            C.        World

            D.        Hello

                        World

               

           


            Output:
                        C.        World



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



Control Instructions ( Part4 )

Control Instructions (C-FAQ's)



Q1: What is the output for the program given below?


            #include<stdio.h>

            int main()

            {

                        int x= 1, y= 2;

                        switch ( x )

                        {

                                    case 1:

                                                printf("In, Case 1\n");

                                    case y:

                                                printf("In, Case y\n");

                                                break;

                        }                               

                        return 0;

            }

 

                       

            Output:
                        Compiler will return an error because variable are not allowed in case.


Q2: Predict the output for the following code ?

          #include<stdio.h>

            int main()

            {

                        float  f = 0.5;

                        if( 0.5 > f )

                                    printf(" 0.5 is greater than f \n");

                        else

                                    printf("0.5 is equal to f \n");

                       

            return 0;

            }

 

           

            Output:        
      0.5 is equal to f 
           


Q3: What is the output of the following code?

          #include<stdio.h>

            int main()

            {

                        int i = 10, j=20;

                        if (  i=5 ) || if  (j=30 )

                                    printf(" Values has been changed\n");

                        else

                                    printf(" Nothing changed !!! \n");

                        return 0;

            }



            Output:        
        In function 'main':
        Line 5: error: expected expression before '||' token  


Q4: What is the output of the following code?


            int main()

            {

                        int loop=5;

                        while( loop-- >=0  )

                                    printf("%d",loop);

                        loop=5;

                        printf("\n");

                        while( loop-- >=0 )

                                    printf("%d",loop);

                        printf("\n");

                        while( loop-- >=0 )

                                    printf("%d",loop);

                        printf("\n");

                        return 0;

            }

 

            A.        43210-1

                        43210-1

                       


            B.        543210

                        543210


            C.        Error

            D.        543210

                        543210

                        543201

               

           


            Output:
                        A.        43210-1
                                    43210-1



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


Declaration and Initialization ( Part2 )

Declaration and Initialization (C-FAQ's)



Q1: Differences between a Declaration and a Definition ?


            When we define a variable, a space is reserved for the variable and it also got some default value. But declaration only identifies the type of a variable.
               
                Redefinition is an error. But re-declaration is not an error.
               
                Example:-
                        extern int iNum;
                        float fVar;
                                    Here extern int iNum is a declaration and float fVar is definition.

 


Q2: Can you identify which of the following are declarations and definition ?

          extern int iNum;

            double dVar;

            void vFunc(int, float);

            int getVar() {}


            Output:        
                        extern int iNum       is a declaration
                        double dVar             is a definition
                        void vFunc(int, float)        is a declaration
                        int getVar() {}          is definition


Q3: Will the following code compile fine. If it is what will be the output ?

          #include<stdio.h>

            int main()

            {

                        extern int iNum;

                        printf("%d\n",iNum);

                        return 0;

            }

            int iNum=30;

 


            Output:        
                                There is no error in the above code, this will compile fine.
                        It prints 30.

Q4: What is the output of the following code?

          #include<stdio.h>

            int main()

            {

                        extern float fNum;

                        printf("%f\n",fNum);

                        return 0;

            }

           


            Output:        
                                Compiler will return an error "Undefined fNum". Because extern float fNum is a declaration not a definition.




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