473,480 Members | 1,885 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 3392
* 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
2501
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
2384
by: ad | last post by:
Hi, What is the difference between Visual Studio.NET 2005 ©MVisual Web Developer 2005 Express ?
12
17349
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
19803
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
1612
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
7052
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
7092
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...
1
6744
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5348
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4790
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4488
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
2989
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
565
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
188
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.