Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Initializing a char as 256

Tags:

c++

char

ascii

My curiosity got the better of me. What would happen (or does happen) when you write the code:

char pseudo_char = 256;

Would it store a virtual char with a value of 256? Or is this illegal and should never be done?

like image 453
C_B Avatar asked Dec 04 '25 10:12

C_B


2 Answers

It's probably an overflow (unless CHAR_MAX >= 256). That's Undefined Behavior, and anything may happen. It's unlikely to format your harddisk.

like image 191
MSalters Avatar answered Dec 05 '25 23:12

MSalters


This would be an undefined behavior. The range of char is -128 to +127 or 0 to 255 based on if it is signed or unsigned, so anything may happen in your case.

Would it store a virtual char with a value of 256?

It will show you an undefined behavior. Something which you cant predict.

Or is this illegal and should never be done?

I would not say that it is illegal but yes if you dont want to get into unpredictable environment then dont do this.

like image 26
Rahul Tripathi Avatar answered Dec 05 '25 23:12

Rahul Tripathi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!