Data categories define how and what kind of information we could incorporate throughout our applications. The C programming language comes with a pre-specified collection of data types that are used to deal with different types of information that we’re using in our software. These data types correspond to a comprehensive system to specify variables and methods of various kinds. The type of such a variable decides what more storage capacity it takes up as well as how the preserved bit pattern would be translated. The memory requirements of such data types vary. Each data type has its own set of operations that can be applied to it.
We have 4 Data types available in the C programming language:
- Basic Data Type: This data type has been used to denote Integer as well as floating-point that are the most common data types. The storage capacity of simple data types fluctuates depending on whether the operating system is 32-bit or 64-bit. For example, int, char, float, and double.
- Enumerated Data Type: They’re arithmetic kinds anyway, and they’re used in the application to describe variables that would only delegate discrete numerical value. The legibility of a program is improved using enumeration data types. For example, enum.
- Void Data Type: The Data Type “Void” denotes the absence of a value. It is simply a null data type that is cast-off as a method return type, e.g., void.
- Derived Data Type: Derived types represent the data types that have been created from basic data types such as an array, structure, union, and pointers.
Let’s have a clear look at some data types with examples to understand.
Basic Data Types
There are a lot of basic data types, e.g., integer, char, float, short, and long. The Integer or “int” data type can have any positive, negative, and zero numerical value except floating or decimal value.
Let’s have a look at the data type syntax and some examples to elaborate on it. You can declare the integer data type by simply specifying the “int” keyword before the variable, as shown in the presented snapshot. This variable could be any alphabet and any word. You can also add some value to the integer variable.
You can also define two variables at the same line if they are of the same data type.
Let’s use some integer data type variables in our C language code to see how it works.
So, create a file “one.c” in the home directory using the “nano” query. You can create this file using the terminal.
Type the presented-below image code in it. This C language script has three integer variables specified. After that, the variables have been printed out.
After saving and closing the file, you have to compile the above script using the “gcc” compiler command. If you don’t have a “gcc” compiler in your Linux system, try to install it first.
Now, let’s run the “one.c” file using the “a.out” query. The output will present the values of all the three variables in sequence concerning the printf statement in the code.
Now, we will be looking at the sizes of different data type variables. So, open the same file using the “nano” instruction.
Add the below-shown code in the file. The code has 4 different data type variables specified and four print statements. The print statements have been using the function of “sizeof()” to fetch the size of all the variables separately. Save the code and leave the file.
Again compile the “one.c” file with the “gcc” compile command.
By running the “one.c” file, we have found the size of all the variables, e.g., integer, character, short, and long.
Enumerate Data Type
The most common example for “enumerate” data types are months and days. We will be looking at the “days” example. Open the same “one.c” file.
Add the below C script in the file. This code contains enumerate data type variable “DAYS” with enumerate list. The list contains some names of days. Another enum “week” contains only one name, “Sunday”. Then the “if” statement has been used to perform a check if the value of “week” is “Monday” or “Sunday”. It prints out the message according to condition.
Compilation of the code has been performed using the “gcc” compiler command.
Running this file shows the message “It’s weekend” according to the correct situation.
Void Data Type
Void data type means “nothing” has been returned or will be returned. This could be used before any variable or any method. We will be having an example while using the “void” data type before the function definition. So, we have opened the file “one.c”.
We have used the following code in the file. This code has a main function with a “void” return type. The main function contains some integer-type variables and “print” statements.
Again, the compilation using the “gcc” query.
Execution of file has been showing the messages and variables without returning any value.
Derived Data Type
There are different derived data types available in the C language. These are formed using these simple data types. These derived data types are arrays, structures, pointers, and unions. So, let’s have an example of array data type. Open the same file.
Add the below code in the nano file. The code contains one integer type array “Numbers”. The next line has been calculating the size of the array. The “for” loop has been used to enter the values in the array by a user.
On execution, it asks for values from the user. Upon entering 5 values the program quits.
We have discussed some data types in our guide. We have also seen how to get the size of some data type variables. Hope you find it easy to get along and learn something new.