0

I suspect this has something to do with scope, but take this code for producing a list with an initial space and subsequent commas, as taken from Expert C Programming, Deep C Secrets by Peter van der Linden:

void generate_initializer(char * string) {
    static char separator = ' ';
    printf("%c %s\n", separator, string);
    separator = ',';
}

Why does separator not get reassigned when it passes by the instruction static char separator = ' ';? I understand that 'static' is telling the compiler to allocate space for separator that extends the length of the program and to also make its scope local only to generate_initializer() but I would assume that the code wouldn't ignore an assignment operation such as this and would always reassign separator as a blank space.

Jayesh Bhoi
  • 24,694
  • 15
  • 58
  • 73
rjs
  • 838
  • 2
  • 10
  • 21
  • Simply not because standard mandates it must not. Without that static wouldn't be so useful! – Adriano Repetti Jul 28 '14 at 05:55
  • possible duplicate of [When do function-level static variables get allocated/initialized?](http://stackoverflow.com/questions/55510/when-do-function-level-static-variables-get-allocated-initialized) – Amir Gonnen Jul 28 '14 at 05:57
  • 2
    The line `static char separator = ' ';` is not an assignment, it's a declaration. Symbols mean different things in declarations than statements, `=` only means assignment in statements. – M.M Jul 28 '14 at 06:01
  • Right, forgot about that difference. Makes perfect sense that compile time assignment would be a one-off thin and "runtime assignment" to be outside of declarations, at least for static. – rjs Jul 28 '14 at 06:32

3 Answers3

1

The line

static char separator = ' ';

gets executed only once -- when separator is initialized.

If you want to reset its value, you have to use:

void generate_initializer(char * string) {
    static char separator = ' ';
    separator = ' ';
    printf("%c %s\n", separator, string);
    separator = ',';
}
R Sahu
  • 204,454
  • 14
  • 159
  • 270
1

static variables are allocated and initialized at compile time, not at runtime. Why? I don't know, let's break it down logically. We can imagine FOUR types of local variables:

  1. allocated and initialized at run time -- i.e., normal local variables
  2. allocated and initialized at compile time -- i.e., static variables
  3. allocated at compile time and (re-)initialized at runtime -- i.e., the sort of variables you apparently expect here
  4. allocated at runtime and initialized at compile time -- i.e., impossible

Okay, (4) is impossible and (3)...what would be the point of (3)? I can't imagine any point. Thus the way it's implemented makes sense.

PS That function, though, is pretty goofy! You can only use it on one string. Then you're done.

Tom Lucas
  • 126
  • 1
  • 3
  • Since enums and other listed, blocked code can have a comma at the end of the list (to be considerate to "automatically generated" code), the author of the book proffers the code snippet as an example that it isn't necessary to allow it. In either case it is quite hackish! – rjs Jul 28 '14 at 06:30
0

For

static char separator = ' '; // Static keyword extend the lifetime of a variable to the lifetime of the programme; 

If a variable declared inside a function scope is static, then it makes it not an automatic variable but a globally allocated one. So the variable will exist after the function exits and thus will keep its value across invocations of the function and also any references (pointers) to it are valid even after the function exits. So it initialized only once.

Jayesh Bhoi
  • 24,694
  • 15
  • 58
  • 73