🟢 Beginner  ·  Lesson 06

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.

Concept
// 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."

C Language
int age;        // Declared, not yet initialized
float price;    // Memory reserved for a float
char grade;     // Memory reserved for a character

Initialization – Giving a Value

C Language
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
⚠️ Uninitialized Variables

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.

C Language
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.

C Language
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.

C Language
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.

C Language
char grade = 'A';
char initial = 'G';
char newline = '\n';  // Escape character

5. Type Modifiers (short, long, unsigned, signed)

C Language
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 TypeSizeRangeFormat Specifier
char1 byte-128 to 127%c
unsigned char1 byte0 to 255%c
short int2 bytes-32,768 to 32,767%hd
int4 bytes-2,147,483,648 to 2,147,483,647%d
unsigned int4 bytes0 to 4,294,967,295%u
long long int8 bytes±9.2 × 10¹⁸%lld
float4 bytes±3.4×10³⁸ (6-7 digits)%f
double8 bytes±1.7×10³⁰⁸ (15-16 digits)%lf

Use sizeof() to check the actual size on your system:

C Language
#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;
}
int: 4 bytes float: 4 bytes double: 8 bytes char: 1 bytes

Complete Programs

Program 1: Student Information

C Language
#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;
}
Roll No: 101 Percentage: 89.5% Grade: A Fees: 45000.75

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: age and Age are different variables
  • No spaces allowed: my age
Valid NamesInvalid Names
age, marks, _count, myName, total22age, 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
🏋️ Practice Exercise

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?
A variable is a named storage location that holds a value your program can use and change. You declare it with a type and a name, such as int age;, which reserves memory to store an integer you can later read or update.
What are the basic data types in C?
The core data types are 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?
Both store decimal numbers, but 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?
You give the type and name to declare it, and optionally assign a value to initialise it, for example 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?
A variable name may contain letters, digits and underscores, must not start with a digit, and cannot be a C keyword such as int or return. Names are case-sensitive, so Age and age are different variables.
← Back to C Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.