473,385 Members | 1,830 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,385 software developers and data experts.

C, C++ and Java Expression same but results different

In Turbo C 3.0
#include<conio.h>
#include<stdio.h>
void main(void)
{
int x=-250;
x = --x + --x + --x;
printf("%d",x);
getch();
}//it gives: -259

In Visual C++ 6.0
#include<iostream>
using namespace std;
void main(void)
{
int x=-250;
x = --x + --x + --x;
cout <<x;
}//it gives: -257

In Java
class Test
{
public static void main(String abc[])
{
int x=-250;
x = --x + --x + --x;
System.out.print(x);
}//it gives: -256
Dec 3 '07 #1
3 1201
sicarie
4,677 Expert Mod 4TB
Cool.

Next time you post, please be sure to follow the Posting Guidelines which ask that you do things like use code tags, etc...
Dec 3 '07 #2
In Turbo C 3.0
#include<conio.h>
#include<stdio.h>
void main(void)
{
int x=-250;
x = --x + --x + --x;
printf("%d",x);
getch();
}//it gives: -259

In Visual C++ 6.0
#include<iostream>
using namespace std;
void main(void)
{
int x=-250;
x = --x + --x + --x;
cout <<x;
}//it gives: -257

In Java
class Test
{
public static void main(String abc[])
{
int x=-250;
x = --x + --x + --x;
System.out.print(x);
}//it gives: -256


I think you mean they give 75* not 25* but anyway

It is based on the order of operations specific to compilers

If Turbo does give 759 and not 259 then it will always perform the decrement operation first on before doing anything resulting in -253 + -253 + -253

In Visual C++ they do the same thing but only look at 2 items at a time so it will take the last items, do the decrements then add them together so you have -250 decremented, then -251 decremented resulting in -252 + -252 giving -504. -504 is then added to x decremented which is -504 + -253 giving -757

Java decrements then adds so x is decremented to -251 and added to that decremented -252 which is added to that decremented -253 resulting in -756

Parenthesis would help resolve how you want it evaluated
Dec 3 '07 #3
weaknessforcats
9,208 Expert Mod 8TB
x = --x + --x + --x;
Your results are indeterminate.

Variables can be modified only once in a statement. More than that and your results are unpredictable.
Dec 4 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

73
by: RobertMaas | last post by:
After many years of using LISP, I'm taking a class in Java and finding the two roughly comparable in some ways and very different in other ways. Each has a decent size library of useful utilities...
23
by: Paul Rubin | last post by:
OK, I want to scan a file for lines matching a certain regexp. I'd like to use an assignment expression, like for line in file: if (g := re.match(pat, line)): croggle(g.group(1)) Since...
11
by: DrUg13 | last post by:
In java, this seems so easy. You need a new object Object test = new Object() gives me exactly what I want. could someone please help me understand the different ways to do the same thing in...
3
by: JL | last post by:
I have a VB.NET desktop program that reads/writes data to a server using a Java-based Web Service. This web service, in identical formats, is located on several servers with each server being a...
1
by: David Van D | last post by:
Hi there, A few weeks until I begin my journey towards a degree in Computer Science at Canterbury University in New Zealand, Anyway the course tutors are going to be teaching us JAVA wth bluej...
458
by: wellstone9912 | last post by:
Java programmers seem to always be whining about how confusing and overly complex C++ appears to them. I would like to introduce an explanation for this. Is it possible that Java programmers...
1
by: Piet71 | last post by:
Hi, I have a stylesheet to select nodes from a source document via their "position" attribute. The template snippet is as follows: <xsl:template match="counter"> <xsl:if test="$number"><!--@pos=2...
5
by: r035198x | last post by:
Setting up. Getting started To get started with java, one must download and install a version of Sun's JDK (Java Development Kit). The newest release at the time of writting this article is...
2
by: jtanz0 | last post by:
Im fairly new to java programming although i have some experience in python. I have to create a calculator program (command line) which takes as input a mathematical expression consisting of...
318
by: King Raz | last post by:
The shootout site has benchmarks comparing different languages. It includes C# Mono vs Java but not C# .NET vs Java. So I went through all the benchmark on the site ... ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.