C Programming

Basic Datatypes in C and How to Use Them

“In the programming language C, each variable has a corresponding data type. Data types are declared for each variable defined. A collection of data with fixed values, meaning, and features are referred to as a data type. Each data type has a unique set of operations that can be done on it and includes varied memory requirements. Basic data types’ memory requirements can vary depending on whether a 32- or 64-bit operating system is used. The variable’s data type, such as integer, character, floating point, double, and so on, is given. Integer- and floating-point-based data types are the fundamental data types. Both signed, and unsigned literals are supported in C. Different forms of data are needed by applications to store information. For instance, age is better stored as an integer even when the name is an array of characters. If the data is saved in the appropriate format and with the appropriate kinds, we can carry out a variety of operations (sum, average, concatenation, matching, etc.). That is why C has so many different data types — so that we can better distinguish and categorize data. There are two kinds of datatypes: Basic Data types (int, char, double, float) and Derived Data types (array).”

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.

About the author

Saeed Raza

Hello geeks! I am here to guide you about your tech-related issues. My expertise revolves around Linux, Databases & Programming. Additionally, I am practicing law in Pakistan. Cheers to all of you.