tags:

views:

127

answers:

4
+1  Q: 

c data type size

How i can know the size of all data type in my computer?

A: 

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

Larry Wang
The correct types are `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.
R..
You're right. Careless mistake on my part. Fixed that and added the appropriate `#include`.
Larry Wang
+5  A: 

You can apply sizeof to each type whose size you need to know and then you can print the result.

James McNellis
I try with printf("%lu\n",sizeof(double)); printf("%lu\n",sizeof(int)); printf("%lu\n",sizeof(long int)); printf("%lu\n",sizeof(long long int)); and get 8488 why int and long int have same size ?
JuanPablo
@JuanPablo: You mean 8 4 4 8, right? `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`.
James McNellis
@James - Just for correctness, it says `short` and `int` must be at least 2 bytes and `long` must be at least 4 bytes, and that `short` < `int` < `long`
Chris Lutz
@James McNellis: That's not true, in C99 5.2.4.2.1, the smallest allowable maximum for `int` 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`.
dreamlax
@JuanPablo: `%lu` is not a valid format specifier for printing numbers of type `size_t`. You need to either cast the result of `sizeof` to `long` or use the `%zu` format specifier.
R..
@dreamlax: Sorry, I wasn't very clear, was I? I only meant that `int` and `long` may be of the same size (in which case, `int` would need to be at least 32-bits in size).
James McNellis
@JuanPablo: depending on the platform, `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.
John Bode
@John Bode: Argh. Obviously, the above comment should read "16 *bits* wide", not 16 bytes. Either too much or not enough caffeine.
John Bode
+1  A: 

sizeof(T) will give you the size of any type passed to it. If you're trying to find out the size of all data types used or defined in a particular program, you won't be able to--C doesn't maintain that level of information when compiling.

Jonathan Grynspan
+1 for possibly successful effort at reading the OP's mind.
Steven Sudit
+4  A: 

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));
}

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.

Clearer
I try this, but get int same size of long long int, why ?
JuanPablo
@JuanPablo - because they are the same size on whatever machine you are running this code.
detly
I have lots of equal sized types, but that's not really the point. The types are not solely dependant on their sizes.On my machine the program tells me the following:sizeof(char) = 1sizeof(short) = 2sizeof(int) = 4sizeof(long) = 8sizeof(long long) = 8sizeof(float) = 4sizeof(double) = 8sizeof(long double) = 16
Clearer
C does not allow 7-bit bytes. `CHAR_BIT` is required to be at least 8. POSIX requires `CHAR_BIT` to be exactly 8.
R..
Btw, instead of `<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.
Amarghosh
@R.. __7-bit bytes__ :)
Amarghosh
@Clearer: I formatted your code block to demonstrate what @Amarghosh explained; if you click the "edit" link under your post you can see what the end result looks like and how to format code in the future. :-)
James McNellis
Clearer