1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Can you find the output of this coding?

Discussion in 'C, C++, C#' started by seoways, Jul 19, 2013.

  1. seoways

    seoways Jr. VIP Jr. VIP

    Joined:
    Dec 19, 2009
    Messages:
    5,310
    Likes Received:
    803
    Gender:
    Male
    Location:
    MILKYWAY ;)
    This code shows an important difference b/w C and JAVA:

    Just find out the output of below code in C and JAVA?

    Code:
    int i=5;
    int j=i+++++i;
     
    Last edited: Jul 19, 2013
  2. innozemec

    innozemec Jr. VIP Jr. VIP

    Joined:
    Aug 19, 2011
    Messages:
    5,342
    Likes Received:
    1,802
    Location:
    www.Indexification.com
    Home Page:
  3. Jannes

    Jannes Newbie

    Joined:
    Jan 9, 2010
    Messages:
    35
    Likes Received:
    15

    I would say j would be 5 + 7 = 12
     
  4. CSharp

    CSharp Newbie

    Joined:
    Jun 7, 2013
    Messages:
    39
    Likes Received:
    7
    Occupation:
    Developer, Entrepeneur
    Location:
    UK
    Disregard, i thought it was c# lol

    Post before I re-read the question.
     
    Last edited: Jul 19, 2013
  5. divok

    divok Senior Member

    Joined:
    Jul 21, 2010
    Messages:
    1,067
    Likes Received:
    648
    Location:
    .IN
    90% sure it would be a error in c .
    because a variable(i) is being accessed more than once in a single assignment operation
     
  6. seoways

    seoways Jr. VIP Jr. VIP

    Joined:
    Dec 19, 2009
    Messages:
    5,310
    Likes Received:
    803
    Gender:
    Male
    Location:
    MILKYWAY ;)
    It is definitely not an error code....
    We can access the value of a variable(i) at any number of time in a single line of coding.
    Don't you know this simple basics!!!:thumpdown
     
  7. divok

    divok Senior Member

    Joined:
    Jul 21, 2010
    Messages:
    1,067
    Likes Received:
    648
    Location:
    .IN
    my bad ..it has been a year since i last coded in c .

    the behavior is undefined , and by access more than once i meant a value can be changed (accessed) only once b/w sequence points , acc to c standards .


    it will still show error , lvalue .
    there should be spaces between increment op and plus sign .
     
    Last edited: Jul 20, 2013
  8. s0ap

    s0ap Executive VIP Jr. VIP Premium Member

    Joined:
    Sep 23, 2008
    Messages:
    230
    Likes Received:
    821
    Occupation:
    :] guess
    Location:
    Congo/DRC
    The output wouldn't be anything since all you have done is assign values to variables. In C, j would be 12, however clang and gcc both refuse to compile it as written. You need to add a space between 'i++ + ++i'.
     
  9. Jannes

    Jannes Newbie

    Joined:
    Jan 9, 2010
    Messages:
    35
    Likes Received:
    15
    Well I think with these kind of questions, you don't have to bother if it is compiler proof code. It's all about understanding what the code does, in this case understanding prefix and postfix operators.

    So why all the fuzz about compiler errors?
     
  10. killersoft

    killersoft Newbie

    Joined:
    Jul 8, 2013
    Messages:
    12
    Likes Received:
    5
    Occupation:
    software developer
    Location:
    Poland
    As Java Language Specification says, all operators are resolved in term of largest length, so in Java "+++++" is not "++ + ++", so, this code would not compile.
     
  11. cyberbrute

    cyberbrute Registered Member

    Joined:
    Jul 29, 2013
    Messages:
    72
    Likes Received:
    50
    Occupation:
    Student
    Location:
    Pakistan
    as in C++ scope i think this code will return with compile time error, because you are using variable two times in single assignment.
    but i am not sure about it.

    Regards,
    Waseem
     
  12. morgan456

    morgan456 Jr. VIP Jr. VIP

    Joined:
    Nov 26, 2010
    Messages:
    2,489
    Likes Received:
    266
    Gender:
    Male
    Occupation:
    SEO company
    Value of j would be 8
     
  13. botrockets

    botrockets Regular Member

    Joined:
    Mar 16, 2013
    Messages:
    355
    Likes Received:
    550
    Gender:
    Male
    Occupation:
    Entrepreneur
    Location:
    BotRockets
    j = 12 is the answer as said by sOap
     
  14. seoways

    seoways Jr. VIP Jr. VIP

    Joined:
    Dec 19, 2009
    Messages:
    5,310
    Likes Received:
    803
    Gender:
    Male
    Location:
    MILKYWAY ;)
    I think j=12 is the correct answer.

    First step, i++ value will be assigned(5) and incremented by one (i=5,j=5)
    next step ++i value will be increased(++6) and added so, (i=5+7)
     
  15. DarkPixel

    DarkPixel Jr. VIP Jr. VIP

    Joined:
    Oct 4, 2011
    Messages:
    1,348
    Likes Received:
    1,252
    Location:
    ↓↓↓↓
    Home Page:
    JAVA
    1) With
    int j=i+++++i; DIDN'T COMPILE
    12hcEBZ.png
    2) With int j=i++ + ++i; COMPILED - Result = 12
    Tw6EOwK.png
    C++
    1) With
    int j=i+++++i; DIDN'T COMPILE
    vHDHk2S.png
    2) With int j=i++ + ++i; COMPILED - Result = 12
    4JmkSO8.png
     
  16. Gogol

    Gogol Jr. VIP Jr. VIP

    Joined:
    Sep 10, 2010
    Messages:
    3,476
    Likes Received:
    3,103
    Gender:
    Male
    Yupp as s0ap pointed out, you need to add two spaces so that it reads i++ + ++i;

    EDIT: PHP would output 12 as well...

    Code:
    <?php
    
    $i=5;
    $j = $i++ + ++$i;
    
    echo $j ;
    
    //outputs 12
    
     
    Last edited: Jul 30, 2013