| | |
Expression same but different results why
![]() |
•
•
Join Date: Dec 2007
Posts: 1
Reputation:
Solved Threads: 0
I am Anwar as a intermediate level currently programming practice in c, c++ and java . In following programs I confused in these expressions that expressions are same but result are different on these results few teachers said it is the problem of compilers.
In Turbo C 3.0
In Visual C++ 6.0
In Java 1.3
In Turbo C 3.0
main()
{int x=-250;
x = --x + --x + --x;
printf("%d",x);
}//it give -259In Visual C++ 6.0
#include<iostream>
using namespace std;
main()
{int x=-250;
x = --x + --x + --x;
cout <<x;
}//it give -257In Java 1.3
class Test
{
public static void main(String abc[])
{int x=-250;
x = --x + --x + --x;
System.out.print(x);
}//it give -256 > on these results few teachers said it is the problem of compilers.
No, it's a problem with the teachers not knowing the language rules well enough.
For C and C++, multiple side effects on the same variable has ALWAYS BEEN UNDEFINED.
What does that mean, it means the compiler is free to interpret it however it wants (or maybe even not at all). Any answer you get is just like rolling a dice.
http://c-faq.com/expr/index.html
- my answer is -256
- your answer is -257
- his hard disk has been reformatted.
As for Java, I've no idea whether the Java standard makes any claims about the evaluation order. It's certainly not comparable to C or C++.
The bottom line is that the code is broken. Don't use such constructs in your program, and don't waste time trying to figure out the mysteries of how your compiler seems to work.
No, it's a problem with the teachers not knowing the language rules well enough.
For C and C++, multiple side effects on the same variable has ALWAYS BEEN UNDEFINED.
What does that mean, it means the compiler is free to interpret it however it wants (or maybe even not at all). Any answer you get is just like rolling a dice.
http://c-faq.com/expr/index.html
- my answer is -256
- your answer is -257
- his hard disk has been reformatted.
As for Java, I've no idea whether the Java standard makes any claims about the evaluation order. It's certainly not comparable to C or C++.
The bottom line is that the code is broken. Don't use such constructs in your program, and don't waste time trying to figure out the mysteries of how your compiler seems to work.
![]() |
Similar Threads
- regular expression (PHP)
- Open In New Window Php (PHP)
- Syntax for adding two columns and sorting the results (MySQL)
- What Certifications do You Have? (IT Professionals' Lounge)
- What relation does **indirection operator have with Multidimensional Arrays (C++)
- program (C)
- Spyware complete with EULA (IT Professionals' Lounge)
- write a program that simulates rooling a pair of dice. (Java)
- Data Abstraction (Computer Science)
- For those of you who need a website database! (ASP.NET)
Other Threads in the C++ Forum
- Previous Thread: MP3 Playback in Visual C++
- Next Thread: IOS ::binary
| Thread Tools | Search this Thread |
ace_thread api array based binary bitmap borland c++ c/c++ calling char class classes code coding compile console conversion count delete delete-line deploy desktop developer directshow dll download dynamic dynamiccharacterarray email embedded encryption error file forms fstream function functions game givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream information input int integer java lib linkedlist linker loop looping loops map math mathhomeworkhelp matrix memory multiple news node number numbertoword output parameter pointer problem program programming project python random read recursion reference reverse richedit rpg string strings temperature template test text text-file tree url variable vector video win32 windows winsock word wordfrequency wxwidgets






