Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Do

...

not

...

write

...

any

...

executable

...

statement

...

inside

...

a

...

switch

...

loop

...

before

...

the

...

first

...

case

...

statement.

...

The

...

statements

...

will

...

never

...

get

...

executed,

...

as

...

the

...

compiler

...

will

...

ignore

...

the

...

statements

...

present

...

before

...

the

...

first

...

case

...

statement

...

inside

...

the

...

switch

...

block.

...

The

...

compiler

...

will

...

compile

...

the

...

above

...

statements,

...

but

...

while

...

generating

...

assembly

...

for

...

the

...

switch

...

loop,

...

those

...

statements

...

will

...

be

...

ignored.

...

If

...

a

...

programmer

...

declares

...

variables

...

and

...

initializes

...

them

...

before

...

the

...

first

...

case

...

statement

...

and

...

try

...

to

...

use

...

them

...

inside

...

any

...

of

...

the

...

case

...

statements,

...

those

...

variables

...

will

...

have

...

scope

...

inside

...

the

...

switch

...

block,

...

but

...

their

...

value

...

will

...

be

...

taken

...

garbage.

...

Any

...

unexpected

...

result

...

can

...

follow

...

because

...

of

...

the

...

above

...

behavior.

...

Non

...

Compliant

...

Code:

...

In

...

the

...

example

...

mentioned

...

below,

...

the

...

variable

...

i

...

will

...

be

...

instantiated

...

with

...

automatic

...

storage

...

duration

...

within

...

the

...

block,

...

but

...

it’s

...

never

...

initialized.

...

Thus,

...

if

...

the

...

controlling

...

expression

...

has

...

a

...

non-zero

...

value,

...

the

...

cause

...

to

...

printf

...

will

...

access

...

an

...

indeterminate

...

value

...

of

...

i.

...

Similarly,

...

the

...

call

...

to

...

function

...

will

...

also

...

never

...

get

...

executed.

Warning
Wiki Markup

{warning:title=Non Compliant Code:} In the example mentioned below, the variable i will be instantiated with automatic storage duration within the block, but it’s never initialized. Thus, if the controlling expression has a non-zero value, the cause to printf will access an indeterminate value of i. Similarly, the call to function will also never get executed. {

int func ( int expr )
{
     switch (expr) {
	int i = 4;
	    f(i);
     case 0:
	    i = 17;
	   /*falls through into default code */
     default:	
	   printf(“%d\n”, i);
	}
	return 0;
}

}