Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

character type int

A character constant has type int in C.

Now suppose my machine's local character set is Windows Latin-1 ( http://www.ascii-code.com/) which is a 256 character set so every char between single quotes, like 'x', is mapped to an int value between 0 and 255 right ?

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

char ch = 'â'

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

Because of the integer promotion ch will be promoted into a negative quantity of type int (cause it has a leading zero) and beingâ mapped to a positive quantity ok will not be printed.

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

like image 804
GionJh Avatar asked Nov 04 '22 19:11

GionJh


1 Answers

Your C implementation has a notion of an execution character set. Moreover, if your program source code is read from a file (as it always is), the compiler has (or should have) a notion of a source character set. For example, in GCC you can tune those parameters on the command line. The combination of those two settings determines the integral value that is assigned to your literal â.

like image 175
Kerrek SB Avatar answered Nov 13 '22 04:11

Kerrek SB