Difference between char and int when declaring character

前端 未结 5 1615
梦谈多话
梦谈多话 2020-12-07 18:49

I just started learning C and am rather confused over declaring characters using int and char.

I am well aware that any characters are made up of integers in the se

5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-07 19:23

    The char type has multiple roles.

    The first is that it is simply part of the chain of integer types, char, short, int, long, etc., so it's just another container for numbers.

    The second is that its underlying storage is the smallest unit, and all other objects have a size that is a multiple of the size of char (sizeof returns a number that is in units of char, so sizeof char == 1).

    The third is that it plays the role of a character in a string, certainly historically. When seen like this, the value of a char maps to a specified character, for instance via the ASCII encoding, but it can also be used with multi-byte encodings (one or more chars together map to one character).

提交回复
热议问题