why overflow?

M

milton

**** 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
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
 
P

pete

milton said:
**** 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 */
 
N

Niklas Norrthon

milton said:
**** 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;
}
 
K

Keith Thompson

pete said:
/*
** 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).
 
P

Peter Nilsson

Keith Thompson said:
pete said:
/*
** 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().
 

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

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top