1

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!...

1
  • 1
    Implementation- and target-dependent. But likely will need unsigned char to represent values > 127. Commented Mar 31, 2021 at 23:27

2 Answers 2

2

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.)

Sign up to request clarification or add additional context in comments.

Comments

0

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.

3 Comments

Whether char is signed is implementation-defined.
"Whether char is signed is implementation-defined." what does this mean?
@egekaygusuz: The C standard says a C implementation (typically a compiler, supporting software, and a system to run on) may make the char type either signed or unsigned. “Implementation-defined” means the C standard requires the C implementation to state in its documentation which choice it makes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.