Initialisation

Discussion in 'C Programming' started by grid, Dec 2, 2005.

  1. grid

    grid Guest

    Hi,
    I have the following code that seems to fail :

    #include<stdio.h>
    char c = "hello"[0];
    int main()
    {
    printf("c == [%c]\n",c);
    return 0;
    }

    And the below snippet passes :
    #include<stdio.h>
    int main()
    {
    char c = "hello"[0];
    printf("c == [%c]\n",c);
    return 0;
    }

    From the standard :
    All objects in static storage shall be initialized (set to their initial
    values) before program startup. The manner and timing of such
    initialization are otherwise unspecified.

    But I can see in one compiler that it dosent even warn and compiles
    cleanly though gcc cribs about it that "initializer element is not
    constant".

    Which behaviour seems to be correct ??

    TIA
    ~
     
    grid, Dec 2, 2005
    #1
    1. Advertising

  2. grid

    pete Guest

    grid wrote:
    >
    > Hi,
    > I have the following code that seems to fail :
    >
    > #include<stdio.h>
    > char c = "hello"[0];
    > int main()
    > {
    > printf("c == [%c]\n",c);
    > return 0;
    > }
    >
    > And the below snippet passes :
    > #include<stdio.h>
    > int main()
    > {
    > char c = "hello"[0];
    > printf("c == [%c]\n",c);
    > return 0;
    > }
    >
    > From the standard :
    > All objects in static storage shall be initialized
    > (set to their initial values)
    > before program startup. The manner and timing of such
    > initialization are otherwise unspecified.
    >
    > But I can see in one compiler that it dosent even warn and compiles
    > cleanly though gcc cribs about it that "initializer element is not
    > constant".
    >
    > Which behaviour seems to be correct ??


    The externally defined object initialization
    requires a constant expression and doesn't have one.
    Violation of a "shall" constraint, gives undefined behavior.

    N869
    6.7.8 Initialization

    [#4] All the expressions in an initializer for an object
    that has static storage duration shall be constant
    expressions or string literals.



    6.6 Constant expressions

    [#7] More latitude is permitted for constant expressions in
    initializers. Such a constant expression shall be, or
    evaluate to, one of the following:
    -- an arithmetic constant expression,
    -- a null pointer constant,
    -- an address constant, or
    -- an address constant for an object type plus or minus an
    integer constant expression.

    [#8] An arithmetic constant expression shall have arithmetic
    type and shall only have operands that are integer
    constants, floating constants, enumeration constants,
    character constants, and sizeof expressions.


    --
    pete
     
    pete, Dec 2, 2005
    #2
    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. Adie
    Replies:
    5
    Views:
    324
  2. Wendy S
    Replies:
    0
    Views:
    951
    Wendy S
    Jun 28, 2003
  3. Xavier Tarrago
    Replies:
    0
    Views:
    1,437
    Xavier Tarrago
    Jun 30, 2003
  4. stixwix

    Runtime initialisation

    stixwix, Feb 16, 2006, in forum: Java
    Replies:
    3
    Views:
    346
    Mike Schilling
    Feb 17, 2006
  5. Tim Clacy
    Replies:
    8
    Views:
    395
    Tim Clacy
    May 30, 2006
Loading...

Share This Page