By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,691 Members | 1,134 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,691 IT Pros & Developers. It's quick & easy.

Difference Between C++ and Visual C++

P: n/a
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"<="89"<<endl;
}
Thanks for the help!!!!!!
Jul 22 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
* so**********@hotmail.com (Sonia) schriebt:

Why does C++ return a 0 and Visual C++ return a 1
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?
Why the difference if they are both MS products?
C++ is not a Microsoft product but a programming language standardized
by ISO.
#include <iostream>

using namespace std;

void main()
'main' must have return type 'int', even if Visual C++ erronously lets
you get away with 'void'.
{
cout<<"123"<="89"<<endl;
}

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?
Jul 22 '05 #2

P: n/a
* so**********@hotmail.com (Sonia) schriebt:

Why does C++ return a 0 and Visual C++ return a 1
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?
Why the difference if they are both MS products?
C++ is not a Microsoft product but a programming language standardized
by ISO.
#include <iostream>

using namespace std;

void main()
'main' must have return type 'int', even if Visual C++ erronously lets
you get away with 'void'.
{
cout<<"123"<="89"<<endl;
}

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?
Jul 22 '05 #3

P: n/a

"Sonia" <so**********@hotmail.com> wrote in message
news:3f**************************@posting.google.c om...
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?

C++ is language, not a MS product! Jesus wept!
#include <iostream>

using namespace std;

void main()
{
cout<<"123"<="89"<<endl;
I think you mean

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

what you wrote should not compile.
}


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","89")<<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
Jul 22 '05 #4

P: n/a

"Sonia" <so**********@hotmail.com> wrote in message
news:3f**************************@posting.google.c om...
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?

C++ is language, not a MS product! Jesus wept!
#include <iostream>

using namespace std;

void main()
{
cout<<"123"<="89"<<endl;
I think you mean

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

what you wrote should not compile.
}


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","89")<<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
Jul 22 '05 #5

P: n/a
Sonia wrote:
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"<="89"<<endl;
}
Thanks for the help!!!!!!


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.
Jul 22 '05 #6

P: n/a
Sonia wrote:
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"<="89"<<endl;
}
Thanks for the help!!!!!!


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.
Jul 22 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.