In programming, a variable is like a labeled storage box in a computer's memory that holds data. Just as you might label a box "Books" to know what's inside, a variable has a name that helps the program remember and use the data stored in it. But not all data is the same - some are whole numbers, some are decimal numbers, some are characters like letters, and so on. This is where data types come in. They tell the computer what kind of data a variable can hold, ensuring the right amount of memory is allocated and the correct operations are performed.
Understanding variables and data types is fundamental to programming because they form the building blocks for storing and manipulating information.
Let's start by understanding how variables work in programming.
Declaration means telling the computer that you want to create a variable and specify what type of data it will hold. Initialization means assigning an initial value to that variable.
For example, in many programming languages, you might write:
int age; // Declaration of an integer variable named ageage = 20; // Initialization with value 20Or combine both steps:
int age = 20; // Declaration and initialization togetherVariable names must follow certain rules to be valid:
int, if, while)Using meaningful names helps make your code easier to read. For example, priceInINR is clearer than just p.
When a variable is declared, the computer sets aside a specific amount of memory to store its value. The size of this memory depends on the data type. For example, an int might use 4 bytes, while a char uses 1 byte.
In this diagram, the variable name age points to a memory location that holds the value 20.
Data types define the kind of data a variable can store. They determine the size of the memory allocated and the operations that can be performed on the data.
These are the basic data types commonly used in programming:
| Data Type | Size (Bytes) | Value Range | Example Usage |
|---|---|---|---|
int | 4 | -2,147,483,648 to 2,147,483,647 | Storing whole numbers like age = 25 or priceInINR = 1500 |
float | 4 | Approximately ±3.4e-38 to ±3.4e+38 (with decimals) | Storing decimal numbers like heightInMeters = 1.75 |
double | 8 | Approximately ±1.7e-308 to ±1.7e+308 (more precise decimals) | Storing precise decimal values like currency calculations |
char | 1 | Single character (e.g., 'A', 'Rs.') | Storing letters or symbols like grade = 'A' |
boolean | 1 (conceptual) | true or false | Storing logical values like isAvailable = true |
These are data types built from primitive types:
Sometimes, you need to convert data from one type to another. This is called type conversion. It can be:
int to a float.graph TD A[Start with int value] --> B{Implicit Conversion?} B -- Yes --> C[Convert int to float automatically] B -- No --> D{Explicit Casting?} D -- Yes --> E[Programmer casts float to int] D -- No --> F[No conversion]Why is this important? Because converting from a larger data type to a smaller one (like from float to int) can lead to loss of information (decimal part gets truncated). Hence, explicit casting warns the programmer to handle this carefully.
Step 1: Choose data types based on the data:
intfloatcharStep 2: Declare and initialize variables:
int age = 20;float heightInMeters = 1.75;char grade = 'A'; Answer: Variables declared and initialized successfully.
Step 1: Choose data types:
floatintStep 2: Declare and initialize variables:
float weightInKg = 65.5;int distanceInMeters = 400; Step 3: Calculate total distance after running 3 times:
int totalDistance = distanceInMeters * 3; // 400 * 3 = 1200 meters Answer: Total distance run is 1200 meters.
Step 1: Choose data types:
floatintfloatStep 2: Declare and initialize variables:
float pricePerItem = 299.75;int quantity = 5; Step 3: Calculate total price:
float totalPrice = pricePerItem * quantity; // 299.75 * 5 = 1498.75 Answer: Total price is Rs.1498.75.
Step 1: Explicit casting from float to int truncates the decimal part:
float floatValue = 9.87;int intValue = (int) floatValue; // intValue becomes 9 Step 2: Implicit conversion from int to float happens automatically:
int intNumber = 7;float floatNumber = intNumber; // floatNumber becomes 7.0 Answer: After casting, intValue = 9 and floatNumber = 7.0.
int price;float discount = 5.5;price = discount;char 1grade = 'B'; Step 1: price = discount; assigns a float to an int without casting - causes type mismatch.
Fix: Use explicit casting or assign to a float variable.
price = (int) discount; // Cast float to int, value becomes 5 Step 2: Variable name 1grade starts with a digit - invalid.
Fix: Rename variable to start with a letter or underscore:
char grade1 = 'B'; Answer: Corrected code:
int price;float discount = 5.5;price = (int) discount;char grade1 = 'B'; Constants are like variables but their values cannot change once set. For example, the value of π (pi) is always approximately 3.14159. In programming, constants are declared using keywords like const or final, depending on the language.
Constants help prevent accidental changes to important fixed values and make programs more reliable.
Scope refers to the part of the program where a variable is accessible. Variables can be:
Lifetime is how long the variable exists in memory during program execution. Local variables exist only while the function runs, while global variables exist throughout the program.
Choosing the right data type is important for efficient memory use and performance. For example, if you only need to store whole numbers within a small range, use int instead of float. Using larger data types unnecessarily wastes memory.
floatValue is a floating-point number, intValue is the integer after truncationvariable1, variable2 are numeric variables, total is the resultint is for whole numbers and float or double are for decimal numbers. When to use: When choosing data types for numeric variables.
priceInINR or heightInMeters. When to use: While declaring variables to improve code readability.
When to use: Before performing operations on variables.
When to use: During type conversions in calculations.
When to use: When working with local and global variables.
Progress tracking is paywalled — subscribe to mark subtopics as understood and save your streak.
Go to practice →