CrazyEngineers
  • 6th c++ Problem

    Manish Goyal

    Manish Goyal

    @manish-r2Hoep
    Updated: Oct 21, 2024
    Views: 843
    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
    0
    Replies
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • Mangesh6688

    MemberJan 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;
    }
    
    Are you sure? This action cannot be undone.
    Cancel
  • Manish Goyal

    MemberJan 6, 2010

    yes you are absolutely right

    and nice explanation mangesh
    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register