Thread: How this code works?

  1. #1
    Registered User
    Join Date
    Aug 2016
    Posts
    1

    How this code works?

    int i=0, j=1;
    j %= (i++ || i++==j);
    printf("El valor de i es %d\n", i);
    printf("El valor de j es %d\n", j);

    what's the meaning of j%?

    and what happend with this (i++ || i++==j)

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,531
    %= is known as an assignment operator, where

    j += 1

    is shorthand for
    j = j + 1

    > and what happend with this (i++ || i++==j)
    It's a boolean expression which will have the value 0 (if false), or 1 (if true).

    Knowledge of short-circuit evaluation rules will be important.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Mar 2016
    Posts
    13
    Here is the result I got:
    Code:
    El valor de i es 2
    El valor de j es 0
    Which makes sense if you consider:
    Order of evaluation - cppreference.com
    6) Every value computation and side effect of the first (left) argument of the built-in logical AND operator && and the built-in logical OR operator || is sequenced before every value computation and side effect of the second (right) argument.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    With the two instances of i++ in the same line, is this not also an example of undefined behavior?
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  5. #5
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    Quote Originally Posted by Elkvis View Post
    With the two instances of i++ in the same line, is this not also an example of undefined behavior?
    Evaluation is specified for ++i and i++ and before or after evaluation. Evaluation in this case means specifically the value of the variable, not the outcome of the function, so it shodul always have the effect of e.g.

    x = 5 then


    5 || 6

    with x being 7 after the fact.

    I wouldn't write such a piece of code though, as this may be an area where some compilers are non-compliant (ahem MSVC).

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,237
    Quote Originally Posted by Elkvis
    With the two instances of i++ in the same line, is this not also an example of undefined behavior?
    No, because || introduces a sequence point.

    Quote Originally Posted by abachler
    Evaluation is specified for ++i and i++ and before or after evaluation. Evaluation in this case means specifically the value of the variable, not the outcome of the function, so it shodul always have the effect of e.g.

    x = 5 then


    5 || 6

    with x being 7 after the fact.
    Not quite: the issue is that between consecutive sequence points, if i is modified more than once, then the behaviour is undefined. Thankfully, || introduces a sequence point, so there is no issue. If it were | instead, then the behaviour would be undefined.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    abachler! Haven't seen you on here in over 4 years! Where have you been? What have you been up to?
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. why this code Works.
    By jafar in forum C Programming
    Replies: 4
    Last Post: 05-01-2014, 03:53 PM
  2. How Obfusicated code works
    By acho.arnold in forum C Programming
    Replies: 3
    Last Post: 08-17-2013, 03:27 PM
  3. How come my code works? Seriously. :)
    By assiduus in forum C Programming
    Replies: 15
    Last Post: 02-10-2011, 12:54 PM
  4. Not sure how this simple code works
    By metros in forum C Programming
    Replies: 8
    Last Post: 02-09-2010, 06:22 PM
  5. How Do u See if a code works
    By Nos in forum C++ Programming
    Replies: 11
    Last Post: 08-05-2005, 01:34 PM

Tags for this Thread