Question

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?

Was it helpful?

Solution

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

OTHER TIPS

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.

That would produce a warning saying "overflow occured". But I figured out that after 255 any other number assigned to the char - it restarts from 0 and assigns appropriate ASCII char to it. Like

256 -> (null) equivalent to 0

300 = 256+44 -> , equivalent to 44
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top