Matt
Matt

Reputation: 179

Unexpected Error C2632: 'char' followed by 'char' is illegal

I am having a piece of code as

    typedef unsigned char   _uint8;
    typedef unsigned short  _uint16;
    typedef unsigned int    _uint32;
    typedef float           _float32;

    typedef char            _int8;
    typedef short           _int16;
    typedef int             _int32;

I'm getting error at the lines that are in the last three lines

The error message is

error C2632: 'char' followed by 'char' is illegal
error C2632: 'short' followed by 'short' is illegal

error C2632: 'int' followed by 'int' is illegal

Upvotes: 3

Views: 7204

Answers (2)

Dietrich Epp
Dietrich Epp

Reputation: 213856

In C and C++, it is illegal (invokes undefined behavior) to define your own identifiers which start with an underscore. Only the compiler and standard library are allowed to use names that begin with an underscore. It appears that these particular names (_int8, etc) have already been defined as macros or intrinsic types.

You must choose different names.

Citation

From n1570 §7.1.3,

All identifiers that begin with an underscore are always reserved for use as identifiers with file scope in both the ordinary and tag name spaces.

...

If the program declares or defines an identifier in a context in which it is reserved (other than as allowed by 7.1.4), or defines a reserved identifier as a macro name, the behavior is undefined.

Therefore, by attempting to define the type _int8, your program invokes undefined behavior. "Undefined behavior" is a technical term, and it means that the compiler makes no guarantees: maybe you will get an error message, maybe it will work as you expect, or maybe something else will happen entirely. Maybe there will be no error message, but your program won't work correctly.

Example

Try compiling the following code in Visual Studio...

int main()
{
    _int8 x = 1;
    return 0;
}

It compiles... which means that _int8 is already in use by the compiler to define a type. It appears to be an undocumented type, since an MSDN search turns up nothing. But you're not allowed to define your own _int8 anyway.

Upvotes: 2

Carl Norum
Carl Norum

Reputation: 225272

Those types have already been created via typedef (maybe) or #define (more likely) before your code tries to do it; taking a look at the preprocessor output (if that's possible with Visual-C++) may help you track down why that is.

Upvotes: 2

Related Questions