trick tracer

download trick tracer

of 22

Transcript of trick tracer

  • 8/7/2019 trick tracer

    1/22

  • 8/7/2019 trick tracer

    2/22

    Trick Tracer :----A place where u can show ur

    skills of c , c++ ;trace the ouput and find the

    error(if it occurs )

  • 8/7/2019 trick tracer

    3/22

    Platform :- gcc compiler

    Notification

  • 8/7/2019 trick tracer

    4/22

    Q. 1 :- what is the output of the program ?

    #includeint main(){

    int j=10;for(j--;j--;j--)printf(%d%d,j,j-- - --j);return 0;

    }

  • 8/7/2019 trick tracer

    5/22

    Output :-

    6 0 2 0 -2 0 ..... infinite

  • 8/7/2019 trick tracer

    6/22

    Q. 2 :- what is the output of the program ?

    #includeint main()

    { int a=3, b=4;printf(%d,(a+b)++);return 0;

    }

  • 8/7/2019 trick tracer

    7/22

    Output :- 7

    Explanation :-

    Its an error becausean expression returns a constantvalue and u can not increment theconstant value.

  • 8/7/2019 trick tracer

    8/22

    Q. 3 :- what is the output of the program ?

    #includeint main()

    { int i;for(;scanf(%d,&i);printf(%d,i))return 0;

    }

  • 8/7/2019 trick tracer

    9/22

    Output :-

    Explanation :-

    This will result in an infinite loop,since the order of execution isinitialization condition check then loopbody and then increment

  • 8/7/2019 trick tracer

    10/22

    Q. 4 :- what is the output of the program ?

    #includeint main(){

    for(;NULL;){printf(hello);}

    return 0;}

  • 8/7/2019 trick tracer

    11/22

    Output :-

    Explanation :-

    It will not print any thingbecause NULL is by default declared aszero so test condition in for loop becomesfalse.

  • 8/7/2019 trick tracer

    12/22

    Q. 5 :- what is the output of the program ?

    #includeint main(){unsigned int y = 12;

    int x = -2;if(x>y)

    printf ("x is greater");else

    printf ("y is greater");

    return 0;}

  • 8/7/2019 trick tracer

    13/22

    Output :- x is greater

    Explanation :-This code produces an

    outstandingly amazing result "x is greater" even though yis positive and x is negative and right from our birth we aretaught a positive number is always greater than a negative

    number. So why such a result in programming, you may bewondering after all computers are definitely better incalculation then we are.The reason for such a result is that both x and y are ofslightly different type. X is signed integer while y is anunsigned integer. Now when the computer compares both

    of them x is promoted to an unsigned integer type.When x is promoted to unsigned integer -2

    becomes 65534 which is definately greater than 12. Soresult is produced x is greater than y.

  • 8/7/2019 trick tracer

    14/22

    Q. 6 :- what is the output of the program ?

    #include int main(){int a, b,c, d;

    a=3;b=5;c=a,b;d=(a,b);printf("c = %d" ,c);printf("\n\nd = %d" ,d);

    return 0;}

  • 8/7/2019 trick tracer

    15/22

    Output :- c=3, d=5

    Due to the use of comma operator theright hand side of the variable data isassigned to the variable hence whatever is the data in the b it will beassigned into c ,similar is the reason for

    the next case.

  • 8/7/2019 trick tracer

    16/22

    Q. 7 :- what is the output of the program ?

    #includeint main(){

    int i=3;int j;j = sizeof(++i + ++i);

    printf(" i = %d\n\n j = %d", i ,j);return 0;

    }

  • 8/7/2019 trick tracer

    17/22

    Output :- j=2, i=3

    Explanation -

    Most important thing to not is 'sizeof'is operator who returns a unsigned integer of typesize_t which represents size in bytes require to storethe expression in the memory. The thing is that onlyend result of expression type is counted and theexpression being operated on is not evaluated so (++i + ++i) is never actually executed so value of iremains unchange as a result value of i is displayedas its initialization value 3.

  • 8/7/2019 trick tracer

    18/22

    Q. 8 :- what is the output of the program ?

    #include

    int main(){char *p;

    char buf[10] = { 1,2,3,4,5,6,9,8} ;p = (buf+1)[5];printf("%d" , p);return 0;}

  • 8/7/2019 trick tracer

    19/22

    Output :- 9

    Explanation :-Simple question considering

    the fact that in C programming language thebase address of the array is the name of thearray without brackets. Now (buf+1) [5] isexactly same as (buf+6). When this memory

    location is accessed it returns the valuestored in the seventh position in the arrayand that would be 9.

  • 8/7/2019 trick tracer

    20/22

    Q. 9 :- what is the output of the program ?

    #includeint main( )

    {printf("%d",printf("%d %d",5,5)&printf("%d %d",7,7));return 0;}

  • 8/7/2019 trick tracer

    21/22

    Output :- 7 7 5 5 3

    Explanation :-First right handed expression

    is evaluated then the control moves to theleft, both the expression returns the value i.e.number of symbols printed by the printfstatement here 3 and 3 is returned anded by

    bitwise hence result is obtained.

  • 8/7/2019 trick tracer

    22/22

    Q. 10 :- what is the output of the program ?

    #includeint main( ){

    int i;i=printf("%d %d",3,3);printf(" %d",printf( %d %d,7,7),i);return 0;}