shaik nisar ahmed
shaik nisar ahmed

Reputation: 45

How many 'char' types are there in C?

I have been reading "The C Programming Language" book by "KnR", and i've come across this statement: "plain chars are signed or unsigned"

  1. So my question is, what is a plain char and how is it any different from signed char and unsigned char?
  2. In the below code how is 'myPlainChar' - 'A' different from 'mySignChar' - 'A' and 'myUnsignChar' - 'A'?
  3. Can someone please explain me the statement "Printable char's are always positive".

Note: Please write examples and explain. Thank you.

{
    char myChar = 'A';
    signed char mySignChar = 'A';
    unsigned char myUnsignChar = 'A';
}

Upvotes: 1

Views: 1046

Answers (5)

chux
chux

Reputation: 153447

How many char types are there in C?

There is one char type. There are 3 small character types: char, signed char, unsigned char. They are collectively called character types in C.

char has the same range/size/ranking/encoding as signed char or unsigned char, yet is a distinct type.


  1. what is a plain char and how is it any different from signed char and unsigned char?

They are 3 different types in C. A plain char char will match the same range/size/ranking/encoding as either singed char or unsigned char. In all cases the size is 1.


2 .how is myPlainChar - 'A' different from mySignChar - 'A' and myUnsignChar - 'A'?

myPlainChar - 'A' will match one of the other two.
Typically mySignChar has a value in the range [-128...127] and myUnsignChar in the range of [0...255]. So a subtraction of 'A' (typically a value of 65) will result a different range of potential answers.


  1. Can someone please explain me the statement "Printable char's are always positive".

Portable C source code characters (the basic execution character set) are positive so printing a source code file only prints characters of non-negative values.

When printing data with printf("%c", some_character_type) or putc(some_character_type) the value, either positive or negative is converted to an unsigned char before printing. Thus it is a character associated with a non-negative value that is printed.

C has isprint(int c) which "tests for any printing character including space". That function is only valid for values in the unsigned char range and the negative EOF. isprint(EOF) reports 0. So only non-negative values pass the isprint(int c) test.

C really has no way to print negative values as characters without undergoing a conversion to unsigned char.

Upvotes: 0

i486
i486

Reputation: 6563

There are signed char and unsigned char. Whether char is signed or unsigned by default depends on compiler and its settings. Usually it is signed.

Upvotes: 2

n. m. could be an AI
n. m. could be an AI

Reputation: 119877

Plain char is the type spelled char without signed or unsigned prefix.

Plain char, signed char and unsigned char are three distinct integral types (yes, character values are (small) integers), even though plain char is represented identically to one of the other two. Which one is implementation defined. This is distinct from say int : plain int is always the same as signed int.

There's a subtle point here: if plain char is for example signed, then it is a signed type, and we say "plain char is signed on this system", but it's still not the same type as signed char.

The difference between these two lines

signed char mySignChar = 'A';
unsigned char myUnsignChar = 'A';

is exactly the same as the difference between these two lines:

signed int mySignInt = 42;
unsigned int myUnsignInt = 42;

The statement "Printable char's are always positive" means exactly what it says. On some systems some plain char values are negative. On all systems some signed char values are negative. On all systems there is a character of each kind that is exactly zero. But none of those are printable. Unfortunately the statement is not necessarily correct (it is correct about all characters in the basic execution character set, but not about the extended execution character set).

Upvotes: 1

Some programmer dude
Some programmer dude

Reputation: 409166

There is only one char type, just like there is only one int type.

But like with int you can add a modifier to tell the compiler if it's an unsigned or a signed char (or int):

signed char   x1;  // x1 can hold values from -128 to +127 (typically)
unsigned char x2;  // x2 can hold values from 0 to +255 (typically)
signed int    y1;  // y1 can hold values from -2147483648 to +2147483647 (typically)
unsigned int  y2;  // y2 can hold values from 0 to +4294967295 (typically)

The big difference between plain unmodified char and int is that int without a modifier will always be signed, but it's implementation defined (i.e. it's up to the compiler) if char without a modifier is signed or unsigned:

char x3;  // Could be signed, could be unsigned
int  y3;  // Will always be signed

Upvotes: 1

imqqmi
imqqmi

Reputation: 463

I think it means char without 'unsigned' in front of it ie:

unsigned char a;

as opposed to

char a; // signed char

So basically a variable is always signed (for integers and char) unless you use the statement 'unsigned'.

That should answer the second question as well.

The third question: Characters that are in the ascii set are defined as unsigned characters, ie the number -60 doesn't represent a character, but 65 does, ie 'A'.

Upvotes: -3

Related Questions