4

Both of these statements work the same:

int x = 5;
int x = {5};

That's also true when I try:

char str[] = "hello";
char str[] = {"hello"};

How does the language define initializing a variable using curly braces?

Lundin
  • 195,001
  • 40
  • 254
  • 396
user3787309
  • 109
  • 1
  • 1
  • 4
  • 1
    This might help you: https://stackoverflow.com/a/18222927/15862198 – Utkarsh Sahu Mar 16 '22 at 09:44
  • 3
    @UtkarshSahu - This is about C, not C++. – T.J. Crowder Mar 16 '22 at 09:46
  • @T.J.Crowder C and C++ have quite a bit similar behaviour in terms of variables except the cases involving objects and classes... – Utkarsh Sahu Mar 16 '22 at 09:49
  • @UtkarshSahu - Sure, lots and lots of differences. :-) But separately, I don't see the above in that answer (unless you're counting `auto z2 = {99};`). Could be my rusty C brain (and it's very rusty indeed). – T.J. Crowder Mar 16 '22 at 09:49
  • @Jean-Baptiste Yunès It's not a great duplicate, the answers are pretty bad and `str[]` is not a scalar but an aggregate. This question is more straight-forward so we can potentially get better answers here too (then maybe close the one you linked as dupe to this). Voting to re-open. – Lundin Mar 16 '22 at 09:54
  • What did your research in the standard reveal? – the busybee Mar 16 '22 at 10:42
  • @thebusybee To be fair, I wouldn't expect the average programmer to read the actual C standard, it is admittedly very cumbersome to read if you aren't used to it (or used at reading technical ISO standards in English for that matter). Asking them language lawyers at SO for formal definitions is an excellent use of this site IMO, so that normal people don't need to waste time chewing through the ISO document. C nerds such as myself would just immediately look up 6.7.9 and copy/paste, no big effort. – Lundin Mar 16 '22 at 12:37
  • @Lundin :-D The average programmer does not have such questions, so you're right. But here we have someone thinking about syntax details, and so I expect at least a simple web research. – the busybee Mar 16 '22 at 13:13
  • @thebusybee It's expected of the average programmer to omit a "wtf" upon spotting `int x = {5};` however. The next sensible question from the average programmer would then be: is this actual standard C or yet another tiresome GNU extension? – Lundin Mar 16 '22 at 14:57

2 Answers2

9

There are three different cases of initialization which apply here:

  • For plain variables/pointers, formally called scalars, then C17 6.7.9 §11 says that you can optionally add braces if you like:

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

  • Then §14 mentions character arrays specifically - they may be initialized with a string literal. And again they may optionally have braces:

    An array of character type may be initialized by a character string literal or UTF–8 string literal, optionally enclosed in braces.

  • Finally, §16 deals with all other initializers of aggregates (arrays or struct) and unions, braces are no longer optional but mandatory:

    Otherwise, the initializer for an object that has aggregate or union type shall be a brace-enclosed list of initializers for the elements or named members.

This makes it possible to write all initializer lists with consistent syntax and using {} no matter which type that the initializer list is initializing. Which in turn can perhaps be handy when writing type-generic macros and similar.

Lundin
  • 195,001
  • 40
  • 254
  • 396
4

There is no difference. In the C11 standard, §6.7.9p11 describes this case:

The initializer for a scalar shall be a single expression, optionally enclosed in braces. The initial value of the object is that of the expression (after conversion); the same type constraints and conversions as for simple assignment apply, taking the type of the scalar to be the unqualified version of its declared type.

Both the integer and the pointer are scalar types (see §6.2.5p21).

(It is the same in C89, the document is just not as nice to link to).

Simon Doppler
  • 1,918
  • 8
  • 26
  • 2
    `char str[]` is however not a scalar but an aggregate. But in fact there is a special initializer rule for character arrays. So this only answers the first part of the question, not the second. – Lundin Mar 16 '22 at 10:03
  • @Lundin you are correct, I somehow mixed up `char *p = {"abc"}` with `char p[] = {"abc"}`. – Simon Doppler Mar 16 '22 at 10:14