This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes C / C++ and the fly likes Java and C Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Languages » C / C++
Bookmark "Java and C" Watch "Java and C" New topic
Author

Java and C

Ramesh Pramuditha Rathnayake
Ranch Hand

Joined: Oct 31, 2012
Posts: 169
    
    1



The final result of x is 100 in java. But in C it is 101.
I know what happens in java. But I don't understand why it's 101 in C.

Is this happened because, C don't have a Virtual Machine..?


Ramesh-X
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18546
    
  40

Ramesh Pramuditha Rathnayake wrote:

The final result of x is 100 in java. But in C it is 101.
I know what happens in java. But I don't understand why it's 101 in C.

Is this happened because, C don't have a Virtual Machine..?



If memory serves, and I could be wrong here, in C, the answer should be the same as in Java -- meaning the result should be 100. However, I believe this was clarified with a particular version of ANSI C (which I don't know which version at the moment). Prior to that, it was not defined, and the answer could either be 100 or 101 depending on what the compiler did.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38075
    
  22
A long time ago, I tried that sort of thing with three different compilers usingTwo printed 4 and one printed 5!

By the way, it would be helpful to know what the thread is about.
Ramesh Pramuditha Rathnayake
Ranch Hand

Joined: Oct 31, 2012
Posts: 169
    
    1

Thanks for the replies..

I got the answer. You both are trying to say that it is a work done by compiler...
As C compiler make machine code file, the result would be vary from compiler to compiler. But in java the Machine code is generated in JVM. Therefore the result may vary in Java from JVM to JVM..
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18546
    
  40

Ramesh Pramuditha Rathnayake wrote:Thanks for the replies..

I got the answer. You both are trying to say that it is a work done by compiler...
As C compiler make machine code file, the result would be vary from compiler to compiler. But in java the Machine code is generated in JVM. Therefore the result may vary in Java from JVM to JVM..



I think this would be a better way to think about it... The Java Language specification, is very specific in this regard. Basically, with post increment, the variable is incremented right after it is used in the expression. This is probably the earliest that it can be incremented and still be called post increment. The JLS is also very specific on how the expression is evaluated.

With C, ANSI C is also very specific in the language definition -- and the result is the same as Java. However, the ANSI C wasn't always that strict (especially, prior to ANSI C). Older specifications simply stated that the increment occurred later. And some compilers interpreted this to mean to increment after the complete expression (including the assignment) has been completed. So, depending on when the "later" increment happened, the results are different depending on which compiler was used.

Henry
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38075
    
  22
Ramesh Pramuditha Rathnayake wrote:. . . But in java the Machine code is generated in JVM.
Not necessarily. The JVM interprets the bytecode, and there may be no machine code generated at all.
Therefore the result may vary in Java from JVM to JVM..
Not at all. As Henry has already said, the JLS is very strict, so such code must behave the same way on all implementations.
Ramesh Pramuditha Rathnayake
Ranch Hand

Joined: Oct 31, 2012
Posts: 169
    
    1

As I know in postfix and prefix, increment is done first. I think that Mr. Henry said that older C compiler "stated that the increment occurred later"..

I thought that machine code is generated by JVM(by interpreter) and send to the OS. Is that wrong..? I think Mr. Campbell said that JVM controls the hardware. Not the OS..

-------------------------------------------
I download a new compiler. I gives answer 100.
But now, there is another problem..



Java gives 303 and C gives 302.
Then I write the following thing...



Answers are as follows..

Java-------------C-----
100 ------------100
201 ------------201
101-------------101
303-------------302

Why is that..?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18546
    
  40

Ramesh Pramuditha Rathnayake wrote:As I know in postfix and prefix, increment is done first. I think that Mr. Henry said that older C compiler "stated that the increment occurred later"..


Clarification please... cause if "increment is done first" without more clarification, then isn't post and pre the same?


Ramesh Pramuditha Rathnayake wrote:
I thought that machine code is generated by JVM(by interpreter) and send to the OS. Is that wrong..? I think Mr. Campbell said that JVM controls the hardware. Not the OS..


The operating system does not have to be involved with an increment. The increment machine code can operate perfectly fine in user space.

Ramesh Pramuditha Rathnayake wrote:
-------------------------------------------
I download a new compiler. I gives answer 100.
But now, there is another problem..


You do know that there is nothing in the Java Language Specification that says that it has to match the C language right? You do know that there is nothing in the ANSI C specification that says that it has to match the Java language right?

Ramesh Pramuditha Rathnayake wrote:


Java gives 303 and C gives 302.
Then I write the following thing...



Answers are as follows..

Java-------------C-----
100 ------------100
201 ------------201
101-------------101
303-------------302

Why is that..?


Because these are two different languages? With two different specifications? Are you asking someone to dive into two tomes of legalese to get you an answer here? I think you may have surpassed what you can expect from a bunch of volunteers.

Henry
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38075
    
  22
Ramesh Pramuditha Rathnayake wrote:As I know in postfix and prefix, increment is done first.
It causes no end of confusion with postincrement that people don’t understand that they are dealing with two values. One is the value of i and the other is the value of i++. When you enter the expression the value of i is 101 but i++ is 100 still.
. . . I think Mr. Campbell said that JVM controls the hardware. Not the OS..
I am quite sure I didn’t say that. The bytecode is interpreted by the JVM and it might do that without creating machine code. You would have to go through the JVM code to find out.
. . .
Java gives 303 and C gives 302.
. . .
And when I tried it with GCC I got 301. If you work it out following the Java Language Specification, you can work out how you get 303. You can write the values of the three subexpressions and they add up to 303. Of course, if you divide it up and use assignments, you have a different program which will behave differently. We have an FAQ about that.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38075
    
  22
And later with GCC again, I got 302. As Henry said, you would have to go through the GCC specifications and see what it says about postincrement and preincrement.
Ramesh Pramuditha Rathnayake
Ranch Hand

Joined: Oct 31, 2012
Posts: 169
    
    1

In both cases increment is done first. Then the assigning.. When assigning, we have to know that i++ has the earlier value and ++i has the increased value..(In java)

I will go through GCC specifications as you tell.
Thank you for your help..!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38075
    
  22
Ramesh Pramuditha Rathnayake wrote: . . . When assigning, we have to know that i++ has the earlier value and ++i has the increased value..(In java)
. . .
Correct

Thank you for your help..!
You're welcome

As I said, postincrement seems to cause no end of confusion.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

Note that what result you get from an expression such as i = i++;, and that in Java you get something different than in C (and different C compilers even produce different results) does not have anything at all to do with the fact that Java runs on the JVM, or which part generates machine code (the compiler or the JVM).

It's simply a matter of programming language specifications.

The Java Language Specification explains exactly how this statement should be executed and what the result should be - how it should officially work in Java.

In the C programming language, there are many things that were left unspecified. The result is that different C compilers might produce code that does different things - the specification for the C programming language simply doesn't say that any particular result is correct. The result that you get is simply because of an arbitrary choice made by the people who wrote your particular C compiler.

Also, Java and C are different languages. There's no reason to expect that the result in one language should be the same as in another language.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java and C
 
Similar Threads
final variables
A Map.Entry object not becoming invalid after map modification
Post increment and the assignment operator
Why there is a compiler error?
data types