I want to play with overflow of signed short integer variable. I declare variable a1 as short, and then I give the greater positive value, zero is 'considered' positive, so maximum value of a signed integer (be it short, int, long or long long) must be exp2(8*sizeof(variable)-1)-1, mustn't it?
#include<stdio.h>
#include <math.h>
int main()
{
int short a1=2;
a1=exp2(8*sizeof(a1)-1)-1;
printf("The last value that DOES NOT overflow in a integer as \'signed short\' (%i bytes) is %hi.\nIf I define this variable equal to this value I get the value in the variable %hi.\n",(unsigned char) sizeof(a1), (short) exp2(8*sizeof(a1)-1)-1, a1);/*key word short from "(short) exp2(8*sizeof(a1)-1)-1"*/
a1=exp2(8*sizeof(a1)-1);/*warning-overflow: "warning: overflow in implicit constant conversion [-Woverflow]"*/
printf("The 1st value that overflows in a integer as \'signed short\' (%i bytes) is %i.\nIf I define this variable equal to this value instead I get the value in the variable %i.\n",(unsigned char) sizeof(a1), (int) exp2(8*sizeof(a1)-1), a1);/*key word int from "(int) exp2(8*sizeof(a1)-1)"*/
return;
}
So I get a overflow-warning, as I wanted, that's the target of this code:
warning: overflow in implicit constant conversion [-Woverflow]
Then ./a.out and the output is
The last value that DOES NOT overflow in a integer as 'signed short' (2 bytes) is 32766. If I define this variable equal to this value I get the value in the variable 32767. The 1st value that overflows in a integer as 'signed short' (2 bytes) is 32768. If I define this variable equal to this value instead I get the value in the variable 32767.
The 2nd printf works fine, doesnt it? But the 1st I think should show the same value to printf of variable a1 (a1=exp2(8*sizeof(a1)-1)-1;) and to the casting of (short) exp2(8*sizeof(a1)-1)-1. I rewrite it more clear:
a1=exp2(8*sizeof(a1)-1)-1;
printf("It should be %hi = %hi.\n",(short) exp2(8*sizeof(a1)-1)-1, a1);
Help me to understand it please
Aucun commentaire:
Enregistrer un commentaire