6th c++ Problem

Manish Goyal
@manish-r2Hoep โ€ข Oct 21, 2024

have a look of this code

#include<iostream.h>
#define square(x)x*x
void main()
{
int b,a=4;
b=64/square(a);
cout<<b;
}

Tell me the output...?and if it gives wrong output then give me correct output?
ie i want b=4

Replies

Welcome, guest

Join CrazyEngineers to reply, ask questions, and participate in conversations.

CrazyEngineers powered by Jatra Community Platform

  • Mangesh6688

    @mangesh6688-2pfEFZ • Jan 6, 2010

    The output of this program is 64 which is wrong.
    Because the #define square(x) x*x means it will replace the square(x) by x*x
    so in program b=64/square(a) will be replaced by b=64/a*a;
    As in c++ both / and * has equal priority the expression will be as follows:
    b=(64/4)*4; i.e. b=16*4 = 64

    So for getting correct output our program should be

    #include<iostream.h>
    #define square(x) (x*x)
    void main()
    {
    int b,a=4;
    b=64/square(a);
    cout<<b;
    }
    
  • Manish Goyal

    @manish-r2Hoep • Jan 6, 2010

    yes you are absolutely right

    and nice explanation mangesh