5

I tried the following code in gcc:

#include<stdio.h>
int main()
{
    int a=3,2,1;//////////////////////ERROR!//////////////////////////
    printf("%d", a);
    return 0;
}

I expected it to compile successfully as:

  • a series of integer expressions seperated by commas will be evaluated from left to right and the value of the right-most expression becomes the value of the total comma separated expression.

Then, the value of the integer variable a should have been 1 right? Or is it 3?

And why am I getting this error when I try to execute this program?

error: expected identifier or '(' before numeric constant

J...S
  • 5,079
  • 1
  • 20
  • 35
  • 3
    it's neither 1 or 3, it's a syntax error – yano Oct 28 '16 at 16:19
  • 3
    There is no comma-operator. The comma is part of the declaration. – too honest for this site Oct 28 '16 at 16:52
  • @Gabriel: No research effort? – too honest for this site Oct 28 '16 at 16:52
  • @Olaf Maybe. But if it is the point here, [this question](http://stackoverflow.com/questions/31044/is-there-an-exists-function-for-jquery?rq=1) should not have so many up votes (it is just an example). – Gabriel Oct 28 '16 at 17:03
  • @Gabriel: So you mean, once a posting has an upvote, there shall be no downvoters? Feel free to start e feature-request on meta for that ... (but then, please also for the other direction!) – too honest for this site Oct 28 '16 at 17:38
  • 1
    @Olaf This is not even close to what I mean. My point is: A question with low research effort can be a good question (so I used that another question as an example). This question here is interesting and well written, thus does not deserve a down vote. – Gabriel Oct 28 '16 at 17:47
  • @Gabriel: Please read site-rules. No research effort is a valid DV reason. Anyway, if you want to criticise on DVs, please do that on meta, not here in comments. Fee free to post a link to your meta-question. Until then: case closed! – too honest for this site Oct 28 '16 at 18:01
  • @Olaf, first, you intentionally misinterpret what I said, then you dare to shut me out. You are not the kind of person I want to have a conversation with. Bye. – Gabriel Oct 28 '16 at 18:41
  • I "shout you out"? Where? By pointing you at site-rules and telling to use the section which is exactly meant for such problems? But good you know what I understand from your comments. I'm not that good, I'm no clairvoyant. – too honest for this site Oct 28 '16 at 18:51

2 Answers2

12

That's parsed as a three-part variable declaration, with two invalid variables.

You need to wrap the entire initializer in parentheses so that it's parsed as a single expression:

int a=(3,2,1);
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • 1
    When the heck does someone use this? – Omid CompSCI Oct 28 '16 at 16:22
  • @OmidCompSCI: To do evil things in expressions. https://en.wikipedia.org/wiki/Comma_operator – SLaks Oct 28 '16 at 16:24
  • thanks for the link, it is quite funny I have never seen the "comma" operator used like this. Usually the most advanced thing I have seen is in for loops with multi-conditional statements; – Omid CompSCI Oct 28 '16 at 16:27
  • @Omid CompSCI: As for `for` loops, most of the time it would be used in the first or third part of the `for` header. It would be rather strange to see it used in the conditional part. – AnT stands with Russia Oct 28 '16 at 16:47
  • @AnT yes that is what I meant sorry, everything but the termination condition. – Omid CompSCI Oct 28 '16 at 19:10
  • @AnT and thanks for the link, in my opinion I would not opt out for readability just for writability. I think people that use this have not read the book Clean Code. – Omid CompSCI Oct 28 '16 at 19:11
3

If you separate the initialization from the declaration, you may get what you expected:

int a;
a = 3, 2, 1;     // a == 3, see note bellow

or

int a;
a = (3, 2, 1);   // a == 1, the rightmost operand of 3, 2, 1

as your original command is syntactically incorrect (it is the declaration so it expected other variables to declare instead of numbers 2 and 1)

Note: All side effects from the evaluation of the left-operand are completed before beginning the evaluation of the right operand.

So

a = 3, 2, 1

which are 3 comma operators a = 3, 2 and 1 are evaluated from left to right, so the first evaluation is

a = 3, 2

which result 2 (right-operand) (which is by the way not assigned to any variable, as the value of the left-operand a = 3 is simply 3), but before giving this result it is completed the side effect a = 3 of the left-operand, i. e. assigning 3 to variable a. (Thank AnT for his observation.)

MarianD
  • 13,096
  • 12
  • 42
  • 54
  • 1
    @J...S at least for me, `a=3,2,1;` results in `a==3` and `a=(3,2,1);` results in `a==1`. I don't know enough to know if this is implementation-defined behavior or not. For both cases, `gcc` warns "operand of comma expression as no effect". I can't think of a practical reason for ever doing this. – yano Oct 28 '16 at 16:37
  • @MarianD: "... get what you expected"? What exactly do you think the OP expected? The original post suggests that the OP expected their code to behave as `int a = (3, 2, 1);`. But your `a = 3, 2, 1;`, is actually `(a = 3), 2, 1;`, which is completely different. It compiles, but the behavior is stiull different from the OP expectations. – AnT stands with Russia Oct 28 '16 at 16:42
  • @MarianD Oops! Your code int a; a = 3, 2, 1; does compile but when I tried int a = 3, 2, 1; it still showed error? Why? What's the difference? – J...S Oct 28 '16 at 16:54
  • 1
    Compare: `int a = 3, b, c` (correct - declaration of 3 variables, the 1st is initialized) and `int a = 3, 2, 1` (incorrect - `2` and `1` **are not correct names for variables**. – MarianD Oct 28 '16 at 17:24
  • 1
    @J...S: The difference has already been explained to you. `int a = 3, 2, 1;` is grouped by grammar as `int (a = 3), 2, 1;`, which is (informally speaking) equivalent to `int a = 3; int 2; int 1;` These last `int 2; int 1;` bits make no sense, which is the reason for the error. – AnT stands with Russia Oct 28 '16 at 17:34