1.2 Data Types
Last updated
Was this helpful?
Last updated
Was this helpful?
We use different types of data when programming. In this tutorial, we will learn about the data types used in C programming such as: int, float, double, char etc. When we declare a variable type, the data type determines how much storage that variable will take, what kind of data will be in that variable.
int type data Integers are integers which can store values like zero, positive numbers, negative numbers (0, -5, 10) etc. but cannot store any decimal number.
The size of int is taken as 4 bytes of computer memory. Only values from -2147483648 to 2147483647 can be kept inside int.
Values outside this range cannot be set to int. C uses 4 bytes of space in computer memory for int type. Since 1 byte = 8 bit, 4 byte means 32 bit (4 byte X 8 bit = 32 bit). Each 1 bit can hold 2 things, 0 and 1.
4 numbers (00, 01, 10, 11) can be kept in 2 bit. Then it can be kept in 32 bit: 2^32 number i.e. 4294967296 number. Now if we put half positive and half negative, then the total number from -2147483648 to -1 is 2147483648 then from 0 to 2147483647 the total number is 2147483648, the total number is 4294967296. Can be kept.
If you have trouble understanding the calculation, read it carefully once more, it will be clear.
This time we will write a program to add the runs of 2 cricketers. Open in the browser as before. Then I write the following program and run it, then I will explain line by line.
After running the program you will see on the output screen on the right that:
Here 2 int type variables named firstPlayer
and secondPlayer
are declared. In these 2 variables we have assigned values 60 and 30 respectively. After the previous chapters of the ebook, it is really easy to understand.
Then we declare a new variable called int total and assign the sum of firstPlayer
and secondPlayer
to it. That is, the value of total will be equal to the sum of firstPlayer
and secondPlayer
values.
The next line is quite important. We have used the printf () function to display the sum on the monitor's output screen.
There are 2 parts inside the printf () function. In the first part, Total score is %d means that we want to print on the screen Total score is and an integer type value which will replace %d. Then we have written the total with a comma and explained that in %d, we have to print the value of total. As I used %d for integer type values, I have to write something else for other types of variables.
For example, if we used the double type value we learned in the previous chapter, we would have to write %lf instead of %d. We learned about int type data. Now we will learn about some more important data types.
float and double are used to store real numbers or real number type values. These include 1, 47, 20.23, -11, -23.2, that is, decimal numbers or zero, positive or negative numbers.
Now the question is, what is the difference between float and double?
float can store 4 bytes of data and double is twice the amount of float, i.e. 8 bytes can store data.
Just as we used %d for int type value, we use %f for float and %lf for double(lowercase L).
We declare character type data using the keyword char. E.g.
We use %c for char type data. Such as:
Output:
Note that char type data can store 1 byte amount of data.
int, float, double, char etc. are different data types.
Each data type uses a different amount of data stores.