C Data types

The Datatype defines the type of data a variable can store such as integer, float, character etc. Every variable which is used in the program must be declared as what data-type it is. C language supports three different type of data types :

1. Primary data types
2. Derived data types
3. User-Defined data types

1. Primary data types :

The primary data types are fundamental data types in c, which are integer(int), character (char), floating point (float), double-precision floating point (float) and void.

Integer :

The integer data type allow a variable to store numeric variables. The 'int' keyword is used to refer integer data type. Generally the size of integer is 2 byte, 4 bytes or 8 bytes depends on the computer. If the int size if 2 byte (16 bit word length) then its value is limited to the range -32768 to +32767 (2^16)for sign and 65536 (2^16) for unsigned. Similarly a 4 byte word (32 bit in word length) can store an integer ranging from -2,147,483,648 to 2,147,483,647. In order to provide some control over the range of numbers and storage spaces, C has three classes of integer storage, namely short int, int and long int both signed and unsigned forms.

Type Storage Size Value Range
int 4 bytes -2,147,483,648 to 2,147,483,647
unsigned int 4 bytes 0 to 4,294,967,295
short int 2 bytes -32,768 to 32,767
unsigned short int 2 bytes 0 to 65,535
long int 8 bytes - 9.223372e+18 to 9.223372e+18
unsigned long int 8 bytes 1.8446744e+19

Floating point :

Floating point or real numbers are stored in 32 bits with 6 digit of precision. Floating point numbers are defined in C by the keyword float. And when the accuracy provided by a float number is not sufficient, the type double can be to define the number. A double data type number uses 64 bits giving a precision of 14 digits. These are known as double precision numbers.

Type Storage Size Value Range
float 4 bytes 3.4E-38 to 3.4E+38
double 8 bytes 1.7E-308 to 1.7E+308
long double 10 bytes 3.4E-4932 to 1.1E+4932

Character Types :

A single character can be defined as a character (char) type data. Characters are usually stored in 8 bits (one byte) of internal storage. The qualifier signed or unsigned may be explicitly applied to char. While unsigned chars have values between 0 and 255, signed chars have values from -128 to 127.

Type Storage Size Value Range
char 1 byte -128 to 127
unsigned char 1 byte 0 to 255

void type :

The void type has no values. This is usually used to specify the type of functions. The type of function is said to be void when it does not return to the calling function. It can also play the role of a generic type, means that it can be represent any of the other standard types.

2. Derived data types

Derived data types are nothing but primary datatypes but a little twisted or grouped together like array, pointer, reference etc.

Arrays : Arrays are sequences of data items having homogeneous values. They have adjacent memory locations to store values.

Pointers : Pointers are powerful C features which are used to access the memory and deal with their addresses.

References : Function pointers allow referencing functions with a particular signature.

3. User-Defined data types

C allows the feature called type definition which allows programmers to define their identifier that would represent an existing data type. There are three such types :

Structure : It is a package of variables of different types under a single name. This is done to handle data efficiently. "struct" keyword is used to define a structure.

Union : These allow storing various data types in the same memory location. Programmers can define a union with different members, but only a single member can contain a value at given time.

Enum : Enumeration is a special data type that consists of integral constants, and each of them is assigned with a specific name. "enum" keyword is used to define the enumerated data type.

Next Topic :