Default Optimization Generates Bad Code in Switch Construct

ID Number: Q65308

6.00

MS-DOS

buglist6.00 fixlist6.00a

Summary:

The Microsoft C Compiler version 6.0 incorrectly compiles the sample

program below when /Ot, /On, /Oc, or default optimization is used.

Under these conditions, the program fails to recognize certain keys as

"default" (function keys and the top row of number keys 1, 2, 3, and

so on). When compiled without the offending optimizations, all keys

are recognized.

The workaround is to use the optimize pragma, as follows:

#pragma optimize("tn","off")

Or, use the quick compile option, /qc.

Microsoft has confirmed this to be a problem in C version 6.0. This

problem was corrected in C version 6.0a.

Sample Code

-----------

/* Compile options needed: none

*/

#include <stdio.h>

#include <bios.h>

#include <stdlib.h>

#define ESC 0x1b

#define HT 0x09

#define S_TAB 0x0f

#define RT 0x4d

#define BS 0x08

#define CR 0x0d

void main(void)

{

unsigned c;

printf("\n");

while (1)

{

c = _bios_keybrd(_KEYBRD_READ);

if (! (c&0xff))

c >>= 8;

else

c &= 0xff;

switch Ó

{

case ESC:

printf("ESCAPE\n");

exit(0);

case HT:

printf("HT\n");

break;

case S_TAB:

printf("SHIFTED TAB\n");

break;

case RT:

printf("RIGHT ARROW\n");

break;

case BS:

printf("BACK SPACE\n");

break;

case CR:

printf("CARRIAGE RETURN\n");

break;

default:

printf("DEFAULT\n");

break;

}

}

}

Additional reference words: 6.00 6.00a