char Type

This page was translated by a robot.

The type charstores an integer value encoded with 1 byte, which nowadays usually corresponds to 8 bits. A charis basically used to store a character (such as a letter)..

Details

The name charstands for Character . This name is rooted in the use of the base C language character set, in which each character takes up 7 bits of space. According charto the standard, a must be able to store a character from the base character set. The number of bits required for this is defined by the byte unit and is always 8 on today's computers.

In the past, the type was charoften used like tiny int(some programming languages ​​have a tiny int type ). This allowed a programmer to specify how many bits to use for an integer, similar to the type modifiers short, longor . long longToday, this usage is seen less and less, since from the C99 standard onwards, many integer types with more precise information on the number of bits are available with the stdintlibrary .

A chartype can be modified with either signedor . unsignedDepending on the case, a stores chareither negative as well as positive values, or explicitly only positive ones. Whether the modifier comes before or after the type doesn't matter.

char               a;
signed char        b;
char signed        c;
unsigned char      d;
char unsigned      e;

signedIf the modifier is omitted, either or unsignedis assumed , depending on the compiler . A compiler can often be steered to use one or the other by means of certain flags. By including the limitslibrary one can find out what is the case with the current compiler:

#include <stdio.h>
#include <limits.h>

int main(){
  if(CHAR_MAX == SCHAR_MAX){
    printf("Char is signed\n");
  }else{
    printf("Char is unsigned\n");
  }
  return 0;
}

All maximum and minimum possible values ​​of a signed and unsigned modifier are defined in the limitslibrary .char.

Positive values ​​are stored as a binary integer . The negative values ​​of a are usually encoded signed intusing two's complement on today's computers.

If the value range of one is exceeded by an arithmetic operation char, this is referred to as an overflow . On today's processors, a signed integer suddenly flips from the positive to the negative range, and in the case of unsigned integers, the number 0(zero) suddenly follows the maximum value. This is called a wrap around . Similarly, falling below the value range is referred to as underflow and the wrap-around happens vice versa. However, there is no guarantee that this effect will occur on every system.

When specifying fixed values ​​in source code, the suffix ucan be used to denote a fixed value as unsigned. However, unlike longand long long, there is no suffix charto mark a fixed value as . Corresponding integers permanently written in the program code always have at least the type int.

Today's compilers usually issue warnings when fixed values ​​are too big for charyou.

To output one charwith or read in with, the modifier is used:printfscanfhh:


123
char c = 123;
printf("%hhd\n", c);

Arithmetic Conversion

In the case of assignments, initializations or casts to a chartype, the compiler may perform arithmetic conversions . The conversion rules for the chartype are described below. In the case of unsafe conversions, a compiler usually issues a warning, which can be suppressed using an explicit cast if necessary.

When converting a floating point type to a floating point type char, the decimal places are simply truncated (rounding towards zero).

123
-123
printf("%hhd\n", (signed char) 123.456);
printf("%hhd\n", (signed char)-123.456);

If the floating point number to be converted has no place in a char, the result is undefined. On today's systems, the maximum or minimum possible value is often assumed.

127
-128
255
0
printf("%hhd\n", (signed   char) 1e20);
printf("%hhd\n", (signed   char)-1e20);
printf("%hhu\n", (unsigned char) 1e20);
printf("%hhu\n", (unsigned char)-1e20);

The result can vary depending on the system. The values ​​given here correspond to the values ​​on the author's system.

When converting an integer type to an char, the value and the sign are preserved exactly in the most important cases:





123
-123
123
-123
unsigned  short s1 =  123;
signed    short s2 = -123;
unsigned  int   i1 =  123;
signed    int   i2 = -123;
printf("%hhu\n", (unsigned char) s1);
printf("%hhd\n", (signed   char) s2);
printf("%hhu\n", (unsigned char) i1);
printf("%hhd\n", (signed   char) i2);

The conversion of an integer value into an char, which is not included in the value range, is not defined. However, the following usually happens on today's systems: If the value to be assigned has the same number or more bits, then all those lowest-order bits of the value to be assigned are copied bit-exactly, which charcan be accommodated in the type. If the value to be assigned has fewer bit positions, the available bit positions are charcopied to the least significant bit positions of the . The non-defined higher-order bit positions are filled as follows: If the type of the value to be assigned is unsigned, all non-defined bit positions are filled with the bit0copied. If the type of value to be assigned is signed, the most significant bit of the available bit positions is copied to all high-order bit positions.

Because of these rules, the following cases can lead to incorrect values: A signed number cannot be converted correctly to an unsigned type. Too large an unsigned number can result in negative values ​​when cast to a signed type. When converting (signed or unsigned) large numbers (especially short, int, longand long long), the value can be falsified by truncating to fewer bits or even lead to the wrong sign.

133
-56
16
-96
printf("%hhu\n", (unsigned char) -123);
printf("%hhd\n", (signed   char)  200);
printf("%hhd\n", (signed   char)  10000);
printf("%hhd\n", (signed   char)  100000);

A Javascript program to illustrate the effects of cutting off bits can be found at Number System Converter.