Difference Between C++ and Visual C++

S

Sonia

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!!!!!!
 
A

Alf P. Steinbach

* (e-mail address removed) (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.
 
J

John Harrison

Sonia said:
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
 
K

Kevin Goodsell

Sonia said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,164
Messages
2,570,898
Members
47,439
Latest member
shasuze

Latest Threads

Top