Friday, July 29, 2016

C Basics - Storage of Data Types in C

The basic data types fall into general categories. The "integral types" include char, int, short, long, signed, unsigned, and enum. The "floating types" include float, double, and long double. The "arithmetic types" include all floating and integral types.
ANSI has the following rules:

short int <=    int <= long int
float <= double <= long double

What this means is that a 'short int' should assign less than or the same amount of storage as an 'int' and the 'int' should be less or the same bytes than a 'long int'.

Data type Size (byte) Range
char 1 -128 to +127
signed char 1 -128 to +127
unsigned char 1 0 to 255
short 2 -32,768 to +32,767
signed short 2 -32,768 to +32,767
unsigned short 2 0 to 65,535
int 2 -32,768 to +32,767
signed int 2 -32,768 to +32,767
unsigned int 2 0 to 65,535
long 4 -2,147,483,648 to +2,147,483,647
signed long 4 -2,147,483,648 to +2,147,483,647
unsigned long 4 0 to 4,294,967,295
float 4 3.4E-38 to 3.4E+38 with 6 digits of precision
double 8 1.7E-308 to 1.7E+308 with 10 digits of precision
long double 8 3.4E-4932 to 1.1E+4932 with 10 digits of precision
Note: The size and range are compiler dependent.

The maximum value a variable can hold depends upon the number of bytes it occupies in memory. By default all the variables are signed. We can declare a variable as unsigned to accommodate greater value without increasing the bytes occupied.

Structure members are stored sequentially in the order in which they are declared: the first member has the lowest memory address and the last member the highest.

Every data object has an alignment-requirement. For structures, the requirement is the largest of its members. Every object is allocated an offset so that

offset % alignment-requirement == 0

You may want to store structures more or less compactly. The preprocessor directive #pragma pack control how structure data is "packed" into memory.

The storage associated with a union variable is the storage required for the largest member of the union. When a smaller member is stored, the union variable can contain unused memory space. All members are stored in the same memory space and start at the same address. The stored value is overwritten each time a value is assigned to a different member.

The storage associated with an array type is the storage required for all of its elements. The elements of an array are stored in contiguous and increasing memory locations, from the first element to the last.

The amount of storage required for an address and the meaning of the address depend on the implementation of the compiler. Pointers to different types are not guaranteed to have the same length. Therefore, sizeof(char *) is not necessarily equal to sizeof(int *).

Related topics:
Memory Management in C   |   Incomplete Type in C   |   Lifetime, Scope, Visibility and Linkage in C   |   Namespace in C   |   Complex and Abstract Declarations in C   |   Standard Library in C

List of topics: C Programming

No comments:

Post a Comment