In C programming, variables must be declared before it is used. The C data types are used to declare these variables. The C data type specify how many bytes to be allocated to a variable. Specifying the data type of a variable depends on the type of operation to be carried out on this variable. Each data type in C programming allocates different memory size to variable and each data types have different capabilities.
The data types in c programming can be categorised as follows-
|C basic data type||These are basic data types and further categorised into three type- 1. intege type 2. floating point type 3. character type|
|C derived data type||These are some complex data type that can be declare with the help of C basic data type. These can be categorised as- 1. Array type 2. Structure type 3. Union type 4. Function type 5. Pointer type 6. Enumeration type|
|C void data type||The void data type specifies that no value is available.|
C Basic Data Type
integer type in C
A integer data type is used to store a whole number. It can not hold a decimal number. A variable can be declared as integer type using the keyword int. For example:
There are also some other integer data types available in C language to hold different range of numbers. These with there range are as follows-
|Type||Storage size||Value range|
|int||2 or 4 bytes||-32,768 to 32,767 or -2,147,483,648 to 2,147,483,647|
|unsigned int||2 or 4 bytes||0 to 65,535 or 0 to 4,294,967,295|
|short||2 bytes||-32,768 to 32,767|
|unsigned short||2 bytes||0 to 65,535|
|long||4 bytes||-2,147,483,648 to 2,147,483,647|
|unsigned long||4 bytes||0 to 4,294,967,295|
floating point type in C
Floating point data type in C programming is used to store real number, that means it can store decimal numbers as well as whole numbers. A floating point variable can be declare with the help of keyword float. For example:
There are also different types of floating point data types in C language with different range of numbers and size. These are-
|Type||Storage size||Value range||Precision|
|float||4 byte||1.2E-38 to 3.4E+38||6 decimal places|
|double||8 byte||2.3E-308 to 1.7E+308||15 decimal places|
|long double||10 byte||3.4E-4932 to 1.1E+4932||19 decimal places|
character type in C
Characters are the special symbols that are supported in C language. The character data types are used in our program to store these special symbol. A character data type can store only one character at a time. A variable can be declare as a character type using the keyword char. For example:
When a character is stored in a variable, it must be written between single quotes as above example. As there is only one character can be store using character data type, there is an extensive version of character data type called string, which can store more than one character at the same time.
Below are the byte width and range of different character types.
|Type||Storage size||Value range|
|char||1 byte||-128 to 255|
|unsigned char||1 byte||0 to 255|
|signed char||1 byte||-128 to 127|
C Derived Data Types
Array in C
An array is a collection of variables which are of similar data type and each element of an array is somehow related with each other. Syntax for declaring an array is data_type array_name[array_size];
Using the above array we can store maximum 10 integer numbers in the array arr. The size of the array can be calculated by multiplying the byte width of the array data type with the size of the array.
Structure in C
Structure in C is similar to array but it can store different kind of variable with different data types together which are interrelated. The size of an structure is the sum of byte of all individual variable declared inside the structure. A structure can be declare using the keyword struct. For example:
The size of the above structure student_details is 8 byte, 4 byte for roll_no and 4 byte for marks.
Union in C
Union in C is similar to the structure, but it can hold value for a single member at a time. A union can be declare using the keyword union. The size of the biggest member within the union is also the size of that particular union.
The size of the union in the above example is 4 byte.
Function in C
The function in C programming is a group of statements that together perform a task. Each C program must have at least one function that is the main function or main().
Pointer in C
A pointer is special type of variable which can store the address or memory location of an another variable. A pointer can be define using the asterisk or symbolically *. Syntax for defining a pointer is data_type *var-name;
In above example, the pointer ptr can store address of integer type variables only as the pointer is of integer type.
Enumeration in C
Enumeration is a special data type that consists of integral constants, and each of them is assigned with a specific name. The enumerated data type can be define using the keyword enum.
The default value for constant1 is 0, constant2 is 1 and so on. We can change these default values by following method.
C void type
The void data type specifies that no value is available. In C programming, void type is used in three different situations. These are-
1. Function return no value
In our program, there may be many functions which do not return any value or in other way we can say that return a void. These types of functions are declared with return type void. For example-
2. Function with no argument
In our program, there may be many many functions which do not have any argument. So, these functions can not accept any parameter. These type of function can accept a void type. For example:
3. Pointers to void
When a pointer variable is declared as a void pointer using the keyword void, the pointer becomes a general purpose pointer variable. Address of any variable of any data type (char, int, float etc.) can be assigned to this void pointer variable. For example:
Check Size Of a Data Type through Compiler
To check the size of any type of data in C programming, we can use the sizeof() function. It returns the size of the data type or variable in byte. For example:
The output of the above program as follows-