Variables and Data Types
What is a Variable?
A variable is a named location in memory that stores a value. Think of it as a labeled box — you give the box a name, specify what type of thing it can hold, and then put a value in it.
// Variable is: type + name + value int age = 20; // ↑ ↑ ↑ // type name value
In the above example, int is the data type (integer), age is the variable name, and 20 is the value stored in it.
Declaration and Initialization
Declaration – Reserving Memory
Declaration tells the compiler: "Set aside memory for a variable with this name and this type."
int age; // Declared, not yet initialized float price; // Memory reserved for a float char grade; // Memory reserved for a character
Initialization – Giving a Value
int age = 20; // Declare + Initialize together float price = 99.50; // Float value char grade = 'A'; // Character (single quotes!) // Multiple declarations int a, b, c; // Declare multiple at once int x = 1, y = 2, z = 3; // Declare + init multiple
Using an uninitialized variable gives garbage value — whatever random data was in that memory location. Always initialize variables before use!
Data Types in C
Data types tell the compiler what kind of data a variable will hold, and how much memory to allocate.
1. int – Integer Numbers
Stores whole numbers (no decimal). Size: usually 4 bytes.
int marks = 95; int temperature = -5; // Negative integers OK int year = 2025;
2. float – Decimal Numbers (Single Precision)
Stores decimal numbers. Size: 4 bytes. Precision: ~6-7 decimal digits.
float price = 99.99f; // 'f' suffix for float float pi = 3.14159f;
3. double – Decimal Numbers (Double Precision)
More precise than float. Size: 8 bytes. Precision: ~15-16 decimal digits. Use double when you need high accuracy.
double pi = 3.14159265358979; // More precise double salary = 55000.75;
4. char – Single Character
Stores a single character. Size: 1 byte. Character is enclosed in single quotes.
char grade = 'A'; char initial = 'G'; char newline = '\n'; // Escape character
5. Type Modifiers (short, long, unsigned, signed)
short int s = 100; // 2 bytes long int l = 1000000L; // 4 or 8 bytes long long int ll = 9999999999LL; // 8 bytes unsigned int u = 50000; // Only positive values
Sizes & Ranges Table
| Data Type | Size | Range | Format Specifier |
|---|---|---|---|
char | 1 byte | -128 to 127 | %c |
unsigned char | 1 byte | 0 to 255 | %c |
short int | 2 bytes | -32,768 to 32,767 | %hd |
int | 4 bytes | -2,147,483,648 to 2,147,483,647 | %d |
unsigned int | 4 bytes | 0 to 4,294,967,295 | %u |
long long int | 8 bytes | ±9.2 × 10¹⁸ | %lld |
float | 4 bytes | ±3.4×10³⁸ (6-7 digits) | %f |
double | 8 bytes | ±1.7×10³⁰⁸ (15-16 digits) | %lf |
Use sizeof() to check the actual size on your system:
#include <stdio.h> int main() { printf("int: %zu bytes\n", sizeof(int)); printf("float: %zu bytes\n", sizeof(float)); printf("double: %zu bytes\n", sizeof(double)); printf("char: %zu bytes\n", sizeof(char)); return 0; }
Complete Programs
Program 1: Student Information
#include <stdio.h> int main() { int rollNo = 101; float percentage = 89.5f; char grade = 'A'; double fees = 45000.75; printf("Roll No: %d\n", rollNo); printf("Percentage: %.1f%%\n", percentage); printf("Grade: %c\n", grade); printf("Fees: %.2lf\n", fees); return 0; }
Variable Naming Rules
- Must start with a letter (a-z, A-Z) or underscore (_)
- Can contain letters, digits (0-9), and underscores
- Cannot start with a digit:
2age❌ - Cannot use C keywords:
int,float,for, etc. ❌ - Case-sensitive:
ageandAgeare different variables - No spaces allowed:
my age❌
| Valid Names | Invalid Names |
|---|---|
| age, marks, _count, myName, total2 | 2age, my-name, int, for, hello world |
Summary
- A variable is a named memory location that holds a value
- Always specify a data type when declaring a variable in C
- Main types:
int(whole numbers),float/double(decimals),char(characters) - Use
sizeof()to check memory size of any type - Format specifiers:
%d(int),%f(float),%lf(double),%c(char) - Always initialize variables to avoid garbage values
Write a C program that stores your name's initial (char), age (int), CGPA (float), and total fees paid (double). Print all using proper format specifiers.
Frequently Asked Questions
What is a variable in C?
int age;, which reserves memory to store an integer you can later read or update.What are the basic data types in C?
int for whole numbers, float and double for decimal numbers, and char for single characters. C also provides modifiers like short, long, signed and unsigned to adjust their size and range.What is the difference between float and double in C?
double uses more memory (usually 8 bytes) and holds about twice the precision of float (usually 4 bytes). Use double when you need more accurate decimals and float when saving memory matters more.How do you declare and initialise a variable in C?
int count = 0;. Declaring and initialising in one line is good practice because an uninitialised variable holds an unpredictable garbage value.What are the rules for naming variables in C?
int or return. Names are case-sensitive, so Age and age are different variables.