473,322 Members | 1,425 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

overflow

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
Nov 15 '05 #1
7 1432
Christian Christmann wrote:

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.

--
pete
Nov 15 '05 #2
oh,please test a=255;and a=256,you will found that it's not overflow
but show you a wrong glance.

Nov 15 '05 #3
pete <pf*****@mindspring.com> writes:
This is overflow:

signed char a = SCHAR_MAX;

++a;


That's not overflow either, unless SCHAR_MAX == INT_MAX.
Nov 15 '05 #4
Christian Christmann <pl*****@yahoo.de> writes:
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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #5
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
Nov 15 '05 #6
Tim Rentsch wrote:

pete <pf*****@mindspring.com> writes:
This is overflow:

signed char a = SCHAR_MAX;

++a;


That's not overflow either, unless SCHAR_MAX == INT_MAX.


Good point!

This is overflow:

int a = INT_MAX;

++a;

--
pete
Nov 15 '05 #7
On 21 Sep 2005 00:56:05 -0700, "us******@gmail.com"
<us******@gmail.com> wrote in comp.lang.c:
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.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 15 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Andrew Fedoniouk | last post by:
I have a simple test document which produce the following in Mozilla and Opera: http://terrainformatica.com/w3/p2/problem1.png Internet Explorer behaves as per recommendation (I guess) Did I...
6
by: Kai Grossjohann | last post by:
I have a frame which is supposed to contain a list of links. The clickable area of each link comprises a picture followed by some text. I want the text to be cut at the right hand side when the...
4
by: aling | last post by:
Given: signed a, b; How to judge overflow of the sum of these two operands? Just use one *if* statement. Is this statement right? if ((a>0 && b>0 && sum<=0) || (a<0 && b<0 && sum>=0)) //...
19
by: Jim | last post by:
I have spent the past few weeks designing a database for my company. The problem is I have started running into what I believe are stack overflow problems. There are two tab controls on the form...
27
by: REH | last post by:
I asked this on c.l.c++, but they suggested you folks may be better able to answer. Basically, I am trying to write code to detect overflows in signed integer math. I am trying to make it as...
7
by: wij | last post by:
Hi: Is there better way of detecting multiplication overflow for type long than by using double precision of lldiv to verify the result? Thanks in advance. I.J.Wang
25
by: junky_fellow | last post by:
Is there any way by which the overflow during addition of two integers may be detected ? eg. suppose we have three unsigned integers, a ,b, c. we are doing a check like if ((a +b) > c) do...
8
by: starffly | last post by:
In my program, the caculated value is supposed to be no more than the constant named MAXINT,otherwise, overflow error will be informed.however, I cannot test if the value exceeds MAXINT within the...
2
by: lexor | last post by:
Hi! I'm struggling with a strange IE behavior regarding tables. I have a table like the following one <table cellpadding="0" cellspacing="0" border="1" width="500"> <tr> <td><div...
42
by: thomas.mertes | last post by:
Is it possible to use some C or compiler extension to catch integer overflow? The situation is as follows: I use C as target language for compiled Seed7 programs. For integer computions the C...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.