Results 1 to 6 of 6
  1. #1
    Join Date
    Aug 2010
    Location
    India
    Posts
    10
    Rep Power
    0

    Default ++/--increments for (C/JAVA) explained!!

    C
    Java Code:
    #include<stdio.h>
    #include<conio.h>
    main(){
      clrscr();
      int i=5; 
      printf("%d%d%d%d%d%d",i++,i--,++i,--i,i);
      getch();
    }
    Answer:
    4 5 5 4 5

    Explanation:
    The arguments in a function call are pushed into the stack from left to right. The evaluation is done by popping out from the stack i.e., from right to left.





    JAVA
    Java Code:
    public class Main {
    
        public static void main(String[] args) {
            int i = 4;
            System.out.println(i++ + " " + i-- + " " + ++i + " " + --i + " " + i);
        }
    }
    Answer:
    4 5 5 4 4

    Explanation:
    The arguements in a function call are pushed into the stack from right to left. The evaluation is donw by popping out from the stack i.e., from left to right.



    I am not sure about the explanations given above.. Can Java Experts confirm if these explanations are right or wrong??
    “Give somebody a fish and they eat for a day.Teach somebody to fish they eat for life!”

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,017
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by _ShivamKapoOr_ View Post
    C
    Java Code:
    #include<stdio.h>
    #include<conio.h>
    main(){
      clrscr();
      int i=5; 
      printf("%d%d%d%d%d%d",i++,i--,++i,--i,i);
      getch();
    }
    Answer:
    4 5 5 4 5
    No, the answer is: undefined behaviour according to the C, C++ ISO/ANSI specs. Nothing is said about the order of pushing stuff on a stack either because there might not even be a stack; it's an implementation detail.

    kind regards,

    Jos

  3. #3
    Join Date
    Aug 2010
    Location
    India
    Posts
    10
    Rep Power
    0

    Default

    1. Do you mean that the output will be different on different machines???????? from where can I read the specifications?

    2. You didn't said any thing about the explanation provided for JAVA!
    “Give somebody a fish and they eat for a day.Teach somebody to fish they eat for life!”

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,017
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by _ShivamKapoOr_ View Post
    1. Do you mean that the output will be different on different machines???????? from where can I read the specifications?
    There might not be any output at all; nothing is said about undefined behaviour; the ANSIC/ISO-C specifications tell it all (you have to buy them, they don't come for free.

    2. You didn't said any thing about the explanation provided for JAVA!
    Java evaluates everything strictly from left to right and doesn't even know about undefined behaviour.

    kind regards,

    Jos

  5. #5
    Join Date
    Aug 2010
    Location
    India
    Posts
    10
    Rep Power
    0

    Default

    no. i mean what about stacks..?

    If suppose no stacks are used at the implementation level then how exaclty is the expression evaluated? and if jvm is capable enough to evaluate it then y not c interpretor?
    “Give somebody a fish and they eat for a day.Teach somebody to fish they eat for life!”

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,017
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by _ShivamKapoOr_ View Post
    no. i mean what about stacks..?

    If suppose no stacks are used at the implementation level then how exaclty is the expression evaluated? and if jvm is capable enough to evaluate it then y not c interpretor?
    I don't know what you had in mind about stacks w.r.t. C or C++. General purpose register machines are very well capable of evaluating expressions without a stack. The definitions of C (or C++) and Java are different; the notion of undefined behaviour is there to translate C or C++ on different platform with different programming models. Java has a more stringent definition.

    kind regards,

    Jos

Similar Threads

  1. Replies: 2
    Last Post: 03-16-2010, 08:24 PM
  2. Pre- and post-increments
    By SirFalcon in forum New To Java
    Replies: 6
    Last Post: 10-29-2009, 03:29 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •