Static storage duration question.

This is a discussion on Static storage duration question. within the C Programming forums, part of the General Programming Boards category; Hi, I'm reading C Primer Plus by Prata, and he mentioned that if you initilize a variable with static storage ...

  1. #1
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    452

    Static storage duration question.

    Hi,
    I'm reading C Primer Plus by Prata, and he mentioned that if you initilize a variable with static storage duration, then you have to use constant values. I don't understand what he means by 'constant values'. Did he mean that I have to declare my variable with the qualifier "const"?

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,668
    Quote Originally Posted by nimitzhunter View Post
    Hi,
    I'm reading C Primer Plus by Prata, and he mentioned that if you initilize a variable with static storage duration, then you have to use constant values. I don't understand what he means by 'constant values'. Did he mean that I have to declare my variable with the qualifier "const"?
    Actually, there is no such requirement.



    ITSA
    Socket Library!

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,453
    Quote Originally Posted by nimitzhunter
    I don't understand what he means by 'constant values'. Did he mean that I have to declare my variable with the qualifier "const"?
    No, it means that you should say, use a literal, or an expression that can be resolved as if it were a literal, to initialise an object with static storage duration. It does not mean that all variables with static storage duration must be declared const.

    Quote Originally Posted by Sebastiani
    Actually, there is no such requirement.
    Actually, there is:
    Quote Originally Posted by C99 Section 6.7.8 Paragraph 4
    All the expressions in an initializer for an object that has static storage duration shall be constant expressions or string literals.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,668
    Quote Originally Posted by laserlight View Post
    Actually, there is:
    Heheh. I think this is the second time you've pointed that out to me, too.

    Why then does GCC not complain with this code?

    Code:
    int bar( void )
    {    }
    
    int main( void )
    {
        static int foo = bar( );
    }



    ITSA
    Socket Library!

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,453
    Quote Originally Posted by Sebastiani
    Why then does GCC not complain with this code?
    But it does...
    Code:
    test.c: In function `main':
    test.c:6: error: initializer element is not constant
    at least for the MinGW port of gcc 3.4.5 with the -pedantic option.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,668
    Quote Originally Posted by laserlight View Post
    But it does...
    Code:
    test.c: In function `main':
    test.c:6: error: initializer element is not constant
    at least for the MinGW port of gcc 3.4.5 with the -pedantic option.
    I see. Yeah, I haven't got the "pedantic" switch enabled, apparently (fixed). I wonder why it's required, anyway? Related to initialization order not being guaranteed WRT different translation units or some such?



    ITSA
    Socket Library!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  2. Using private class members in static functions
    By sethjackson in forum C++ Programming
    Replies: 2
    Last Post: 09-23-2005, 09:54 AM
  3. Automatic vs. Static Duration
    By JMB in forum C++ Programming
    Replies: 7
    Last Post: 10-14-2002, 07:16 PM
  4. Simple storage question
    By zipfur in forum C Programming
    Replies: 2
    Last Post: 10-05-2002, 12:27 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21