overflow

  • Thread starter Christian Christmann
  • Start date
C

Christian Christmann

Hi,

I would like to hear your opinion about the C standards.

When I have a

signed char a = 130;

I explicitly generate an overflow since the maximum value a singned char
can accommodate is "127". Is this behavior of the overflow defined in any
way or do I get an undefined state of "a" due to the C standards?

Thank you
Chris
 
P

pete

Christian said:
Hi,

I would like to hear your opinion about the C standards.

When I have a

signed char a = 130;

I explicitly generate an overflow
since the maximum value a singned char
can accommodate is "127".

That's the minimum maximum value.
SCHAR_MAX is higher than that, if CHAR_BIT is greater than 8.
Is this behavior of the overflow defined in any way

That's not overflow.
That's the assignment of an out of range value.
or do I get an undefined state of "a" due to the C standards?

Overflow must be the result of an arithmetic operation.

This is overflow:

signed char a = SCHAR_MAX;

++a;

Undefined behavior of part of the code, means that
the behavior of the entire program is undefined.
 
U

usr.root

oh,please test a=255;and a=256,you will found that it's not overflow
but show you a wrong glance.
 
K

Keith Thompson

Christian Christmann said:
I would like to hear your opinion about the C standards.

When I have a

signed char a = 130;

I explicitly generate an overflow since the maximum value a singned char
can accommodate is "127". Is this behavior of the overflow defined in any
way or do I get an undefined state of "a" due to the C standards?

Assuming SCHAR_MAX==127 (which is common but not universal), the
expression 130 is of type int, so it's implicitly converted to type
signed char. To find out what happens, we have to read C99 6.3.1.3,
which covers conversion of signed and unsigned integers:

When a value with integer type is converted to another integer
type other than _Bool, if the value can be represented by the
new type, it is unchanged.

Otherwise, if the new type is unsigned, the value is converted
by repeatedly adding or subtracting one more than the maximum
value that can be represented in the new type until the value is
in the range of the new type.

Otherwise, the new type is signed and the value cannot be
represented in it; either the result is implementation-defined
or an implementation-defined signal is raised.

The third paragraph applies here; either an implementation-defined
value is assigned to a, or an implementation-defined signal is raised.
(The wording about signals is new in C99.)

It's likely that the implementation-defined value will be -126, but
the standard doesn't guarantee that.

The rules are different for conversions and for arithmetic operations.
If an arithmetic operation overflows for a signed type, the behavior
is undefined.
 
C

Christian Christmann

The third paragraph applies here; either an implementation-defined value
is assigned to a, or an implementation-defined signal is raised. (The
wording about signals is new in C99.)


Thank you very much. This was an illuminating answer and help
me a lot.

Regards,
Chris
 
J

Jack Klein

oh,please test a=255;and a=256,you will found that it's not overflow
but show you a wrong glance.

Please don't post nonsense in this group, with improper grammar,
punctuation, and capitalization to boot.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top