?output

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • jw

    #1

    ?output

    what is its output and why?
    #include<iostre am>

    using namespace std;


    #define abs(x) (x>0 ? x:-x)
    void main()
    {
    int a=3,b=5;
    cout<<abs(abs(a +b));
    }

  • Ivan Vecerina

    #2
    Re: ?output

    "jw" <jackwht@gmail. com> wrote in message
    news:1140101482 .397041.297420@ g14g2000cwa.goo glegroups.com.. .
    : what is its output and why?
    : #include<iostre am>
    :
    : using namespace std;
    :
    :
    : #define abs(x) (x>0 ? x:-x)
    : void main()
    : {
    : int a=3,b=5;
    : cout<<abs(abs(a +b));
    : }

    We don't do homework.
    Try substituting the macros, and see what happens.

    --
    http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form


    Comment

    • Dietmar Kuehl

      #3
      Re: ?output

      jw wrote:
      [color=blue]
      > #include<iostre am>
      >
      > using namespace std;
      >
      >
      > #define abs(x) (x>0 ? x:-x)
      > void main()
      > {
      > int a=3,b=5;
      > cout<<abs(abs(a +b));
      > }[/color]

      This program is not well-formed and should not compile in the first
      place. If it compiles without diagnostic your compiler is broken and
      thus its output is not a reliable source of information.
      --
      <mailto:dietmar _kuehl@yahoo.co m> <http://www.dietmar-kuehl.de/>
      <http://www.eai-systems.com> - Efficient Artificial Intelligence

      Comment

      • Victor Bazarov

        #4
        Re: ?output

        jw wrote:[color=blue]
        > what is its output and why?
        > #include<iostre am>
        >
        > using namespace std;
        >
        >
        > #define abs(x) (x>0 ? x:-x)
        > void main()
        > {
        > int a=3,b=5;
        > cout<<abs(abs(a +b));
        > }
        >[/color]

        What do you think the output is and why?

        Read the FAQ 5.2.

        Comment

        • jw

          #5
          Re: ?output


          Dietmar Kuehl wrote:[color=blue]
          > jw wrote:
          >[color=green]
          > > #include<iostre am>
          > >
          > > using namespace std;
          > >
          > >
          > > #define abs(x) (x>0 ? x:-x)
          > > void main()
          > > {
          > > int a=3,b=5;
          > > cout<<abs(abs(a +b));
          > > }[/color]
          >[/color]
          it is not a homework am on holiday and studying c++,this code gives 8
          as a result but i think it must give 2 i couldnt understand this

          Comment

          • Ben Pope

            #6
            Re: ?output

            jw wrote:[color=blue]
            > Dietmar Kuehl wrote:[color=green]
            >> jw wrote:
            >>[color=darkred]
            >>> #include<iostre am>
            >>>
            >>> using namespace std;
            >>>
            >>>
            >>> #define abs(x) (x>0 ? x:-x)
            >>> void main()
            >>> {
            >>> int a=3,b=5;
            >>> cout<<abs(abs(a +b));
            >>> }[/color][/color]
            > it is not a homework am on holiday and studying c++,this code gives 8
            > as a result but i think it must give 2 i couldnt understand this[/color]

            8 looks about right to me.

            On paper, do a+b
            then apply abs.
            then apply abs.

            What should "abs" conceptually do?
            What does your abs above do?

            Ben Pope
            --
            I'm not just a number. To many, I'm known as a string...

            Comment

            • jw

              #7
              Re: ?output

              aww sorry it is (a-b) and gives 8

              #define abs(x) (x>0 ? x:-x)
              void main()
              {
              int a=3,b=5;
              cout<<abs(abs(a-b));
              }

              Comment

              • Dietmar Kuehl

                #8
                Re: ?output

                jw wrote:[color=blue]
                > Dietmar Kuehl wrote:[color=green]
                >> jw wrote:[/color][/color]
                [...][color=blue][color=green][color=darkred]
                >> > void main()
                >> > {[/color][/color][/color]
                [...][color=blue][color=green][color=darkred]
                >> > }[/color][/color][/color]
                [color=blue]
                > it is not a homework am on holiday and studying c++,this code gives 8
                > as a result but i think it must give 2 i couldnt understand this[/color]

                Maybe you should start of studying English first: the above is not
                proper sentence. Of course, it doesn't claim to be as all periods
                are absent. Second, note that I haven't claimed that it is a
                homework assignment. I claimed that your code or even the above
                excerpt is illegal C++ which is backed up by basic.start.mai n
                (3.6.1), paragraph 2, second sentence which reads:

                It shall have a return type of type int, [...]

                The "It" refers to "main()". The stuff following the comma is
                irrelevant to the return type of 'main()'. Your turn!

                Apparently, you are more interested in the effect of your macros
                than the answer to your apparent question (well, strictly speaking
                there was no question; I assumed you would be interested in the
                output of the program and as I stated, the C++ standard makes no
                statement about the output of this malformed program). With
                respect to your macros, the actual answer is this: Why bother?
                There are uses for macros in C++ but this is none of them. Use a
                [n inline] function instead!
                --
                <mailto:dietmar _kuehl@yahoo.co m> <http://www.dietmar-kuehl.de/>
                <http://www.eai-systems.com> - Efficient Artificial Intelligence

                Comment

                • Ben Pope

                  #9
                  Re: ?output

                  jw wrote:[color=blue]
                  > aww sorry it is (a-b) and gives 8
                  >
                  > #define abs(x) (x>0 ? x:-x)
                  > void main()
                  > {
                  > int a=3,b=5;
                  > cout<<abs(abs(a-b));
                  > }
                  >[/color]

                  compare with:

                  #include <iostream>

                  #define abs(x) (x>0 ? x:-x)
                  int main() {
                  int a=3,b=5;
                  std::cout<<abs( (abs((a-b))));
                  }

                  macros are evil. Just use an inline function.

                  Ben Pope
                  --
                  I'm not just a number. To many, I'm known as a string...

                  Comment

                  • jw

                    #10
                    Re: ?output

                    i know i can use inline func. but i saw this code in a
                    competition,out put was asked and many of the competitors said 2 but the
                    ans. is 8 ?

                    Comment

                    • Dietmar Kuehl

                      #11
                      Re: ?output

                      jw wrote:[color=blue]
                      > aww sorry it is (a-b) and gives 8
                      >
                      > #define abs(x) (x>0 ? x:-x)
                      > void main()
                      > {
                      > int a=3,b=5;
                      > cout<<abs(abs(a-b));
                      > }[/color]

                      This program is still broken...

                      Of course, the macro use results in an expression evaluating to
                      "8"! What else should be result? Just write replace the macro
                      with the corresponding expression or, even better, have the
                      compiler do it for, usually using the "-E" switch. For example,
                      with gcc you can look at the preprocessed output using something
                      like

                      g++ -E tst.cpp

                      if you store the above code in tst.cpp (you will probably want
                      to omit the include directive to get concise output...).
                      --
                      <mailto:dietmar _kuehl@yahoo.co m> <http://www.dietmar-kuehl.de/>
                      <http://www.eai-systems.com> - Efficient Artificial Intelligence

                      Comment

                      • Dietmar Kuehl

                        #12
                        Re: ?output

                        jw wrote:[color=blue]
                        > i know i can use inline func. but i saw this code in a
                        > competition,out put was asked and many of the competitors said 2 but the
                        > ans. is 8 ?[/color]

                        Of course it is "8" (assuming the program is correct to have a
                        return type of 'int' for 'main()'). Macros just to textual
                        replacement and do not in any way affect operator precedence of
                        the resulting expression. Just do the textual replacement and you'd
                        see that the result is effectively "-(-a-b)" which, of course,
                        evaluates to "8" if "a" is "3" and "b" is "5".
                        --
                        <mailto:dietmar _kuehl@yahoo.co m> <http://www.dietmar-kuehl.de/>
                        <http://www.eai-systems.com> - Efficient Artificial Intelligence

                        Comment

                        • n2xssvv g02gfr12930

                          #13
                          Re: ?output

                          jw wrote:[color=blue]
                          > aww sorry it is (a-b) and gives 8
                          >
                          > #define abs(x) (x>0 ? x:-x)
                          > void main()
                          > {
                          > int a=3,b=5;
                          > cout<<abs(abs(a-b));[/color]
                          abs(a-b) expands to (a-b>0 ? a-b:-a-b)

                          abs(abs(a-b)) expands to

                          (a-b>0 ? a-b:-a-b)>0 ? (a-b>0 ? a-b:-a-b):-(a-b>0 ? a-b:-a-b)[color=blue]
                          > }
                          >[/color]

                          quite simply this is obviously a mess and not what was intended
                          but does give the result 8. Perhaps now you'll see why macros can
                          cause so much grief and should generally be avoided.

                          JB

                          Comment

                          • Arjun Thounaojam

                            #14
                            Re: ?output

                            ...and if you still insist on using macros, follow macro rule no. 2:
                            Always enclose your args in parentheses!
                            so instead of:
                            #define abs(x) (x>0 ? x:-x)
                            use:
                            #define abs(x) ((x)>0 ? (x):-(x))

                            -80

                            Comment

                            Working...