loumbut5 loumbut5 - 2 months ago 9
C Question

Error C2196: case value '?' already used

Ok, curious error while building with Visual Studio Ultimate 2012 (probably issue with ANSI, unicode etc) in the code...

switch (input[index])
{
case 'א': // Alef Hebrew character
if (/*conditional*/)
{
// Do stuff.
}
break;

case 'ב': // Beth Hebrew character
if (/*conditional*/)
{
//Do stuff
}
break;

default:
{
//Do some other stuff.
}
break;

}


The second case parameter generates...

Error C2196: case value '?' already used


Simple fix if possible.

Answer

The 'א' character cannot fit into a char. Usually it take several bytes, so 'א' constitutes a "multicharacter literal", just like 'ab'(NOTE: SINGLE quote is used).

A "multicharacter literal" has type int, and implementation-defined value. And, different multicharacter literal may have the same value, as there are infinite number of multicharacter literals.

In your case, apparently your compiler treated both 'א' and 'ב' as '?', so it issues an error as the same value('?') appear after case labels twice in a same switch.

Assume you are using wchar_t, the fix is already given by Ivan.

If you are using char, then א is a multicharactar string instead of char. You can use function that compare strings to check whether א appears at a certain index of input:

const int ONE = 1;
if (strncmp(input+index, "א", sizeof("א") - ONE) == 0) {
      // Alef Hebrew character
    if (/*conditional*/) 
    {
       // Do stuff.
    }
} else if (strncmp(input+index, "ב", sizeof("ב") - ONE) == 0) {
     // Beth Hebrew character
    if (/*conditional*/)
    {
        //Do stuff
    }
} else {
    // default:
    // Do 
}
Comments