Microsoft KB Archive/73850

= PRB: Variables with Local Scope to Switch Won't Be Initialized =

Article ID: 73850

Article Last Modified on 12/12/2003

-

APPLIES TO


 * Microsoft Visual C++ 1.0 Professional Edition
 * Microsoft Visual C++ 1.5 Professional Edition
 * Microsoft Visual C++ 2.0 Professional Edition
 * Microsoft Visual C++ 2.1
 * Microsoft Visual C++ 4.0 Standard Edition
 * Microsoft Visual C++ 5.0 Enterprise Edition
 * Microsoft Visual C++ 5.0 Professional Edition

-



This article was previously published under Q73850





SYMPTOMS
In Microsoft C, variables may be declared as local to a switch statement by defining them within the braces that make up the switch. They then have visibility and life for the duration of the switch statement. However, because of the design of a switch block, they will not be initialized when declared unless the variable is static or is declared after a case label.



RESOLUTION
If you must declare an initialized variable that is local to the switch statement and transient in duration, use brackets before and after the switch and place the declaration outside the switch statement.



MORE INFORMATION
The ANSI Standard (Section 3.6.4.2) states:

A &quot;switch&quot; statement causes control to jump to, into, or past the statement that is the switch body, depending on the value of a controlling expression, and on the presence of a &quot;default&quot; label and the values of any &quot;case&quot; labels on or in the switch body. A &quot;case&quot; or &quot;default&quot; label is accessible only within the closest enclosing &quot;switch&quot; statement.

The integral promotions are performed on the controlling expression. The constant expression in each &quot;case&quot; label is converted to the promoted type of the controlling expression. If a converted value matches that of the promoted controlling expression, control jumps to the statement following the matched &quot;case&quot; label. Otherwise, if there is a &quot;default&quot; label, control jumps to the labeled statement. If no converted case constant expression matches and there is no default label, no part of the switch body is executed.

Compiling the sample code below and runnning the resultant executable demonstrates this problem. The following text will be output:   local_int NOT initialized There are three options available to ensure that the variable local_int is initialized to the correct value (5 in this case):   Declare it outside the switch statement, using braces to limit its scope if necessary. For example: {  int local_int = 5; switch (param) {     case 100: local_int+=5; rc = (local_int == 10 ? 1 : 0); break; default: break; } }                         Declare after each case label in which it is to be used. For example: switch (param) {  case 100: {     int local_int = 10; rc = (local_int == 10 ? 1 : 0); break; }  case 101: {     int local_int = 11; rc = (local_int == 10 ? 1 : 0); break; }  default: break; }                         Declare the variable as static. /* Compile options needed: none

int _cdecl printf(const char *, ...); int switch_func(int); int main(void);

int main(void) {  int rc = switch_func(100);

if ( rc ) printf(&quot;local_int initialized\n&quot;); else printf(&quot;local_int NOT initialized\n&quot;);

return( rc ); }

int switch_func(int param) {  int rc = 0;

switch (param) {     int local_int = 5; case 100: local_int+=5; rc = (local_int == 10 ? 1 : 0); break; default: break; }

return( rc ); }                       

Keywords: kblangc kbprb KB73850

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.