-
Number of bits in types
How correct is this?
char: 8 bit
short: 16 bit
long: 32 bit
long long: 64 bit
int: depending on your system (usually 32 bit today)
float: 32 bit (or depending on your system?)
double: 64 bit
This is only something I believe, but don't know. Is it the same in C++ as in C?
-
There are exactly CHAR_BIT bits in a byte/char, there are at least 8.
There are exactly sizeof(type) bytes in a type. [edit]Though whether or not there are padding bits is another matter.[/edit]
This is the same for both C and C++.
Everything else you showed may or may not be true -- other than besides the int, those will be the minimums. [edit]And I don't know that the larger floating types are required to be "bigger" or not.[/edit]
-
Investigate limits.h -- it shows you some information like CHARBITS (8 bits per char)
-
This how you would check the memory information of your system. It depends really on you machine. But the following shows the information which was taken out from my machine
Code:
#include<stdio.h>
int main()
{
printf("These are the memory size information on my system\n");
printf("\n Int : %d bits\n", sizeof(int) * 8);
printf(" Floart : %d bits\n", sizeof(float) * 8 );
printf(" Char : %d bits\n", sizeof(char) * 8);
printf(" Double : %d bits\n", sizeof(double) * 8);
printf(" Short : %d bits\n", sizeof(short) * 8);
printf(" Long : %d bits\n", sizeof(long) * 8);
getchar();
return 0;
}
/* my output
These are the memory size information on my system
Int : 32 bits
Floart : 32 bits
Char : 8 bits
Double : 64 bits
Short : 16 bits
Long : 32 bits
*/
ssharish2005
-
> printf("\n Int : %d bits\n", sizeof(int) * 8);
Read the other posts, 8 is an assumption.
Use CHAR_BIT
-
ohh sorry, thought that normally it would be 8. dint look at the Daves post. Updated code
Code:
#include<stdio.h>
#include<limits.h>
int main()
{
printf("These are the memory size information on my system\n");
printf("\n Int : %d bits\n", sizeof(int) * CHAR_BIT);
printf(" Floart : %d bits\n", sizeof(float) * CHAR_BIT);
printf(" Char : %d bits\n", sizeof(char) * CHAR_BIT);
printf(" Double : %d bits\n", sizeof(double) * CHAR_BIT);
printf(" Short : %d bits\n", sizeof(short) * CHAR_BIT);
printf(" Long : %d bits\n", sizeof(long) * CHAR_BIT);
getchar();
return 0;
}
Thanks Salem
ssharish2005
-
For C89, the most portable way to handle size_t results in printf is to cast to unsigned long and use %lu. And again, I think only the char types are guaranteed to use all the bits as value bits.
-
Thanks everyone. So a byte isn't necessary 8 bits, that's something new.