static storage class and variable initialization

Discussion in 'C Programming' started by arnuld, Aug 8, 2009.

  1. arnuld

    arnuld Guest

    Here is the program and i have some observations about the use of Static
    storage class specifier. Please tell me whether I am right or wrong:


    #include <stdio.h>


    #define DBLVAL 1.001

    int main(void)
    {
    int i = 0;
    char c = 'c';

    static char ch;
    static int x = i;
    static double d = DBLVAL;

    ch = c;

    printf("static int x = %d\n", x);
    printf("static char ch = %c\n", ch);
    printf("static doubler d = %f\n", d);
    return 0;
    }

    ==================== OUTPUT =========================
    [arnuld@dune programs]$ gcc -ansi -pedantic -Wall -Wextra static.c
    static.c: In function ‘main’:
    static.c:14: error: initializer element is not constant
    [arnuld@dune programs]$




    OBSERVATION 1: If you going to initialize a variable declared with static
    specifier then you can only pass compile-time constant values, else you
    can't compile the program.

    OBSERVATION 2: If you want to pass a variable (which is not a compile-
    time constant) to a variable declared/defined with static specifier then
    you can do that only in assignment.




    --
    www.lispmachine.wordpress.com
    my email is @ the above blog.
    arnuld, Aug 8, 2009
    #1
    1. Advertising

  2. On 08 Aug 2009 06:22:01 GMT, arnuld <> wrote:

    >Here is the program and i have some observations about the use of Static
    >storage class specifier. Please tell me whether I am right or wrong:
    >
    >
    >#include <stdio.h>
    >
    >
    >#define DBLVAL 1.001
    >
    >int main(void)
    >{
    > int i = 0;
    > char c = 'c';
    >
    > static char ch;
    > static int x = i;
    > static double d = DBLVAL;
    >
    > ch = c;
    >
    > printf("static int x = %d\n", x);
    > printf("static char ch = %c\n", ch);
    > printf("static doubler d = %f\n", d);
    > return 0;
    >}
    >
    >==================== OUTPUT =========================
    >[arnuld@dune programs]$ gcc -ansi -pedantic -Wall -Wextra static.c
    >static.c: In function ‘main’:
    >static.c:14: error: initializer element is not constant
    >[arnuld@dune programs]$


    If you are going to include these types of messages, add something to
    tell us where line 14 is. Unless the blank line before the #include
    is part of your source, line 14 is the blank line after the definition
    of d.

    --
    Remove del for email
    Barry Schwarz, Aug 8, 2009
    #2
    1. Advertising

  3. arnuld

    Phil Carmody Guest

    Barry Schwarz <> writes:
    > On 08 Aug 2009 06:22:01 GMT, arnuld <> wrote:
    >
    >>Here is the program and i have some observations about the use of Static
    >>storage class specifier. Please tell me whether I am right or wrong:
    >>
    >>
    >>#include <stdio.h>
    >>
    >>
    >>#define DBLVAL 1.001
    >>
    >>int main(void)
    >>{
    >> int i = 0;
    >> char c = 'c';
    >>
    >> static char ch;
    >> static int x = i;


    ....
    >>static.c: In function main:
    >>static.c:14: error: initializer element is not constant


    Well, i isn't constant.

    > If you are going to include these types of messages, add something to
    > tell us where line 14 is. Unless the blank line before the #include
    > is part of your source, line 14 is the blank line after the definition
    > of d.


    Guesswork's part of the fun!

    Phil
    --
    If GML was an infant, SGML is the bright youngster far exceeds
    expectations and made its parents too proud, but XML is the
    drug-addicted gang member who had committed his first murder
    before he had sex, which was rape. -- Erik Naggum (1965-2009)
    Phil Carmody, Aug 8, 2009
    #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. Patrick Hoffmann
    Replies:
    3
    Views:
    2,827
    Christian Janßen
    Aug 8, 2003
  2. Peter van Merkerk

    static variable and stack storage

    Peter van Merkerk, Aug 25, 2003, in forum: C++
    Replies:
    1
    Views:
    5,004
    Eric Liu
    Aug 26, 2003
  3. Pavan
    Replies:
    3
    Views:
    428
    Malcolm
    May 28, 2005
  4. sarathy
    Replies:
    2
    Views:
    658
    sarathy
    Jul 17, 2006
  5. , India
    Replies:
    1
    Views:
    716
    Victor Bazarov
    Dec 8, 2009
Loading...

Share This Page