i have structure variable health can accessed disk->health, , of type double. trying print double , convert int , print again.
printf("the disk health in loop is: %lf\n", disk_ptr->health); //result is: 13230000014.560297 printf("the disk health in loop is: %i\n", (int) floor(disk_ptr->health)); //result is: -2147483648 printf("the disk health in loop is: %d\n", (int) floor(disk_ptr->health)); //result is: -2147483648 the code above using. want second result output: 13230000014, instead giving me strange negative number.
you said want print 13230000014, value greater maximum value int can hold, 2^31 - 1 (on systems). need stop using %i specifier , switch bigger specifier, %lld, , cast long long instead.
like this:
printf("the disk health in loop is: %lld\n", (long long) floor(disk_ptr->health)); note have change both casting format specifier, or printf still treat argument int.
if want more explicit using 64 bit integer, can consider including stdint.h, , making cast int64_t instead of long long.
edit: note floor call redundant in case, casting long long automatically truncate decimal. left in above example better match code, know isn't necessary.
Comments
Post a Comment