C -printf casting -


i want play overflow of signed short integer variable. declare variable a1 short, , give greater positive value, 0 'considered' positive, maximum value of signed integer (be short, int, long or long long) must 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 not overflow in integer \'signed short\' (%i bytes) %hi.\nif define variable equal value value in variable %hi.\n",(unsigned char) sizeof(a1), (short) exp2(8*sizeof(a1)-1)-1, a1);/*key word short "(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 overflows in integer \'signed short\' (%i bytes) %i.\nif define variable equal value instead value in variable %i.\n",(unsigned char) sizeof(a1), (int) exp2(8*sizeof(a1)-1), a1);/*key word int "(int) exp2(8*sizeof(a1)-1)"*/ return; } 

so overflow-warning, wanted, that's target of code:

warning: overflow in implicit constant conversion [-woverflow]

then ./a.out , output is

the last value not overflow in integer 'signed short' (2 bytes) 32766. if define variable equal value value in variable 32767. 1st value overflows in integer 'signed short' (2 bytes) 32768. if define variable equal value instead value in variable 32767.

the 2nd printf works fine, doesnt it? 1st think should show same value printf of variable a1 (a1=exp2(8*sizeof(a1)-1)-1;) , casting of (short) exp2(8*sizeof(a1)-1)-1. rewrite more clear:

#include<stdio.h> #include <math.h> #include <limits.h>  int main() { int short a1=exp2(char_bit*sizeof(a1)-1)-1; printf("it should %hi = %hi.\n",(short) exp2(char_bit*sizeof(a1)-1)-1, a1); return; } 

and output is

it should 32766 = 32767.

when think should be: "it should 32767 = 32767."

help me understand please

solved @chux

#include<stdio.h> #include <math.h> #include <limits.h> #include <float.h>   int main() { int short a1=exp2(char_bit*sizeof(a1)-1)-1; printf("it should %hi = %hi.\n",(short) (round(exp2(char_bit*sizeof(a1)-1))-1), a1); return; } 

i must remark brakets here important, without them, mean (short) round(exp2(char_bit*sizeof(a1)-1))-1, value.

double int truncation.

the unposted non-standard function exp2() double exp2(double x) , not implemented.

when converting small int double in passing argument exp2(8*sizeof(a1)-1, conversion exact.

when taking result , converting short in short a1=exp2(), that issue. suppose result of exp2(15) in error , 32767.99999999 instead of hoped 32768.0. conversion to int "truncation toward 0".

often solution round before truncation.

// add  vvvvvv                           v  (short) round(exp2(char_bit*sizeof(a1)-1)) - 1 

try debugging following see return value enough precision.

#include <float.h> int short a1; printf("%.*f\n", dbl_decimal_dig - 1, (double) exp2(char_bit*sizeof(a1)-1)); 

Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -