Procedure
We will be learning about the data types that we can use to define our variables in the C. We will be doing different examples on different data types, e.g., for the “integer” data type, we will be first learning about the basic definition and memory size of the integer data type, and then we will learn how we can declare and then initialize a variable using the integer data type. There are numerous other data types that we would like to address individually in this article. Following is the discussion that will let you know about the various data types and their declaration method associated with their allocated memory sizes.
Integer
From the name “integer”, we can observe that this data type would be used to declare an integer. These whole numbers start from zero, and they may end at any possible number. Integers can be either positive or negative numbers, e.g., -3, 2, etc. But these numbers can never be decimal, e.g., 4.5, 6.7, 8.9, etc. The integers have a memory size of at least 2 bytes and a maximum of 4 bytes, where each byte has eight bits stored in it. So, if we declare any decimal number with the integer data type, our compiler won’t support it and will give an error.
In the figure directly above, we have indicated the method to declare any variable. Say named as “variable” with the data type integer, and we assign it the numeric value “-3,” and for the second integer “age,” we have assigned a value “13”. In the above code, we have declared these variables separately, but we can also define them collectively as well, following the procedure as shown below:
Char
Our next data type for the declaration of a variable in the C is a character abbreviated as “char”. Char is the most fundamental data type of all the existing data types. As the name suggests, this data type stores the characters only, e.g., a, b, h, r, etc. In short, the char data type is responsible for declaring the ASCII characters in the code. Such data types only take the memory size of one byte, which is 8 of the bits in total. There could be no variation in the allocated memory size of this data for any compiler. The format specifier for this data type is represented symbolically as “%c”. Let’s try to declare a variable with the data type as char and try to display it using its format specific representation.
int main () {
char mycharacter = 'A';
printf ("%c\n", mycharacter);
return 0;
}
In the example shown above, we have declared a variable of data type char and initialized it with the ASCII character “A”. Then we displayed it by using the “printf ()” method with its format specifier.
Float/ Double
Now we will discuss another data type, float/ double. Both the float and double data types have the same functionality, but they vary from each other when it comes to memory size. These two same data types are used to declare the decimal numbers or any numbers in the exponential form. The float data type declares the decimal number with the single precision; that is, it has 4 bytes preserved for its memory size (a total of 32 bits), whereas the double declares the decimal numbers with double the precision as compared to float with the memory size of 8 bytes (64 bits). Now in the example, we will try to declare both the numerical and exponential variables using both the types double and float and will display these values afterward using the format specifier for double as “%lf” and for a float as “%f”.
int main () {
float salary;
double price;
salary = 34.5;
price = 64.6;
float exponential = 32.442e2;
printf ("/f", salary);
printf ("%lf", price);
printf ("/f", exponential);
return 0;
}
Void
Another data type in the C language that is used most commonly in any code is the “void” data type. This data type has a memory size of almost 0 bytes. We use such data types whenever we need to define any new function in the code. The void has the basic meaning equal to nothing; it depicts the empty value. Whenever we are required to make any function in the code in which we want to return nothing, then we declare the function with data type void since, with this declaration, the function does not return or store anything in it as it has no memory size allocated for this purpose. We will now try to do an example in the compiler where we will first create the smallest function for “print,” and then we will declare its data type to be Void; if we want the function to return a value, then we will declare the function with data type integer.
void print () {
float salary;
printf ("/f", salary);
}
The example displays how we can define a function with the data type void without returning any value.
Conclusion
Declaration of the data types before defining any variable is the most basic task when it comes to writing the code for any software application. We have discussed these data types’ declaration methods for the variables in this article. This article covers the definition, declaration method, and format specification of the five basic data types in the C programming language, e.g., int, float, double, char, and void. For all these data types, we have implemented different examples to let you guys know about their declaration method in Visual Studio C.