The static keyword documentation in MSDN states can be used in the following situations:
- When you declare a variable or function in the file area.
- When you declare a variable in a function ...
- When you declare a data item in a class declaration
- When you declare a member function in a class declaration ...
The use of the static to declare a local scope is not specified here, so it is not valid.
If you try to write it in the function body:
void foo(){ static{ int i = 0; } }
this will result in the error 'C2143: syntax error: missing'; ' before '{' " because a variable declaration is expected. If you replace static{ with static;{ , the static keyword is ignored, so your code becomes compiled, but the compiler still warns you: "warning C4091: 'static': is ignored to the left of 'int' when the variable is declared .
If you try to write it outside the function body, this will result in the error "C2447: '{': the function header is missing (formal list in the old style?) , Because the function declaration is expected.
source share