How i can know the size of all data type in my computer?
4 Answers
The following program should do the trick for the primitive types:
#include <stdio.h>
int main()
{
printf("sizeof(char) = %d\n", sizeof(char));
printf("sizeof(short) = %d\n", sizeof(short));
printf("sizeof(int) = %d\n", sizeof(int));
printf("sizeof(long) = %d\n", sizeof(long));
printf("sizeof(long long) = %d\n", sizeof(long long));
printf("sizeof(float) = %d\n", sizeof(float));
printf("sizeof(double) = %d\n", sizeof(double));
printf("sizeof(long double) = %d\n", sizeof(long double));
return 0;
}
This prints the number of "bytes" the type uses, with sizeof(char) == 1 by definition. Just what 1 means (that is how many bits that is) is implementation specific and likely depend on the underlying hardware. Some machines have 7 bit bytes for instance, some have 10 or 12 bit bytes.
7 Comments
CHAR_BIT is required to be at least 8. POSIX requires CHAR_BIT to be exactly 8.<code> and <br> tags and > entities, next time you can just type the code, select it and press ctrl-k to format it (or click on the code icon (with ones and zeros) on the edit toolbar above the text area). Basically, if the code block is padded with one blank line and indented by 4 spaces, it will appear formatted.You can apply sizeof to each type whose size you need to know and then you can print the result.
8 Comments
int and long may have the same size. There's no rule saying that long must be able to represent a greater range of values than int, only that it must be able to represent at least the range representable by int.short and int must be at least 2 bytes and long must be at least 4 bytes, and that short < int < longint is 32767, but the smallest allowable maximum for long is 2147483647. int is allowed to be as large as long, but that doesn't mean long is allowed to be as small as int.int and long may be of the same size (in which case, int would need to be at least 32-bits in size).int may be the same size as either long or short. int must be at least 16 bytes wide (it must be able to represent the range [-32767,32767] at minimum), but may be (and often is) wider.Use sizeof to get the size of the type of variable (measured in bytes).
For example:
#include <stdint.h>
sizeof(int32_t) will return 4
sizeof(char) will return 1
int64_t a;
sizeof a; will return 8
See http://publications.gbdirect.co.uk/c_book/chapter5/sizeof_and_malloc.html
2 Comments
int32_t and int64_t not int32 and int64. -1 for using some weird system-specific types in an example instead of the standard ones.#include.
sizeof Xshould return the size of a typeX.X. IfXis a type, you should usesizeof(X).