Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

significance of negative values of type char in C

Tags:

c

character

chars

  1. chars in 'C' are used to represent characters.
  2. Numbers representing characters in all code pages are always positive.

What is the use of having signed characters?? Are negative values contained in chars used only as integral values in a smaller integral data-type than int and short?? Do they have no other interpretation??(like positive values in chars representing characters)

like image 645
Abhijith Madhav Avatar asked Dec 21 '09 15:12

Abhijith Madhav


People also ask

Can char store negative values in C?

Char is an unsigned type and cannot represent a negative value. In any case, you should not use Char to hold numeric values.

Can a char have a negative value?

Negative numbers can be assigned to char variables. To assign negative numbers “signed” type qualifier is used. I believe most compilers use signed char by default. To retrieve the negative number assigned a simple printf statement with integer format specifier (%d) will suffice.

What is the ASCII value of negative numbers?

There is no such thing as a "negative ASCII" value. ASCII defines character and control codes for values from 0 to 127.

Can char store negative numbers in Java?

One of the tricky parts of this question is that Java has multiple data types to support numbers like byte, short, char, int, long, float, and double, out of those all are signed except char, which can not represent negative numbers.


2 Answers

Only characters of the basic execution character set are guaranteed to be nonnegative (C99, 6.5.2 §3):

An object declared as type char is large enough to store any member of the basic execution character set. If a member of the basic execution character set is stored in a char object, its value is guaranteed to be nonnegative. If any other character is stored in a char object, the resulting value is implementation-defined but shall be within the range of values that can be represented in that type.

You have to discern between the 'plain' char type and the types signed char and unsigned char as well: signed char and unsigned char are ordinary integer types for which the following holds (C99, 6.5.2 §5):

An object declared as type signed char occupies the same amount of storage as a ‘‘plain’’ char object.

like image 96
Christoph Avatar answered Sep 22 '22 04:09

Christoph


chars in 'C' are used to represent characters.

Not always, chars are used to represent bytes, they are the only type in c with a known size.

like image 24
Martin Beckett Avatar answered Sep 25 '22 04:09

Martin Beckett