🟢 Beginner  ·  Lesson 06

Variables and Data Types

Variables और 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.

Variable क्या है?

एक variable memory में एक named location है जो एक value store करती है। इसे एक labeled box की तरह सोचें — आप box को एक नाम देते हैं, specify करते हैं कि इसमें किस type की चीज़ रखी जा सकती है, और फिर उसमें value डालते हैं।

Concept
// Variable = type + naam + value
int age = 20;
//  ↑    ↑    ↑
// type naam value

Declaration और Initialization

Declaration – Memory Reserve करना

C Language
int age;        // Declare — अभी value नहीं दी
float price;    // float के लिए memory reserve
char grade;     // character के लिए memory reserve

Initialization – Value देना

C Language
int age = 20;          // Declare + Initialize एक साथ
float price = 99.50f; // Float value
char grade = 'A';     // Character (single quotes!)
⚠️ Uninitialized Variables

बिना initialize किए variable use करने पर garbage value मिलती है। हमेशा variable को use करने से पहले initialize करें!

C के Data Types

1. int – Integer (पूर्ण संख्या)

Whole numbers store करता है (कोई decimal नहीं)। Size: आमतौर पर 4 bytes

C Language
int marks = 95;
int temperature = -5;   // Negative integers भी हो सकते हैं
int year = 2025;

2. float – Decimal Number (Single Precision)

Decimal numbers store करता है। Size: 4 bytes। Precision: ~6-7 decimal digits।

3. double – Decimal Number (Double Precision)

float से ज़्यादा precise। Size: 8 bytes। जब high accuracy चाहिए तो double use करें।

4. char – Single Character

एक single character store करता है। Size: 1 byte। Character को single quotes में लिखते हैं।

Sizes और Ranges

Data TypeSizeRangeFormat Specifier
char1 byte-128 से 127%c
int4 bytes-2 अरब से +2 अरब%d
float4 bytes±3.4×10³⁸ (6-7 digits)%f
double8 bytes±1.7×10³⁰⁸ (15-16 digits)%lf

Complete Programs

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

  • Letter (a-z, A-Z) या underscore (_) से शुरू होना चाहिए
  • Letters, digits (0-9), और underscores contain कर सकता है
  • Digit से शुरू नहीं हो सकता: 2age
  • C keywords use नहीं कर सकते: int, float, for
  • Case-sensitive है: age और Age अलग-अलग variables हैं

सारांश

  • Variable एक named memory location है जो value hold करती है
  • C में variable declare करते समय हमेशा data type specify करें
  • Main types: int (पूर्ण संख्या), float/double (दशमलव), char (character)
  • Format specifiers: %d (int), %f (float), %lf (double), %c (char)
  • Garbage value से बचने के लिए variables को हमेशा initialize करें
🏋️ Practice Exercise

एक C program लिखें जो आपका नाम का initial (char), age (int), CGPA (float), और total fees (double) store करे और print करे।

← Back to C Tutorial