JuanPablo
JuanPablo

Reputation: 24794

c data type size

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

Upvotes: 7

Views: 11814

Answers (4)

Clearer
Clearer

Reputation: 2306

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.

Upvotes: 8

Larry Wang
Larry Wang

Reputation: 1006

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

Upvotes: 0

Jonathan Grynspan
Jonathan Grynspan

Reputation: 43472

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.

Upvotes: 2

James McNellis
James McNellis

Reputation: 355297

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

Upvotes: 6

Related Questions