user15509107
user15509107

Reputation:

Can I use the Char variable as an integer value in C?

I want to do what I stated in the question. Is this possible?

For example:

char number=5;
char number_two=number*3;
char x=number_two/15;

Can I do the arithmetic above?And if the answer to all of these is yes, would a char variable have a minimum value of 0 and a maximum value of 255?

THANKS!...

Upvotes: 1

Views: 73

Answers (2)

Eric Postpischil
Eric Postpischil

Reputation: 222536

The char type is an integer type, like short, int, and long. It is implementation-defined whether it is signed or unsigned.

If it is signed, it must support a range from −127 to +127, inclusive. If it is unsigned, it must support a range from 0 to 255, inclusive. (So a common range it must support whether it is signed or not is 0 to 127.)

If you want to guarantee a range of 0 to 255, you should use unsigned char. (A C implementation may also provide larger ranges for char and unsigned char.)

Upvotes: 2

Outrageous Bacon
Outrageous Bacon

Reputation: 562

You can do what you ask. However, char could be a signed type depending on the implementation you're using. If you want to guarantee you get a range 0 to 255, you will need to use unsigned char type.

Upvotes: 0

Related Questions