Character type int

The character constant is of type intin C.

Now suppose my machine local character set is the Latin Latin Windows letter ( http://www.ascii-code.com/ ), which is a 256 character set, so each charbetween single quotes, for example 'x', maps to a value intbetween 0 and 255 on the right?

Suppose plain is char signedon my machine and consider the following code:

char ch = 'â'

if(ch == 'â')  
{
    printf("ok");
}

Due to the integer advance, the chtype number will be increased to negative int(because it has an initial zero) and âdisplayed on a positive number okwill not be printed.

But I'm sure that I have something missing, can you help?

+5
source share
3 answers

Actually the initial assignment will not work as expected:

char ch = 'â';

Overflow occurs here, and gcc will warn you about it. Technically, this behavior is undefined, although for a very common single-byte type, charbehavior is predictably enough - it's a simple integer overflow. Depending on your default character set, this is a multibyte character; I get decimal 50082 if I print it as an integer on my machine.

In addition, the comparison is unacceptable, again, because it is chartoo small to hold the compared value, and again a good compiler will warn about this.

ISO C wchar_t, , , ( -ASCII) , . , , ASCII, .

+1

C . , ( ), ( ) . , GCC . , â.

+1

, char :

char ch = 'â' â 0xFFFFFFE2 0xE2 ch. , .

During processing, the if(ch == 'â')compiler extends ch (0xE2) to integer (0xFFFFFFE2) and compares it with 'â' (also 0xFFFFFFE2), so the condition will be true.

0
source

All Articles