Win a copy of Bad Programming Practices 101 (e-book) this week in the Beginning Java forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Different between java and c  RSS feed

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hai..i dont know how it is silly question...but i have a doubt in this concept. when i run the following code.

int a=4;
a=a++;

if i print "a" value in next line
In C it gives an output 5..but
In Java it gives only 4..
can you Please tell me the reason..why it is..?
 
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I first read your question, I said "no way". But you're right. I haven't programmed in C for a while, but I scrounged around for my C compiler and tried it out and got the same results as you did.

In Java, an assignment such as "a=a++", returns a warning that says "The assignment to variable a has no effect". After a few tests, I found that:

- in Java "a = a++" has no effect, whilst "a++" does increment,
- in C, "a = a++" is equivalent to "a++" (it increments).

The only conclusion that I can come to is that the Java and C compilers work differently in this respect.

Regards,
JD
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
search here, sun, google for
i = i++

this question is asked about every other day
 
Marshal
Posts: 59412
187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Java and C compilers work differently.

True. But I have several C compilers on my PC, three of which I can get to work. I tried two, one giving me the result of 4, and the other 5.

CR
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!

In Java there is a difference between post- and pre-increment.

//post, your case
int i=4;
i=i++;
Here the variable a is first assigned to itself (i=i) and after this a copy of i is incremented.
After this line, the copy of i vanishes and i is still four.

//pre
int i=4;
i=++i;
Here the copy of i is incremented in the first place and then this value is assigned to the original i.
Makes five. The copy of i vanishes as in the post case.

And as John said, some development environments will produce a warning when you code something like i=i++; but the original compiler will not.

AP
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Natesan Prabhakaran:
int a=4;
a=a++;

In C it gives an output 5..but



I vividly recall battling for days on HP-UX with a C application that was segmentation faulting (but not on any other platform - AIX, Solaris, Win32, Linux).

It turns out that particular compiler - which lacked an ANSI mode - did not increment (a pointer in this case) in the example above. Granted, it's silly code that shouldn't have been written in the first place, but it took a long time to track down.
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since this is the beginner forum, maybe that is the lesson to take home:

If the code is hard to understand and possibly shouldn't be written that way then take the time to fix it. Lessons on respective compilers and the secrets that occur under the covers are nice, but write code that works first
 
author and iconoclast
Sheriff
Posts: 24219
40
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David's comment is spot-on, but still...

One thing that I'm surprised no one has pointed out is that in Java, this code has a precisely defined meaning, and the value of a will always be 4. The exact mechanism by which the assignment statement is broken down, analyzed, and executed is exactly specified.

In C, though, the meaning of this code -- like the meaning of a surprising amount of C code -- is undefined. 4 and 5 are both perfectly legal and possible. Different compilers will give different answers.

This difference between Java and C/C++ is an important one: the Java folks deliberately wanted to do away with all of C's "undefined behavior" and the bugs and security issues it can cause.
 
Ranch Hand
Posts: 1847
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by John Dell'Oso:
- in Java "a = a++" has no effect, whilst "a++" does increment,
- in C, "a = a++" is equivalent to "a++" (it increments).



Wrong, in C the result is undefined, for Java it is.
Your compiler might treat it like you state, another might do something different.

Originally posted by John Dell'Oso:

The only conclusion that I can come to is that the Java and C compilers work differently in this respect.

Regards,
JD



Yup, or rather may work differently depending on the specific C compiler you are talking about.
 
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And for a good explanation of what actually happens in Java, see http://faq.javaranch.com/view?PostIncrementOperatorAndAssignment
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
FWIW, the result in ANSI C99 is defined to be the same as Java.
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
int i=0;
i=i++;
i=i++;
i=i++;
System.out.println("i "+ i);
Still i=0,Can anybody explain why?i dint get.
 
Java Cowboy
Sheriff
Posts: 16084
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Ela Jha:
int i=0;
i=i++;
i=i++;
i=i++;
System.out.println("i "+ i);
Still i=0,Can anybody explain why?i dint get.


Read this, Ilja already posted this link above:
http://faq.javaranch.com/view?PostIncrementOperatorAndAssignment
 
Ranch Hand
Posts: 1249
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Ela Jha:
int i=0;
i=i++;
i=i++;
i=i++;
System.out.println("i "+ i);
Still i=0,Can anybody explain why?i dint get.



Dear Ela,

Or alternatively you can take a look at this.

Post Increment Operator
 
Rancher
Posts: 274
C++ Debian VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Tony Morris:
FWIW, the result in ANSI C99 is defined to be the same as Java.



Could you cite the relevant chapter, section and paragraph in the ANSI/ISO C standard that validates your statement?

TIA,
- Anand
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Ela Jha:
int i=0;
i=i++;
i=i++;
i=i++;
System.out.println("i "+ i);
Still i=0,Can anybody explain why?i dint get.



Well, if

i=i++;

doesn't change the value of i, just repeating it an arbitrary number of times shouldn't change it either, should it?
 
Ela Jha
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks evryone,i got it.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!