1

Please note that I am doing this strange assignment not because I am unclear of the syntax or am new to c.

I was just trying out what would happen if an int was assigned an array:

int a = {1, 2, 3};

To this I got the following warning:

 warning: excess elements in scalar initializer
  int a = {1, 2, 3};
              ^
warning: excess elements in scalar initializer
  int a = {1, 2, 3};
                 ^

and when I print the contents of a, I get 1.

But when I do this:

int a;
a = {1, 2, 3};

I get an error:

error: expected expression before ‘{’ token
  a = {1, 2, 3};
      ^

Though I know arrays are not meant to be assigned to int variables, the above results lead me to question:

  • the difference between int x; x = ... and int x = ...

and

  • what exactly causes the warning or the error message?
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
Polaris000
  • 938
  • 1
  • 11
  • 24
  • 2
    They're both wrong and it is up to the compiler writer what the contents of the error message is , and any behaviour – M.M Mar 05 '19 at 06:47
  • So the error message or the warning is completely independent of the way in which I do the initialization (directly vs on two separate lines)? – Polaris000 Mar 05 '19 at 06:49
  • 1
    It could be that the compiler assigns its address at initialization or it automatically dereferences to the first element, etc. etc. but not in case of assignment, it actually really depends on how the compiler is designed. Try running your code in different compilers to see the change... – Ruks Mar 05 '19 at 06:53
  • Write `int a[3] = {1, 2, 3};` – Alejandro Blasco Mar 05 '19 at 07:04
  • 1
    @AlejandroBlasco, or even better, `int a[ ] = {1, 2, 3};` – Sourav Ghosh Mar 05 '19 at 07:05
  • As I mentioned in the description, I am aware I am not doing a valid assignment. Was just curious about this behavior I noticed. Thanks @AlejandroBlasco – Polaris000 Mar 05 '19 at 07:06
  • You defined `a` as an integer and initialized it with an array. That's bad and the compiler alerts you that he was expecting an expression. – Alejandro Blasco Mar 05 '19 at 07:14

1 Answers1

5

First of all,

int a = {1, 2, 3};

is a definition and initialization statement, and

int a;
a = {1, 2, 3};

is a definition and assignment statement.

They have different rules. The syntax {1, 2, 3} is called a brace-enclosed initializer list, that can be used in an initialization statement, not in an assignment - it's a syntax error. So, that answers about the error in the second snippet.

For the first snippet, for a scalar, quoting C11, chapter §6.7.9/P11

The initializer for a scalar shall be a single expression, optionally enclosed in braces. [...]

So, the expression {1, 2, 3} attempts to provide more that one element, which is excess. That's what your compiler is warning you about.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • 2
    @Inian As you should know, actual standard document is not free. There are draft versions that are available for free online. and yes, I refer to the same one you're pointing out. I'd refrain from adding links here as I cannot guarantee the availability of those (unofficial) sources. A quick google search should lead you to the draft versions. – Sourav Ghosh Mar 05 '19 at 07:09
  • 1
    Sure, that makes sense! Thanks! – Inian Mar 05 '19 at 07:12
  • 1
    https://stackoverflow.com/questions/81656/where-do-i-find-the-current-c-or-c-standard-documents – M.M Mar 05 '19 at 07:14