Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c data type size

Tags:

c

types

size

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

like image 919
JuanPablo Avatar asked Jul 12 '10 02:07

JuanPablo


2 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.

like image 107
Clearer Avatar answered Oct 05 '22 13:10

Clearer


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

like image 32
James McNellis Avatar answered Oct 05 '22 14:10

James McNellis