Integer
The first data type of the Basic Data Types that will be discussed is the Integer. Integer types can have unsigned values meaning only positive, or signed values which include negative values. Integer values are always signed unless otherwise specified. The integer can further be classified into other types such as int, short int, and long int, which are further classified into a signed int, unsigned int, signed short int, unsigned short int, signed long int, and unsigned long int. In the example shown below, the line of code: int a; shows that the variable a has been given the data type int, which allows it to store a number into it, which in this case is 55.
Char
Now next data type is Char, which stands for Character. One character is kept in char as char is made up of just one byte. It is to be noted that we have used single quotes for single characters, whereas, in the example below, the variable a is a character array that stores more than one character, or rather a series of characters; hello world. For this, there is a need for double quotes for Strings (character arrays).
Char can be signed (range: -128 to +127) or unsigned (range: 0 to 1), just as the int data type (0 to 255). Moreover, since char accepts int values as well, you can also conceive of char as an int value. When you store an int within the defined range in a char, the difference between signed and unsigned values becomes important.
The example below shows that the single character h has been assigned the variable a with char as its data type. Whereas the next image shows a being declared as a character array that has been assigned with a hello world, an array of chars.
Float and Double
In this part, we will examine two different data types: float and double. Decimal and exponential numbers are stored in C using the float datatype. It is normally used to hold decimal integers with single precision (numbers with floating point values). In the example below, we see that the variable a has been declared with datatype float and given the decimal value 10.588.
On the other hand, in C, double precision decimal numbers (numbers having floating point values) are stored using the Double data type. The double data type is essentially a precision data type that can store 64 bits of floating point or decimal numbers. Since double has greater precision than float, it is clearer that it uses up twice as much memory as the floating-point type. This can easily manage integers between 16 and 17, either prior to or after the decimal place. The image below shows that the variable a with datatype double holds the value 10.5887.
Array
The array is a datatype belonging to the class of Derived Data Types. Thus, an array of integers, chars, floats, doubles, and other data types is possible. Either the array needs to be initialized, or the declaration needs to include the array’s size. In the example below, the array variable has been named a with the unspecified size of the array (in the square brackets, the size of the array can be declared) and its data type is int meaning the array a stores all values that are of int data type which is clearly seen since 1,2,3,4,5 are all integers.
Signed and Unsigned
The type modifiers in C are signed and unsigned. By utilizing them, you can change how a data type stores its data. With signed, it is permitted to have both positive and negative values stored. Whereas, for unsigned, it is only allowed to store just positive numbers. As seen below, an unsigned int datatype named x stores a positive int (5), whereas the int variable y stores a negative integer (-5).
Short and Long
Short and Long are subtypes of datatype int. Short can be used if just a small integer (in the [32,767, +32,767] range) will be used. On the other hand, you can declare the int to be long if a large number is used. As seen in the example below, the long int x gets assigned a larger number, 54564, whereas the short int y gets a smaller value of -5.
Conclusion
In this article, we looked into all the Basic datatypes, their subtypes, and even a Derived datatype as well. There are more datatypes in C as well. Each data type serves a purpose and contributes to the C programming language’s stability, reliability, and durability. We implemented several examples of these data types better to understand the basic data types and their usage.