Difference Between C++ and Visual C++

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

    Difference Between C++ and Visual C++

    Why does C++ return a 0 and Visual C++ return a 1 when I execute this
    program? Why the difference if they are both MS products?

    #include <iostream>

    using namespace std;

    void main()
    {
    cout<<"123"<="8 9"<<endl;
    }


    Thanks for the help!!!!!!
  • Alf P. Steinbach

    #2
    Re: Difference Between C++ and Visual C++

    * sonia_galvan@ho tmail.com (Sonia) schriebt:[color=blue]
    >
    > Why does C++ return a 0 and Visual C++ return a 1[/color]

    Visual C++ is Microsoft's implementation of the C++ language.

    C++ does not return anything.

    Perhaps what you mean is that some other implementation of C++ creates
    a program that gives 0 as output?


    [color=blue]
    >Why the difference if they are both MS products?[/color]

    C++ is not a Microsoft product but a programming language standardized
    by ISO.


    [color=blue]
    > #include <iostream>
    >
    > using namespace std;
    >
    > void main()[/color]

    'main' must have return type 'int', even if Visual C++ erronously lets
    you get away with 'void'.


    [color=blue]
    > {
    > cout<<"123"<="8 9"<<endl;
    > }[/color]


    The expression


    "123" <= "89"


    does not compare the strings "123" and "89".

    It compares pointers to those strings.

    What the pointer values are depends on where exactly in memory the strings
    are stored (if, in fact, they are stored anywhere). That can vary both with
    the compiler and the options you're specifying to the compiler. From a formal
    point of view what you have is the infamous Undefined Behavior (UB), because
    you're comparing two unrelated pointer values, and so any result, including
    that the program sends an angry e-mail to president Bush, is allowed.

    You can fix that easily by using std::string:


    #include <iostream>
    #include <string>

    int main()
    {
    std::string const a = "123";
    std::string const b = "89";

    std::cout << (a <= b) << std::endl;
    }


    In this case the expected result is '1' or 'true' (I actually havent't the
    foggiest notion of which of these two, but they mean the same), because when
    you sort the two strings alphabetically "123" comes before "89".

    If you want a numerical comparision you'll have to compare numbers instead.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is top-posting such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?

    Comment

    • Alf P. Steinbach

      #3
      Re: Difference Between C++ and Visual C++

      * sonia_galvan@ho tmail.com (Sonia) schriebt:[color=blue]
      >
      > Why does C++ return a 0 and Visual C++ return a 1[/color]

      Visual C++ is Microsoft's implementation of the C++ language.

      C++ does not return anything.

      Perhaps what you mean is that some other implementation of C++ creates
      a program that gives 0 as output?


      [color=blue]
      >Why the difference if they are both MS products?[/color]

      C++ is not a Microsoft product but a programming language standardized
      by ISO.


      [color=blue]
      > #include <iostream>
      >
      > using namespace std;
      >
      > void main()[/color]

      'main' must have return type 'int', even if Visual C++ erronously lets
      you get away with 'void'.


      [color=blue]
      > {
      > cout<<"123"<="8 9"<<endl;
      > }[/color]


      The expression


      "123" <= "89"


      does not compare the strings "123" and "89".

      It compares pointers to those strings.

      What the pointer values are depends on where exactly in memory the strings
      are stored (if, in fact, they are stored anywhere). That can vary both with
      the compiler and the options you're specifying to the compiler. From a formal
      point of view what you have is the infamous Undefined Behavior (UB), because
      you're comparing two unrelated pointer values, and so any result, including
      that the program sends an angry e-mail to president Bush, is allowed.

      You can fix that easily by using std::string:


      #include <iostream>
      #include <string>

      int main()
      {
      std::string const a = "123";
      std::string const b = "89";

      std::cout << (a <= b) << std::endl;
      }


      In this case the expected result is '1' or 'true' (I actually havent't the
      foggiest notion of which of these two, but they mean the same), because when
      you sort the two strings alphabetically "123" comes before "89".

      If you want a numerical comparision you'll have to compare numbers instead.

      --
      A: Because it messes up the order in which people normally read text.
      Q: Why is top-posting such a bad thing?
      A: Top-posting.
      Q: What is the most annoying thing on usenet and in e-mail?

      Comment

      • John Harrison

        #4
        Re: Difference Between C++ and Visual C++


        "Sonia" <sonia_galvan@h otmail.com> wrote in message
        news:3f83ed9d.0 404062028.34b15 745@posting.goo gle.com...[color=blue]
        > Why does C++ return a 0 and Visual C++ return a 1 when I execute this
        > program? Why the difference if they are both MS products?
        >[/color]

        C++ is language, not a MS product! Jesus wept!
        [color=blue]
        > #include <iostream>
        >
        > using namespace std;
        >
        > void main()
        > {
        > cout<<"123"<="8 9"<<endl;[/color]

        I think you mean

        cout<<("123"<=" 89")<<endl;

        what you wrote should not compile.
        [color=blue]
        > }
        >[/color]

        The problem with you program is that C++ language (not Microsoft) says that
        the result of "123"<="89" is undefined. It could be 0, it could be 1.
        Different compilers will produce different results. The same compiler at
        different times of the day could produce different results.

        It's hard to know what you are expecting from the above program, but what
        you program is doing is comparing pointers. It is not comparing strings, it
        is not comparing numbers. And because the pointer values of "123" and "89"
        are not defined some compilers will say 1 and others will say 0.

        Here's one way to compare strings.

        #include <iostream>
        #include <cstring>
        using namespace std;

        int main()
        {
        cout<< strcmp("123","8 9")<<endl;
        }

        that should always produce -1 because the string "123" is less than the
        string "89" (because "1" is less than "8").

        Here's one way to compare numbers

        #include <iostream>
        using namespace std;

        int main()
        {
        cout<< (123 <= 89)<<endl;
        }

        That's should always produce 0, because 123 is not less than or equal to 89
        (obviously).

        john


        Comment

        • John Harrison

          #5
          Re: Difference Between C++ and Visual C++


          "Sonia" <sonia_galvan@h otmail.com> wrote in message
          news:3f83ed9d.0 404062028.34b15 745@posting.goo gle.com...[color=blue]
          > Why does C++ return a 0 and Visual C++ return a 1 when I execute this
          > program? Why the difference if they are both MS products?
          >[/color]

          C++ is language, not a MS product! Jesus wept!
          [color=blue]
          > #include <iostream>
          >
          > using namespace std;
          >
          > void main()
          > {
          > cout<<"123"<="8 9"<<endl;[/color]

          I think you mean

          cout<<("123"<=" 89")<<endl;

          what you wrote should not compile.
          [color=blue]
          > }
          >[/color]

          The problem with you program is that C++ language (not Microsoft) says that
          the result of "123"<="89" is undefined. It could be 0, it could be 1.
          Different compilers will produce different results. The same compiler at
          different times of the day could produce different results.

          It's hard to know what you are expecting from the above program, but what
          you program is doing is comparing pointers. It is not comparing strings, it
          is not comparing numbers. And because the pointer values of "123" and "89"
          are not defined some compilers will say 1 and others will say 0.

          Here's one way to compare strings.

          #include <iostream>
          #include <cstring>
          using namespace std;

          int main()
          {
          cout<< strcmp("123","8 9")<<endl;
          }

          that should always produce -1 because the string "123" is less than the
          string "89" (because "1" is less than "8").

          Here's one way to compare numbers

          #include <iostream>
          using namespace std;

          int main()
          {
          cout<< (123 <= 89)<<endl;
          }

          That's should always produce 0, because 123 is not less than or equal to 89
          (obviously).

          john


          Comment

          • Kevin Goodsell

            #6
            Re: Difference Between C++ and Visual C++

            Sonia wrote:
            [color=blue]
            > Why does C++ return a 0 and Visual C++ return a 1 when I execute this
            > program? Why the difference if they are both MS products?
            >
            > #include <iostream>
            >
            > using namespace std;
            >
            > void main()
            > {
            > cout<<"123"<="8 9"<<endl;
            > }
            >
            >
            > Thanks for the help!!!!!![/color]

            Hotmail address + Google posting + bizarre claims about C++ being a
            Microsoft product + void main + bizarre syntax = likely troll, I think.

            -Kevin
            --
            My email address is valid, but changes periodically.
            To contact me please use the address from a recent posting.

            Comment

            • Kevin Goodsell

              #7
              Re: Difference Between C++ and Visual C++

              Sonia wrote:
              [color=blue]
              > Why does C++ return a 0 and Visual C++ return a 1 when I execute this
              > program? Why the difference if they are both MS products?
              >
              > #include <iostream>
              >
              > using namespace std;
              >
              > void main()
              > {
              > cout<<"123"<="8 9"<<endl;
              > }
              >
              >
              > Thanks for the help!!!!!![/color]

              Hotmail address + Google posting + bizarre claims about C++ being a
              Microsoft product + void main + bizarre syntax = likely troll, I think.

              -Kevin
              --
              My email address is valid, but changes periodically.
              To contact me please use the address from a recent posting.

              Comment

              Working...