473,387 Members | 1,864 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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"<="89"<<endl;
}
Thanks for the help!!!!!!
Jul 22 '05 #1
6 3391
* 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
* 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

"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

"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: xman | last post by:
very hard is fix exactly group for this question.. but I am sure that some of people here have some experience with these compilers: Visual Studio NET Enterprise Architect 2002 and Visual Studio...
5
by: ad | last post by:
Hi, What is the difference between Visual Studio.NET 2005 ©MVisual Web Developer 2005 Express ?
12
by: Nathan Sokalski | last post by:
What is the difference between the Page_Init and Page_Load events? When I was debugging my code, they both seemed to get triggered on every postback. I am assuming that there is some difference,...
1
by: bharathreddy | last post by:
This Article gives an introduction to VSTS Team Foundation & fundamental difference between Visual Source Safe (VSS) and VSTS Team Foundation. Team Foundation is a set of tools and technologies...
1
by: =?Utf-8?B?RnJpZWRp?= | last post by:
Hi everybody, I am studying VB 2005 express and think, very interesting projects can be developed with it. Before I step over and buy the VB full version, can anybody tell me the difference of...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.