In Progress
Lesson 6, Topic 1
In Progress

# 6.1 What are variables and data types?

##### 08/10/2021
Lesson Progress
0% Complete A variable is a value that can change depending on the conditions and the information in the computer program. Variable can store information like characteristics, number or quantity which can be measured or counted.

A variable can be thought of as a memory location that can hold values of a specific type. The value in a variable may change during the life of the program—hence the name “variable.”

Each variable has a specific data type, which indicates which type of data it may hold. For instance, a variable that holds text strings has the data type String and is called a string variable. A variable that holds integers (whole numbers) has the data type Integer and is called an integer variable. When creating a variable, it is necessary to declare a data type it contains. A data types classifies that which type of value a variable has and what type of mathematical or logical operation can be applied without an error.

The most commonly used data types are:

• int − Used to store an integer value.

• char − Used to store a single character.

• float − Used to store decimal numbers with single precision.

• double − Used to store decimal numbers with double precision.

Below is table showing the different range and bytes of the data types used in programming:

Data TypesBytesRange
short int2-32,768 to 32,767
unsigned short int20 to 65,535
unsigned int40 to 4,294,967,295
int4-2,147,483,648 to 2,147,483,647
long int4-2,147,483,648 to 2,147,483,647
unsigned long int40 to 4,294,967,295
signed char1-128 to 127
unsigned char10 to 255
float41.2E-38 to 3.4E+38
double82.3E-308 to 1.7E+308

The data types that hold numeric values are: byte, short, int, long, float and double. The difference between them is the size and precision of the value they contain.

#### How to declare a Variable?

The declaration of variable comprises of three parts :- first data type then the variable name and last the statement is terminated with a semicolon (;).

For example : int Name=10;

In this example int is the integer data type and Name is the variable.

Note : This is a case sensitive programming so use the Name variable as you declared.

Example Code:

#include <stdio.h>

int main() {

// datatypes

int a = 10;

char b = ‘S’;

float c = 2.88;

double d = 28.888;