c puzzles by raju(srk)

Upload: srkmnr

Post on 29-May-2018

226 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/9/2019 c Puzzles by Raju(Srk)

    1/40

    simple

    C Puzzles

  • 8/9/2019 c Puzzles by Raju(Srk)

    2/40

    main(){int i=400,j=300;

    printf("%d%d");

    }ans:

  • 8/9/2019 c Puzzles by Raju(Srk)

    3/40

    Answer:400300

    Explanation:

    printf takes the values of the first two

    assignments of the program.

    Any number of printf's may be given. All

    of them take only the first two values.

    If more number of assignments given in

    the program,then printf will take garbage

    values.

  • 8/9/2019 c Puzzles by Raju(Srk)

    4/40

    #define ABC 200

    #define XYZ 10#define XXX ABC / XYZ

    void main()

    {

    int a;

    a = XXX * 10;printf("%d ", a);

    }ans:

  • 8/9/2019 c Puzzles by Raju(Srk)

    5/40

    Ans:2

    a = xxx * 10which is => a = ABC/XYZ * 10

    => a = 200 /10 * 10

    => a = 200 / 100

    => a = 2

  • 8/9/2019 c Puzzles by Raju(Srk)

    6/40

    main()

    {printf("\nab");

    printf("\bsi");

    printf("\rha");

    }ans:

  • 8/9/2019 c Puzzles by Raju(Srk)

    7/40

    Answer:hai

    Explanation:

    \n - newline

    \b - backspace

    \r - linefeed

  • 8/9/2019 c Puzzles by Raju(Srk)

    8/40

    main()

    {

    int c=- -2;

    printf("c=%d",c);

    }ans:

  • 8/9/2019 c Puzzles by Raju(Srk)

    9/40

    Ans: c=2

    Explanation:

    Here unary minus (or

    negation) operator is used

    twice. Same maths rules

    applies, ie. minus * minus=plus.

  • 8/9/2019 c Puzzles by Raju(Srk)

    10/40

    #define square(x) x*x

    main(){

    int i;

    i = 64/square(4);

    printf("%d",i);

    }ans:

  • 8/9/2019 c Puzzles by Raju(Srk)

    11/40

    Answer:

    64i=64/x*x

    i=64/4*4

    i=16 *4

    i=64

  • 8/9/2019 c Puzzles by Raju(Srk)

    12/40

    main()

    {int i;

    printf("%d",scanf("%d",&i));

    // value 10 is given as

    input here

    }ans:

  • 8/9/2019 c Puzzles by Raju(Srk)

    13/40

    ans:1

    Scanf returns number ofitems successfully read.

    Here 10 is given as input

    which should have been

    scanned successfully. So

    number of items readis 1.

  • 8/9/2019 c Puzzles by Raju(Srk)

    14/40

    #define int char

    main(){

    int i=65;

    printf("sizeof(i)=%d",sizeof

    (i));

    }ans:

  • 8/9/2019 c Puzzles by Raju(Srk)

    15/40

    Ans:sizeof(i)=1

    Since the #define replaces

    the string int by the macro

    char and 65 treated as

    single.

  • 8/9/2019 c Puzzles by Raju(Srk)

    16/40

    Void main()

    {int x=4,y,z;

    y=--x;

    z=x--;

    printf(%d %d %d,x,y,z);

    }ans:

  • 8/9/2019 c Puzzles by Raju(Srk)

    17/40

    Ans:2 3 3

    y=--x (x=3,y=3)

    z=x-- (z=3,x=2)

  • 8/9/2019 c Puzzles by Raju(Srk)

    18/40

    void main()

    {

    printf(%c,abcdef[4]);

    }ans:

  • 8/9/2019 c Puzzles by Raju(Srk)

    19/40

    Ans:e

  • 8/9/2019 c Puzzles by Raju(Srk)

    20/40

    void main()

    {int x=4,y=2,z;

    z=x-- -y;

    printf(%d %d %d,x,y,z);}ans:

  • 8/9/2019 c Puzzles by Raju(Srk)

    21/40

    Ans:3 2 2

  • 8/9/2019 c Puzzles by Raju(Srk)

    22/40

    #include

    void main (){ int i=1;

    switch(i-2)

    { case -1:printf(a);case 0:printf(b);

    case 1:printf(c);

    default:printf(none);

    }

    }ans:

  • 8/9/2019 c Puzzles by Raju(Srk)

    23/40

    Ans:abcnone

  • 8/9/2019 c Puzzles by Raju(Srk)

    24/40

    #include

    void main (){ int i=1;

    switch(i)

    { printf(b);case 1:printf(a);break;

    case 2:printf(aa);break;

    }

    }ans:

  • 8/9/2019 c Puzzles by Raju(Srk)

    25/40

    Output:a

  • 8/9/2019 c Puzzles by Raju(Srk)

    26/40

    #include

    void main(){

    float a=3.15555;

    printf(\n%5.4f,a);printf(\n%0.0f,a);

    }ans:

  • 8/9/2019 c Puzzles by Raju(Srk)

    27/40

    Output:

    3.15553

  • 8/9/2019 c Puzzles by Raju(Srk)

    28/40

    #include

    void main(){

    int a=500,b=100,c;

    if(!a>=400)b=300;

    c=200;

    printf(b=%d c=%d,b,c);

    }ans:

  • 8/9/2019 c Puzzles by Raju(Srk)

    29/40

    Output:b=100 c=200

    (!a>=400)==(a

  • 8/9/2019 c Puzzles by Raju(Srk)

    30/40

    Void main()

    {int x;

    for(x=-1;x

  • 8/9/2019 c Puzzles by Raju(Srk)

    31/40

    Ans:0 times

  • 8/9/2019 c Puzzles by Raju(Srk)

    32/40

    #include

    void main(){ int a[5]={5,1,15,20,25};

    int i,j,k;

    i=++a[1];j=a[1]++;

    k=a[i++];

    printf(\n%d %d %d,i,j,k);

    }ans:

  • 8/9/2019 c Puzzles by Raju(Srk)

    33/40

    Output:

    3 2 15++a[1]=2=i

    a[1]=2=j& ++ a[1]=3

    m=a[2]=15 & i++ ie,i=3

  • 8/9/2019 c Puzzles by Raju(Srk)

    34/40

    #include

    main()

    {

    int a = 5;

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

    } ans:

  • 8/9/2019 c Puzzles by Raju(Srk)

    35/40

    Output:5 7

  • 8/9/2019 c Puzzles by Raju(Srk)

    36/40

    What is the output of the following

    code?# include

    #define a 10

    main()

    { printf("%d..",a);foo();

    printf("%d",a);

    }

    void foo(){

    #undef a

    #define a 50

    }ans:

  • 8/9/2019 c Puzzles by Raju(Srk)

    37/40

    Output:10..10

  • 8/9/2019 c Puzzles by Raju(Srk)

    38/40

    #include

    void main(){

    int I=3,*j,**k;

    j=&I;k=&j;

    printf("%d%d%d",*j,**k,*(*k)

    );

    }ans:

  • 8/9/2019 c Puzzles by Raju(Srk)

    39/40

  • 8/9/2019 c Puzzles by Raju(Srk)

    40/40

    P

    resentationbym.n.raju

    (08X41A1230)