* (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.