Basic Data Types in C
What we have done in the previous article is a lot. I mean we were able to print Hello World..! that in itself is an achievement. This article is about variables and basic data types in C.
What is a Variable
As the name suggests, a variable is used to store a value that may be needed in the future. For example, if you have to add two numbers, you have to store the values in a variable so that you can perform an addition in it. Now you might be wondering on-How to store the value in a variable? For that to understand, you should know about datatypes.
What is a Datatype in C
You want to save a value to a variable but a value can be of different types. For example, a number or a character or a decimal. So, you want to tell the “compiler” that “Hey man, the value I am storing in this variable is an integer type, please save it as an integer”. Datatype tells the compiler to store the value in a particular type. For example-
Here, a is the name of a variable which holds the “integer value” 10. The int datatype tells the compiler that the variable stores an integer value.
Types of Data-Types
They can be divided into basic and derived data types. Let’s leave derived type for now. The basic data types can be divided into integer, float, and void. Let’s go through one by one in short.
Integer data types
These data types include char, int, long int. These are used for storing the number. How big the number can be stored depends on the size of the datatypes. A char has a memory of one byte, therefore, it can store the number up to 255(2^8-1). Similarly, “int” and long has a memory of 2 bytes and 4 bytes respectively which is why “int” can store the number up to 65535(2^16-1). A char datatype is generally used to hold the character which is why it is often referred to as the third datatype which is character datatype.
Float data type
These are used to store decimal numbers. In this, mainly three datatypes are used viz float, double and long double. A float has a memory of 4 bytes, double has a memory of 8 bytes and long double has a memory of 10 bytes.
Void data type
Void means nothing. This datatype is used in functions that returns nothing. You will learn about function in an upcoming post. There I will explain it’s importance.
I hope you guys get the idea about datatypes in C. If there is something that you haven’t able to get please do tell me in the comment section below.