static variable in for loop init

Discussion in 'C Programming' started by paulw@mmail.ath.cx, Mar 11, 2005.

  1. Guest

    Hi

    I have a question:

    main() { static int i;

    printf("%d\n",i); // should I see see 0 or 5 ???

    for (i=5;i<=15;i++) {...} // What's the meaning of static
    variable in
    // for's init section???
    }
    , Mar 11, 2005
    #1
    1. Advertising

  2. Richard Bos Guest

    wrote:

    > main() {


    Note that in C99, implicit int is no longer legal; in that version of
    the language, you have to write int main(). int main(void) is better
    yet.

    > static int i;
    >
    > printf("%d\n",i); // should I see see 0 or 5 ???


    Wherever do you get the idea that you _could_ see 5? There's nothing
    that gives i any value other than the default 0 that all static objects
    have.

    > for (i=5;i<=15;i++) {...} // What's the meaning of static
    > variable in
    > // for's init section???


    None whatsoever. The only differences, from the programmer's point of
    view, between a static and an automatic object are:
    1. Statics are initialised to 0; automatic variables start out
    containing garbage unless you explicitly initialise them. This is
    relevant to the printf() above, since that relies on i _having_ a value,
    and not containing garbage; but it is of no importance to the for loop,
    since the first thing you do is give it another value.
    2. Statics exist throughout the program's run, and retain their value
    between calls of the function they're in; automatics are destroyed and
    recreated every time their declarations are encountered, and get their
    initial value (or garbage) each time. Since i is in main(), and you
    don't call main() recursively, this is of no importance to your program;
    even if you did, it still wouldn't mean a thing to the for loop, since,
    again, you give i a new value first thing you do in the loop.

    Richard
    Richard Bos, Mar 11, 2005
    #2
    1. Advertising

  3. -berlin.de Guest

    wrote:
    > I have a question:


    > main() { static int i;


    main() is a function that always returns an int and that takes
    either 0 or 2 arguments. Since in your case main() doesn't seem
    to take an argument better make that

    int main( void )

    > printf("%d\n",i); // should I see see 0 or 5 ???


    You will see "0" since static variables are always initialized
    to 0 at program startup.

    > for (i=5;i<=15;i++) {...} // What's the meaning of static
    > variable in
    > // for's init section???


    It has no special meaning, it's just a variable that has declared
    as static. And that doesn't seem to make to much sense here - local
    static variables are useful if you need to retain the value of a
    variable between invocations of a function, but since you probably
    won't call main() recursively and you initilize it at the start of
    the loop anyway having the variable declared as static doesn't make
    any difference.

    BTW, C++-stype comments (the ones starting with a "//") can get
    you in trouble, only compilers following the newer C99 standard
    must understand them. And when posting code better use the "old"
    form of comments since, as you see, line wraping can happen and
    then it's much harder to simply copy and paste the program for
    testing purposes.
    Regards, Jens
    --
    \ Jens Thoms Toerring ___ -berlin.de
    \__________________________ http://www.toerring.de
    -berlin.de, Mar 11, 2005
    #3
  4. wrote:
    > Hi
    > I have a question:


    > main() { static int i;
    > printf("%d\n",i); // should I see see 0 or 5 ???
    > for (i=5;i<=15;i++) {...} // What's the meaning of static
    > variable in
    > // for's init section???
    > }


    Note that uncompilable code is not a good idea. Let's start with this
    version of your code
    [1]
    #include <stdio.h>

    int main(void)
    {
    static int i;

    printf("%d\n", i);
    for (i = 5; i <= 15; i++)
    printf("%d%c", i, (i == 15) ? '\n' : ' ');
    return 0;
    }

    The difference between this and
    [2]
    #include <stdio.h>

    int main(void)
    {
    int i;

    printf("%d\n", i);
    for (i = 5; i <= 15; i++)
    printf("%d%c", i, (i == 15) ? '\n' : ' ');
    return 0;
    }

    is that in [1] i is initialized (to 0) and in [2] it is used
    uninitialized in the first printf statement. Static variables are
    initialized by default (although initialization by default is probably a
    poor programming practice). So [1] has well defined output:
    0
    5 6 7 8 9 10 11 12 13 14 15
    while [2], using an uninitialized variable, does not.

    The example here does not really show why a static variable might be
    used. Variables within a block without the 'static' are 'auto'
    (automatic) variables. Each entry to the block has a logically new
    variable to work with. Static variables are an implementation of what
    in Algol were called 'own' variables; they persist from one use of the
    block to the next. Here's an example that I hope will show the difference:

    #include <stdio.h>

    void auto_i(void)
    {
    int i = 0;
    printf("In 'auto_i', i = %d\n", i);
    i++;
    }

    void static_i(void)
    {
    static int i = 0;
    printf("In 'static_i', i = %d\n", i);
    i++;
    }

    int main(void)
    {
    int j;
    for (j = 0; j < 4; j++) {
    auto_i();
    static_i();
    }
    return 0;
    }

    [output]
    In 'auto_i', i = 0
    In 'static_i', i = 0
    In 'auto_i', i = 0
    In 'static_i', i = 1
    In 'auto_i', i = 0
    In 'static_i', i = 2
    In 'auto_i', i = 0
    In 'static_i', i = 3
    Martin Ambuhl, Mar 11, 2005
    #4
    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. Tony Cheng
    Replies:
    1
    Views:
    8,186
    Juan T. Llibre
    Feb 24, 2006
  2. Replies:
    1
    Views:
    649
    Jules
    Aug 18, 2005
  3. Jess
    Replies:
    4
    Views:
    430
  4. news.aon.at
    Replies:
    11
    Views:
    632
    Ian Collins
    Jan 29, 2011
  5. Isaac Won
    Replies:
    9
    Views:
    351
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page