Friday 14 October 2011

Chapter 2: [A] What would be the output of the following programs:


(a) 

main( )
{
    int   a = 300, b, c ;
    if ( a >= 400 )
    b = 300 ;
    c = 200 ;
    printf ( "\n%d %d", b, c ) ;
}

output

garbage-value 200




(b)

main( )
{
    int   a = 500, b, c ;
    if ( a >= 400 )
    b = 300 ;
    c = 200 ;
    printf ( "\n%d %d", b, c ) ;
}

output

200 300



(c)

main( ) 
    int x = 10, y = 20 ; 
    if ( x == y ) ; 
    printf ( "\n%d %d", x, y ) ; 

output

10  20



(d)

main( ) 
   int x = 3, y = 5 ; 
   if ( x == 3 ) 
   printf ( "\n%d", x ) ; 
   else
   printf ( "\n%d", y ) ; 

output  
 
5





(e) 

main( ) 
    int x = 3 ; 
    float y = 3.0 ; 
    if ( x == y ) 
    printf ( "\nx and y are equal" ) ; 
    else
    printf ( "\nx and y are not equal" ) ;


output

x and y are equal



(g) 

main( ) 
   int k = 35 ; 
   printf ( "\n%d %d %d", k == 35, k = 50, k > 40 ) ; 

output

0  50  0



(h)

main( ) 
int i = 65 ; 
char j = 'A' ; 
if ( i == j ) 
printf ( “C is WOW” ) ; 
else 
printf( "C is a headache" ) ; 

output 

C is WOW 




(i) 

main( ) 
   int a = 5, b, c ; 
   b = a = 15 ; 
   c = a < 15 ; 
   printf ( "\na = %d b = %d c = %d", a, b, c ) ;


output

a = 15 b = 15 c = 0




(j) 

main( ) 
   int x = 15 ; 
   printf ( "\n%d %d %d", x != 15, x = 20, x < 30 ) ; 


output

1  20  1



14 comments:

  1. @Promod.. In Printf compiler work right to left.
    Then first operation is x<30 this is true that mean output is 1.
    2nd opertn is x=20 then output is 20 and now x value is 20.
    3rd op is x¡=15 , x is not equal to 15, this is true, then output is 1.
    Final aus is 1,20,1

    ReplyDelete
  2. hi friend in c we fallow generally standered calling convension which decides 2 things
    1. the order in which arguments are passed to a fn
    2. the cleanup of stack elements former to store formal arguments and local variable
    in c using standard calling convention arguments are passed in right to left order
    so initially we have x=15
    hence x<20 is true ie non zero value 1
    again x becomes 20
    third
    20is not equal to 15 true ie non zero value 1
    and these are printed in order 1 20 1

    ReplyDelete
  3. How wow gets printed in h. Please help.

    ReplyDelete
    Replies
    1. See the ascii code of character 'A'.
      The ascii code of character'A' is 65.
      Therefore output get printed by wow.

      Delete
  4. In case of program b,
    Output is not 200 300
    Correct output is: 300 200

    ReplyDelete
  5. This comment has been removed by the author.

    ReplyDelete
  6. This comment has been removed by the author.

    ReplyDelete
  7. Where is the solution of question(f)?
    (f) main()
    {
    int x=3,y,z;
    y=x=10;
    z=x<10;
    printf("\nx=%d y=%d z=%d",x,y,z);
    }

    ReplyDelete
    Replies
    1. initially x gets assigned a value 10, then the value stored in x is copied into y. so x=y=10. now constant 10 is compared with the value in x (x too holds 10). both are equal. hence condition evaluates to false and a value 0 is assigned to z.so answr is x=10,y=10,z=0.

      Delete
  8. Hi, in C we execute the code from right to left so in this case first condition in right is X < 30, yes 15<30 so its true (1), the second order is X = 20 now X's value is 20 the last condition is X != 15,X's value already changed from 15 to 20 so this condition is true as well (1)

    ReplyDelete
  9. Replies
    1. 0 50 0 refer solution for (j) in comments

      Delete