{} in assignment

Discussion in 'C Programming' started by slashman, Mar 26, 2008.

  1. slashman

    slashman Guest

    I recently came across code that looked like this

    float i = {1.0};

    Can anyone tell me the significances of the curly braces{}.
    Thanks in advance
    Vivek
     
    slashman, Mar 26, 2008
    #1
    1. Advertising

  2. slashman

    santosh Guest

    slashman wrote:

    > I recently came across code that looked like this
    >
    > float i = {1.0};
    >
    > Can anyone tell me the significances of the curly braces{}.
    > Thanks in advance
    > Vivek


    It's redundant for this particular initialisation, but if i were to be
    an array of float then you'd need the curly braces to initialise one or
    more of it's elements. Ex:

    float arr[5] = { 1.0, 2.0, 3.0 };

    Elements arr[0], arr[1] and arr[2] are initialised with 1.0, 2.0 and 3.0
    respectively while the remaining elements are set to zero by the
    compiler. This is true only for statically allocated arrays.
     
    santosh, Mar 26, 2008
    #2
    1. Advertising

  3. slashman wrote:
    > I recently came across code that looked like this
    >
    > float i = {1.0};
    >
    > Can anyone tell me the significances of the curly braces{}.


    There's no "assignment" in your code. This is _initialization_, not
    assignment. The initializer is enclosed in '{}'. Initializers enclosed
    in '{}' are normally used with aggregates. However, it is perfectly
    legal to use '{}'-enclosed initializers with scalar objects, in which
    case the '{}' are simply redundant. They don't mean anything, meaning
    that your code is the exact equivalent of

    float i = 1.0;

    The ability to use '{}' in scalar initializers might be useful in
    "generic" code (in macros, for example), where you can use the common '=
    { 0 }' initialization idiom with a wider range of types.

    --
    Best regards,
    Andrey Tarasevich
     
    Andrey Tarasevich, Mar 26, 2008
    #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. itsme
    Replies:
    1
    Views:
    1,635
    Ralf Hildebrandt
    Jul 23, 2003
  2. Anand P Paralkar
    Replies:
    2
    Views:
    9,398
    Srinivasan Venkataramanan
    Aug 4, 2003
  3. John Potter

    Question: inout signal assignment

    John Potter, Sep 6, 2003, in forum: VHDL
    Replies:
    1
    Views:
    1,728
    Renaud Pacalet
    Sep 8, 2003
  4. nagy
    Replies:
    36
    Views:
    1,011
    Terry Reedy
    Jul 20, 2006
  5. Chris
    Replies:
    34
    Views:
    1,526
Loading...

Share This Page