C: char to convert int

From the C programming language (Brian W. Kernighan), 2.7 TYPE CONVERSIONS, p. 43:

"There is one subtle point about converting characters to integers .... On some macines a char, whose leftmost bit is 1, will be converted to a negative integer. On others, ... always positive. For portability, specify a signed or unsigned if non-character data should be stored in char variables. "

My questions:

  • Why does someone want to store non-char data in char? (an example where necessary is really good)

  • Why does the integer char change when it is converted to int?

  • Can you talk more about the issue of portability?

+4
source share
6 answers

Regarding 1)

People often use char arrays when they really need a byte buffer for data flow. This is not a good practice, but many projects do this, and if you are careful, there will be no real harm. There are probably other times.

Regarding 2)

Signed integers often expand when they move from a smaller data type. thus 11111111b (-1 in base 10) becomes 11111111 11111111 11111111 11111111 when expanding to 32 bits. However, if char was supposed to be unsigned +255, then the signed integer could end up being -1.

About tolerance 3)

Some machines treat characters as signed integers, while others interpret them as unsigned. It may also vary depending on the compiler implementation. Most of the time you do not need to worry about it. Kernighan is simply trying to help you figure out the details.


Edit

I know this is a dead issue, but you can use the following code to check if char is signed on your system or unsigned:

#include <limits.h> //Include implementation specific constants (MAX_INT, et c.) #if CHAR_MAX == SCHAR_MAX // Plain "char" is signed #else // Plain "char" is unsigned #endif 
+6
source

1) char is the size of one byte in C and therefore is used to store any data. For example, when loading an image into memory, data is represented as a char array. In modern code, typedefs such as uint8_t are used to indicate the purpose of a buffer more useful than just char .

2 and 3) Regardless of whether the char signed or not, or the unsigned is platform dependent, therefore, if the program depends on this behavior, it is better to specify one or the other explicitly.

+6
source
  • The char type is defined to store one byte, i.e. sizeof(char) is defined as 1 . This is useful for serializing data, for example.

  • char defined by the implementation as an unsigned char or signed char . Now imagine char means smallint . You simply convert a small integer to a larger integer when you go from smallint to int . The problem is that you do not know if it is smallint or not.

  • I would say that this is not a portability issue if you follow the Bible (K&R).

+3
source

unsigned char often used to process binary data one byte at a time. A common example is UTF-8 strings, which are not strictly composed of "characters".

If the signed char is 8 bits and the top bit is set, it means that it is negative. When it converts to a larger type, the character is preserved, expanding the high bit to the top bit of the new type. This is called an extended character designation.

+1
source

1) Char is implemented as one byte on all systems, so it is consistent.

2) The bit mentioned in your question is the one that is used in single byte integers to sign them. When an int in the system is more than one byte, the signed apartment does not affect the conversion of Char to int, in other words, it is. (there are also foamy and unsigned characters)

3). Because of the Char implementation sequence, many libraries use them as Intel IPP (Intel Performance Primitives) libraries and their OpenCV cousins.

+1
source

Usually in C, char to convert int and vice versa, a problem arises because stanard APIs for reading character input / writing characters use int's for character arguments and return values. See getchar() , getc() and putchar() , for example.

In addition, since char is 1 byte in size, this is a convenient way to process arbitrary data in the form of a byte stream.

+1
source

All Articles