473,545 Members | 1,859 Online
Bytes | Software Development & Data Engineering Community
+ 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"<="8 9"<<endl;
}
Thanks for the help!!!!!!
Jul 22 '05 #1
6 3396
* so**********@ho tmail.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"<="8 9"<<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**********@ho tmail.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"<="8 9"<<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**********@h otmail.com> wrote in message
news:3f******** *************** ***@posting.goo gle.com...
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"<="8 9"<<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","8 9")<<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**********@h otmail.com> wrote in message
news:3f******** *************** ***@posting.goo gle.com...
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"<="8 9"<<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","8 9")<<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"<="8 9"<<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"<="8 9"<<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
2511
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 NET 2003 1. What is difference between Visual Studio NET Enterprise Architect 2002 and Visual Studio NET 2003 2. If I try user Visual Studio NET...
5
2388
by: ad | last post by:
Hi, What is the difference between Visual Studio.NET 2005 ©MVisual Web Developer 2005 Express ?
12
17363
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, and I would like to know what it is so that I can take advantage of it in my code. Thanks. -- Nathan Sokalski njsokalski@hotmail.com...
1
19814
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 that enable a team to collaborate and coordinate a project (software or non software projects). The team collaboration is achieved by several tools...
1
1618
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 possibilities VB offers in comparison to Visual C ++ or tell me whare I could find more info on it? -- Thanks for clarification Friedi
0
7486
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7776
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6001
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5347
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4965
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3473
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3456
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1032
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
729
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.