Home > database >  Why doesn't a comma work as a punctuation in this program?
Why doesn't a comma work as a punctuation in this program?

Time:12-13

int inp, temp;

inp = 2347653;

printf("%d, %d", (temp = inp / 10000, inp %= 10000, temp), (temp = inp / 1000, inp %= 1000, temp));

this program showed me an unexpected result. ( 0, 0 )

So I tried separate the program like this

printf("%d, ", (temp = inp / 10000, inp %= 10000, temp));

printf("%d", (temp = inp / 1000, inp %= 1000, temp))

it works properly. Therefore, I thought there is an operator issue. I think the comma between two expressions didn't work as a punctuation but an operator. What should I do to make the first program work? and Why the compiler comprehend the comma as an operator?

CodePudding user response:

You are mixing 2 different uses of the comma lexical element.

  1. It can be used as an operator between 2 expressions. In that case both expressions are evaluated in order, and the result of the operator is the result of the second expression. Or
  2. It can be used to separate the parameters in a function call. In that case all the parameters are evaluated in a non specified order, and the function call is evaluated after all its parameter have been evaluated.

In your first version of code, you call the print function with 3 parameters (format string and 2 arguments), and the compiler is free to evaluate the parameters in any order. In the second version, you have 2 distinct calls and the compiler has to fully evaluate first call before starting the evaluation of the second (or at least the result must be what they should be if the rule had been obeyed...)

CodePudding user response:

Why the compiler comprehend the comma as an operator?

Because it can be an operator. What does the comma operator , do? In your case you have the argument list to printf separated by commas - those are not operators as such but part of the function call syntax. But the commas inside each parenthesis is the comma operator.

this program showed me an unexpected result.

There is no expected result - you are invoking both undefined and unspecified behavior.

Each sub-expression (temp = inp / ..., inp %= ..., temp) is sequenced from left to right, as guaranteed by the comma operator. But the two sub-expressions aren't sequenced in relation to each other: the order of evaluation of function arguments is unspecified.

Furthermore, there is no sequence point between the temp = inp... expression of each subexpression and the right-most value computation of temp in the other expression, meaning that the code invokes undefined behavior. And that's why your second example works, it has the poorly-defined behavior removed.

Do not mix over ten(!) operators and four(!) side-effects in the same expression. That's plain obfuscation. Instead rewrite it as readable, well-defined C:

int inp, result1, result2;

inp = 2347653;

result1 = inp / 10000;
inp %= 10000;
result2 = inp / 1000;
inp %= 1000;

printf("%d, %d", result1, result2);
  •  Tags:  
  • c
  • Related