Saturday, 5 July 2014

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 >>



No comments:

Post a Comment