viuser
viuser

Reputation: 965

What counts as character type in C11?

What actually belongs to the "character type" in C11 — besides char of course?

To be more precise, the special exceptions for the character type (for example that any object can be accessed by an lvalue expression of character type — see §6.5/7 in C11 standard), to which concrete types do they apply? They seem to apply to uint8_t and int8_t from stdint.h, but is this guaranteed? On the other hand gcc doesn't regard char16_t from uchar.h as a "character type".

Upvotes: 13

Views: 986

Answers (2)

2501
2501

Reputation: 25752

Only char, signed char and unsigned char1.

The types uint8_t, int8_t, char16_t, or any type in the form intN_t or charN_t, may or may not be synonyms for a character type.


1(Quoted from: ISO/IEC 9899:201x 6.2.5 Types 15)
The three types char, signed char, and unsigned char are collectively called the character types.

Upvotes: 8

P.P
P.P

Reputation: 121387

char, signed char, and unsigned char are the character types in C11. This is the same since C89.

Treating int8_t (or uint8_t) as a character type has many problems.

  1. They are optional.
  2. They may not exist if CHAR_BIT > 8.
  3. Defined to work if the implementation uses 2's complement representation (which is the most common). But they are other representations, namely 1's complement and sign-magnitude defined/allowed by the C standard.

Since they are, if they exist, typedef'ed to an existing type, you can probably get away with using int8_t or uint8_t as a character type in practice. But the standard doesn't guarantee anything and there's no reason to treat them as such anyway when you have the real character types.

Upvotes: 7

Related Questions