• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Java and C

 
Ranch Hand
Posts: 178
2
Netbeans IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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..?
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 178
2
Netbeans IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 178
2
Netbeans IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 79180
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 178
2
Netbeans IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic