473,698 Members | 2,571 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

why overflow?

**** Post for FREE via your newsreader at post.usenet.com ****

Compiled by Turboc 2.0,PC with win2000

main()
{
int a=32767;
long b=a+1;
printf("%d,%ld" ,a,b);
}

that output a and b with 32767,-32768.why overflow?

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
*** Usenet.com - The #1 Usenet Newsgroup Service on The Planet! ***
http://www.usenet.com
Unlimited Download - 19 Seperate Servers - 90,000 groups - Uncensored
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Nov 13 '05 #1
5 1801
milton wrote:

**** Post for FREE via your newsreader at post.usenet.com ****

Compiled by Turboc 2.0,PC with win2000

main()
{
int a=32767;
long b=a+1;
printf("%d,%ld" ,a,b);
}

that output a and b with 32767,-32768.why overflow?


/*
** int plus int (a + 1), equals int.
** int plus long (a + 1L), equals long.
*/

/* BEGIN new.c */

#include <stdio.h>
#include <limits.h>

int main(void)
{
short a = SHRT_MAX;
long b;

b = a + 1L;
printf("%d, %ld\n", a, b);

b = (long)a + 1;
printf("%d, %ld\n", a, b);

return 0;
}

/* END new.c */

--
pete
Nov 13 '05 #2
"milton" <py*@mail.zjitc .net> wrote in message news:<3f******@ post.usenet.com >...
**** Post for FREE via your newsreader at post.usenet.com ****

Compiled by Turboc 2.0,PC with win2000

main()
{
int a=32767;
long b=a+1;
printf("%d,%ld" ,a,b);
}

that output a and b with 32767,-32768.why overflow?


Try to run this program on your different platforms:

#include <limits.h>
#include <stdio.h>

int main(void)
{
printf("char: %d\n", (int)CHAR_MAX);
printf("short: %d\n", (int)SHRT_MAX);
printf("int: %d\n", INT_MAX);
printf("long: %ld\n", LONG_MAX);
return 0;
}
Nov 13 '05 #3
pete <pf*****@mindsp ring.com> writes:
milton wrote:

**** Post for FREE via your newsreader at post.usenet.com ****

Compiled by Turboc 2.0,PC with win2000

main()
{
int a=32767;
long b=a+1;
printf("%d,%ld" ,a,b);
}

that output a and b with 32767,-32768.why overflow?


/*
** int plus int (a + 1), equals int.
** int plus long (a + 1L), equals long.
*/

[...]

More generally, the type of an expression (even if it's a
subexpression) is determined by the types of its operands; the context
in which it appears has no effect. Assuming that the type is affected
by the context seems to be a common newbie error (no offense to the OP
intended). For example, few programmers are surprised that

int n = 16 / 5;

performs an integer division and assigns the value 3 to n, but I've
seen people become deeply offended that

float f = 1 / 3;

sets f to 0.0 (because the conversion happens *after* the division).

--
Keith Thompson (The_Other_Keit h) ks*@cts.com <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
Nov 13 '05 #4
Keith Thompson <ks*@cts.com> wrote in message news:<lz******* *****@cts.com>. ..
pete <pf*****@mindsp ring.com> writes:
milton wrote:

**** Post for FREE via your newsreader at post.usenet.com ****

Compiled by Turboc 2.0,PC with win2000

main()
{
int a=32767;
long b=a+1;
printf("%d,%ld" ,a,b);
}

that output a and b with 32767,-32768.why overflow?


/*
** int plus int (a + 1), equals int.
** int plus long (a + 1L), equals long.
*/

[...]

More generally, the type of an expression (even if it's a
subexpression) is determined by the types of its operands; the context
in which it appears has no effect.


There can be subtleties like integer promotion. The type of an
expression passed to sizeof can be different to the promoted type of
the same expression passed as an argument to printf().

--
Peter
Nov 13 '05 #5
ni***@passagen. se (Niklas Norrthon) wrote in message news:<d1******* *************** ****@posting.go ogle.com>...
....
printf("short: %d\n", (int)SHRT_MAX);


The cast is redundant.

--
Peter
Nov 13 '05 #6

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

Similar topics

15
4827
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 miss something and width of paragraph can be set less than its content (without overflow specification)? Document is here: http://terrainformatica.com/w3/p2/problem1.htm
6
19405
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 user resizes the frame to be "too narrow". Right now, the source for that frame looks like this: <table border="1" width="100%"> <tr style="background-color:#C0C0C0">
4
3551
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)) // overflow else // not overflow
19
3136
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 (nested), three list views, one tree control with up to 30,000 nodes, maybe 15 comboboxes (half of which have a large recordset as rowsource), 20 or so buttons and around 30 text boxes (not to mention the images, labels, etc and around 1000 lines...
27
4544
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 efficient as possible without resorting to assembly language, and without causing undefined behavior. That, of course, means catching the overflow before it happens. What I asked was (stripping any relevance to C++):
7
2419
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
6250
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 something;
8
10665
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 integer scope smaller than MAXINT,so I want to seek a measure to test the excess without the value's comparing to MAXINT. Please let me know if you have a good idea. THX.
2
2751
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 style="overflow:hidden;width: 355px;">Header1</div></td> <td><div style="overflow:hidden;width: 100px;">Header2</div></td> <td><div style="width: 100%;overflow:hidden;">Header3</div></td> </tr>
42
7013
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 type 'long' is used. That way native C speed can be reached. Now I want to experiment with raising a Seed7 exception (which is emulated with setjmp(), longjmp() in C) for integer
0
8683
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
8902
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8873
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7740
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4372
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4623
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3052
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2339
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.