As we know the range of a char variable is from -128 to 127 and the maximum value is 255 (in case of unsigned char). So kindly clear the below doubts of mine:
char c=255;As it is exceeding the range so why it wraps up 255 instead of giving errors?Why is there no ascii values of negative char values (-128 to -1)? like 48-57 for 0-9 (char values)
when we do
int x=4*1024*1024*1024;it gives overflow error but, when we doint x=4 * 1024; x*=(1024*1024);it (compiler) does not give error instead does wrapping. Why?
CodePudding user response:
As we know the range of a char variable is from -128 to 127 and the maximum value is 255 (in case of unsigned char)
The C standard requires the range of unsigned char to be at least 0 to 255. You can make a C implementation with a larger range for unsigned char, with more than eight bits for it.
char may be signed (with range at least −127 to 127) or unsigned.
1.)char c=255;// As it is exceeding the range so why it wraps up 255 instead of giving errors??
The rule in the C standard is that 255 will be converted to the type of char in an implementation-defined way, if 255 is outside the range of char. So each C implementation can define its own rule for the conversion.
A compiler can go beyond the C standard and issue a warning for this. If you enable additional warnings in your compiler, it might do that.
2.)Why is there no ascii values of negative char values** (-128 to -1)** ?? like 48-57 for 0-9( char values)
This is like asking why there are only seven items on my grocery list. There are not more because I did not put any more on the list. The ASCII committee only defined characters for codes 0 to 127. There are not more codes in ASCII because the committee did not define more.
3.)when we do int x=4102410241024; it gives overflow error but, when we do int x=4 * 1024; x=(1024*1024); it(compiler) do not gives error instead does wrapping. Why?
When asking about error messages, you should give the exact text of the error message. I do not know which “overflow error” your compiler is giving you, but it is likely warning you that the right operand is larger than the type of the left operand can represent. With a constant, it is easy for the compiler to see the value is too large and give you that warning. With an expression involving operators, it would take more work for the compiler to see the result will be too large and give you a warning. Some compilers will do this, if it is not too difficult to compute the value of the expression at compile time.
The result of an operation that overflows is not defined by the C standard. Conversions such as the above are required to be implementation-defined, but if you do a multiplication that overflows, the implementation is not required to define the behavior. Some C implementations may define the arithmetic to wrap, but they are not required to do so.
CodePudding user response:
Programming is all about rules — following them, or at least knowing what they are.
And one of the rules is that, sometimes, there are no rules.
As we know the range of a char variable is from -128 to 127 and the maximum value is 255 (in case of unsigned char)
Well, actually, no, we don't know that. The actual rules are that the range of signed char is from at least -127 to 127 (but on some systems it might be more), and the range of unsigned char is from at least 0 to 255 (but it might be more), and the type of plain char might be either signed or unsigned.
There's also another rule that says that, in most cases, the behavior on overflow is not perfectly defined (often it's formally undefined), which means that, in some cases, the compiler can do whatever it wants, and it doesn't even have to be consistent. (This is what I meant when I said that "one of the rules is that, sometimes, there are no rules".)
So when you wrote
char c = 255;
it might be the case that plain char on your machine is unsigned (in which case there's no overflow at all), or it might be the case that, although there was an overflow, your compiler decided not to complain about it.
Why is there no ascii values of negative char values** (-128 to -1)** ??
Because the original ASCII was a 7-bit code. In those days, the eighth bit was often a parity bit, which meant that you only had 7 real bits to play with. Eventually, definitions for the other 128 values (those with the eighth bit set) did arise, the most popular being the ISO 8859 sets, and also various MS-DOS, Windows, and Macintosh character sets. However, for any 8-bit character set, it's not always clear whether the "extended" characters corresponded to the values 128 — 255, or to the values -1 — -128. (This is related to the fact that, again, plain char can be signed or unsigned.)
3.)when we do int
x=4*1024*1024*1024; it gives overflow error but, when we do intx=4 * 1024; x*=(1024*1024); it(compiler) do not gives error instead does wrapping. Why?
This is what I meant earlier when I said that "in some cases, the compiler can do whatever it wants, and it doesn't even have to be consistent". Some compilers do try to warn about integer overflow, but sometimes their attempts can seem rather half-hearted. See Eric Postpischil's answer for more information about this case.
