A data type specifies the type of value that we use in our programs. A data type is generally specified when declaring variables, arrays, functions etc.
In ANSI C, the data types are divided into three categories. They are:
1) Primitive or Fundamental data types
2) User-defined data types
3) Derived data types
Primitive or Fundamental data types
The primitive data types in ANSI C are as shown in the below diagram:
The most fundamental data types that every C compiler supports are: int, char, float and double. Other compilers support the extended versions of these fundamental data types like: short int, long int, long double etc.
The signed data types are used for storing both positive and negative values where as the unsigned data types are used to store only positive values. The memory capacity of these data types are based on the hardware.
The memory capacity of all the fundamental data types on a 16-bit machine is as shown below:
User-Defined data types
ANSI C allows the users to define identifiers as their own data types, based on the already existing primitive or fundamental data types. This concept is known as “type definition” and the data types thus created are known as user-defined data types.
We can create user-defined data types in two ways:
1) By using the “typedef” keyword
2) By using the “enum” keyword
The “typedef” keyword can be used to declare an identifier as a user-defined data type. The syntax for using typedef is as shown below:
In the above example, rollno and average are new user-defined data types. Now we can use rollno and average as data types as shown below:
The “enum” is used to declare identifiers as user-defined data types. Such data types are also called as enumerations. The “enum” keyword can be used to declare an identifier as data type which can store a limited set of integer values.
The syntax of using “enum” keyword is as shown below:
Example usage of enum is as shown below:
In the above example, “days” is a new user-defined type. All the variables created using the “days” data type can only contain values in the range Mon to Sat as shown in the above example.
We can declare variables by using the “days” data type as shown below:
If we display “day1” in the printf() statement, it will display it as zero. Generally, the value of a first element in an enumeration always starts with zero and the next element’s value is previous element’s value plus 1. So, the value of “Tue” will be one and so on.
Derived data types
The data types which are created using the already existing primitive or fundamental types are known as derived data types. Like user-defined data types we cannot declare new variables using the derived data types.
Examples of derived data types in C are: arrays, functions, structures, unions and pointers.