Why this LOC is valid <int something = {42};>

Discussion in 'C++' started by MJ_India, Nov 2, 2011.

  1. MJ_India

    MJ_India Guest

    C++ file with following code compiles without any complaint on GCC and
    Microsoft Visual C++.

    int main() {
    int something = {42}; // 1. LOC that makes me sick
    // something = something; // To silent unused variable warning
    }

    {...} seems perfect for initialization for a structure or an array.
    My question is; why is it valid to initialize an int with {42}?
    Sometimes I think it is okay, but some other time this is confusing.
    Please help.

    Microsoft Visual C++ also compiles the following lines without any
    complaints:
    2. int weird[2] = {{{12}}};
    3. int weirder = { { 3,} ,};
    MJ_India, Nov 2, 2011
    #1
    1. Advertising

  2. On 11/2/2011 9:30 AM, MJ_India wrote:
    > C++ file with following code compiles without any complaint on GCC and
    > Microsoft Visual C++.
    >
    > int main() {
    > int something = {42}; // 1. LOC that makes me sick
    > // something = something; // To silent unused variable warning
    > }
    >
    > {...} seems perfect for initialization for a structure or an array.
    > My question is; why is it valid to initialize an int with {42}?
    > Sometimes I think it is okay, but some other time this is confusing.
    > Please help.
    >
    > Microsoft Visual C++ also compiles the following lines without any
    > complaints:
    > 2. int weird[2] = {{{12}}};
    > 3. int weirder = { { 3,} ,};


    I think the curly braces are optional when writing the initialization of
    a scalar variable. Commas are optional in some cases, but I am not sure
    they are allowed in initializers; can simply be VC++ extension.

    V
    --
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Nov 2, 2011
    #2
    1. Advertising

  3. MJ_India

    Jorgen Grahn Guest

    On Wed, 2011-11-02, MJ_India wrote:
    > C++ file with following code compiles without any complaint on GCC and
    > Microsoft Visual C++.
    >
    > int main() {
    > int something = {42}; // 1. LOC that makes me sick
    > // something = something; // To silent unused variable warning
    > }


    Confirmed, with g++ and full warnings enabled. It must be standard C
    and C++. That was news to me too, after ~20 years of C and C++
    programming.

    And yes, it makes me a bit sick too. On the other hand I have never
    seen it cause any confusion in practice.

    /Jorgen

    --
    // Jorgen Grahn <grahn@ Oo o. . .
    \X/ snipabacken.se> O o .
    Jorgen Grahn, Nov 3, 2011
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Schnoffos
    Replies:
    2
    Views:
    1,189
    Martien Verbruggen
    Jun 27, 2003
  2. Hal Styli
    Replies:
    14
    Views:
    1,604
    Old Wolf
    Jan 20, 2004
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,671
    Smokey Grindel
    Dec 2, 2006
  4. aling
    Replies:
    8
    Views:
    927
    Jim Langston
    Oct 20, 2005
  5. pozz
    Replies:
    12
    Views:
    700
    Tim Rentsch
    Mar 20, 2011
Loading...

Share This Page