EvilZone

Programming and Scripting => C - C++ => Topic started by: Lionofgod on June 15, 2012, 12:57:21 AM

Title: [C] Find Out How Long an Int is!!!
Post by: Lionofgod on June 15, 2012, 12:57:21 AM
I was bored and had nothing to do... made a program to print constants in limits.h as well as float.h, those constants describe max and min values of a type. Apparently every computer has different values. Try it out : )
Some results are pretty weird :S
Code: (c) [Select]
#include <limits.h>
#include <stdio.h>
#include <float.h>
main(){

printf("A char is %d bits long.\n", CHAR_BIT);
printf("It has a max value of %d and minimum value of %d.\n", SCHAR_MAX,
SCHAR_MIN);
printf("An Unsigned char has a maximum value of %d.\n\n", UCHAR_MAX);
printf("A short int has a max value of %d and a minimum value of %d.\n"
"An unsigned short int has a max value of %d.\n\n",SHRT_MAX,SHRT_MIN,USHRT_MAX);
printf("An int has a max value of %d and a minimum value of %d.\n"
"An unsigned int has a maximum value of %d.\n\n",INT_MAX,INT_MIN,UINT_MAX);
printf("A long int has a max value of %ld and a minimum value of %ld.\n"
"An unsigned long int has a max value of %ld.\n\n", LONG_MAX,LONG_MIN,ULONG_MAX);
printf("A float is as precise as %d decimal spots.\nIt has a max value of %g and a minimum"
" value of %g.\n\n",FLT_DIG,FLT_MAX,FLT_MIN);
printf("A double is as precise as %d decimal spots.\nIt has a max value of %g and a minimum"
" value of %g.\n\n",DBL_DIG,DBL_MAX,DBL_MIN);
printf("A long double is as precise as %d decimal spots.\nIt has a max value of %Le and a"
" minimum value of %Le.\n",LDBL_DIG,LDBL_MAX,LDBL_MIN);
}

My results:
Code: [Select]
A char is 8 bits long.
It has a max value of 127 and minimum value of -128.
An Unsigned char has a maximum value of 255.

A short int has a max value of 32767 and a minimum value of -32768.
An unsigned short int has a max value of 65535.

An int has a max value of 2147483647 and a minimum value of -2147483648.
An unsigned int has a maximum value of -1.

A long int has a max value of 2147483647 and a minimum value of -2147483648.
An unsigned long int has a max value of -1.

A float is as precise as 6 decimal spots.
It has a max value of 3.40282e+38 and a minimum value of 1.17549e-38.

A double is as precise as 15 decimal spots.
It has a max value of 1.79769e+308 and a minimum value of 2.22507e-308.

A long double is as precise as 18 decimal spots.
It has a max value of 1.189731e+4932 and a minimum value of 3.362103e-4932.
An int is actually a long int 0.o
An unsigned long int is as large as -1 ???
Couldnt make sense of floats, tried a lot of different placeholders.
Title: Re: [C] Find Out How Long an Int is!!!
Post by: s3my0n on June 15, 2012, 07:02:10 AM
Do printf("%u\n", (unsigned int) n) on unsigned integers.
Title: Re: [C] Find Out How Long an Int is!!!
Post by: Lionofgod on June 22, 2012, 12:49:31 AM
That worked, I got a reasonable value for long ints
Thanks
Title: Re: [C] Find Out How Long an Int is!!!
Post by: pllaybuoy on July 02, 2012, 11:40:38 AM
You should've mentioned the LOGIC behind it , here let me tell
sizeof is a manipulator used with cout object of ostream class of iostream library , what this manipulator does is find the size of something in BYTES
in c++ a byte is not always 8 bit , it can be 4,8 or may vary .
so to find the byte size(width) of something it will be
cout<<sizeof (int);
or for a variable
cout<<sizeof variable_1;
the jobs is not done next you have to convert the bytes in bits
to do this we use
CHAR_BIT which is defined in the climits header file so you gotta include it to
#include <climits>
so you'll have to covert the bytes in bit using CHAR_BIT definition
Title: Re: [C] Find Out How Long an Int is!!!
Post by: Frankenstinejoe on July 24, 2012, 12:37:17 PM
Mate pllaybuoy is right about sizeof() function its simpler and neat as compared to others .