C comma in ternary statement
Asked Answered
K

4

4
int m = 5, d = 12, y = 1975, val;
    // May 12, 1975

Can someone please explain the function/purpose of the comma operator in the line of code below:

val = (d+=m<3?y--:y-2,23*m/9+d+4+y/4-y/100+y/400)%7;

The above line was written by Mike Keith to calculate the day of the week given the date (d = day, m = month, y = year). Where Sunday = 0, Monday = 1, Tuesday = 2, Wednesday = 3, Thursday = 4, Friday = 5, Saturday = 6. I understand that the y-- gets executed if d+=m<3 is true, else the y-2 is executed. What I don't understand is the purpose of the comma after y-2.

Knowling answered 14/2, 2015 at 21:16 Comment(3)
This code is unmaintainable gross nonsense. Don't use it as an example of how to write sensible code.Flatcar
Please explain what's actually happening because I've never seen this. I'm pretty new to CKnowling
Someone please tell me that this isn't in production code...Kissie
P
7

The comma operator separates expressions to be executed one after the other, just like ;. But with , they constitute one whole expression that evaluates to the value of the last sub-expression. For example

int i = 1;
int j = (++i, i*2);
printf("%i", j)

prints out 4.

It can for example be used in for expressions, where 3 expressions need to be in the header. For example

for(i = 0, j = 0; i < n; i++, j++)
Pseudoscope answered 14/2, 2015 at 21:23 Comment(1)
May I respectfully suggest that keeping i and j at the same value in the last loop is not as compelling as the similar for (i = 0, j = n; i < j; i++, j--)?Grime
O
8

The statement

val = (d+=m<3?y--:y-2,23*m/9+d+4+y/4-y/100+y/400)%7;  

is equivalent to

val = ( (d+=(m<3?y--:(y-2))), (23*m/9+d+4+y/4-y/100+y/400) ) % 7; 

, is comma operator (evaluates its first operand and discards the result, and then evaluates the second operand and returns this value) here.

Left operand of comma operator, ie., (d+=m<3?y--:y-2) is evaluated and side effect to y take place. Value of this expression is discarded. Right operand (23*m/9+d+4+y/4-y/100+y/400) will be evaluated and its value is the value of expression (d+=m<3?y--:y-2), (23*m/9+d+4+y/4-y/100+y/400).

Ostia answered 14/2, 2015 at 21:22 Comment(2)
actually, the first operand sets 'd' as either 'y' or 'y-2', it may, depending on what month is use (I.E. Jan or Feb will after setting 'd' then decrement 'y') The calculated value of 'dCounteract
this looks like the kind of coding that might be utilized in contests to maximize the functionality of a program while minimizing the number of characters in the program.Counteract
P
7

The comma operator separates expressions to be executed one after the other, just like ;. But with , they constitute one whole expression that evaluates to the value of the last sub-expression. For example

int i = 1;
int j = (++i, i*2);
printf("%i", j)

prints out 4.

It can for example be used in for expressions, where 3 expressions need to be in the header. For example

for(i = 0, j = 0; i < n; i++, j++)
Pseudoscope answered 14/2, 2015 at 21:23 Comment(1)
May I respectfully suggest that keeping i and j at the same value in the last loop is not as compelling as the similar for (i = 0, j = n; i < j; i++, j--)?Grime
R
4

That line is equivalent to

if ( m < 3 )
{
    d = d + y;
    y--;
}
else
{
    d = d + y - 2;
}

val = (23 * m/9) + d + 4 + y/4 - y/100 + y/400;
val = val % 7;

because the ternary operator has higher precedence than the assignment += and the comma operator ,. So the first action is m is compared to 3. If m < 3 then both y and d are tweaked, otherwise only d is tweaked. The updated values of y and d are then used to calculate some large magical value. Finally, the modulo operator reduces that value to a number between 0 and 6, which supposedly is the day of the week.

To quote Oliver Charlesworth, "This code is unmaintainable gross nonsense."

Ragged answered 14/2, 2015 at 22:10 Comment(1)
I can see where the y/4 and y/100 and y/400 are calculating the number of leap year days.Counteract
A
1

The comma operator separates expressions that are evaluated in order and then the result is the value of the last one. In this case, the programmer wants to tweak the value of y and d before entering the main expression. I agree with others who question the clarity of the code.

See the wikipedia article http://en.wikipedia.org/wiki/Comma_operator.

Alicealicea answered 14/2, 2015 at 21:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.