Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C: char to int conversion

Tags:

c

From The C Programming Language (Brian W. Kernighan), 2.7 TYPE CONVERSIONS, pg 43 :

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

My questions are:

  1. Why would anyone want to store non-char data in char? (an example where this is necessary will be real nice)

  2. Why does integer value of char change when it is converted to int?

  3. Can you elaborate more on this portability issue?

like image 834
Midnight Blue Avatar asked Dec 03 '22 07:12

Midnight Blue


2 Answers

In regards to 1)

People often use char arrays when they really want a byte buffer for a data stream. Its not great practice, but plenty of projects do it, and if you're careful, no real harm is done. There are probably other times as well.

In regards to 2)

Signed integers are often sign extended when they are moved from a smaller data type. Thus 11111111b (-1 in base 10) becomes 11111111 11111111 11111111 11111111 when expanded to 32 bits. However, if the char was intended to be unsigned +255, then the signed integer may end up being -1.

About portability 3)

Some machines regard chars as signed integers, while others interpret them as unsigned. It could also vary based on compiler implementation. Most of the time you don't have to worry about it. Kernighan is just trying to help you understand the details.


Edit

I know this is a dead issue, but you can use the following code to check if char's on your system are signed 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
like image 179
Andres Avatar answered Dec 04 '22 20:12

Andres


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

2 & 3) Whether or not char is signed or unsigned is platform dependent, so if a program depends on this behavior then it's best to specify one or the other explicitly.

like image 28
John Millikin Avatar answered Dec 04 '22 20:12

John Millikin