Variables are containers used to store data values like numbers and characters.

In simple words:

In C, every variable must have a data type, which tells the program what kind of value it can store.

🔤 Example 1: School Marks

In C:

int marks = 95;

🔤 Example 2: Age

In C:

int age = 20;

🔤 Example 3: Temperature

In C:

float temperature = 36.5;

🔤 Example 4: Grade

In C:

char grade = 'A';

🛒 Example 5: Shopping Price

In C:

float price = 199.99;

To create a variable:

✅ Syntax:

C Variable Declaration

Example:

int myNum = 17;

Declare First, Assign Later

int myNum;   // Declaration
myNum = 17;  // Assignment

Think of variables like labeled bottles:

Bottle Label

Value Inside

C Variable

"Milk"

1 liter

int milk = 1;

"Water"

2.5 liters

float water = 2.5;

"Grade"

A

char grade = 'A';

printf("Hello World!");

Printing Variables

#include<stdio.h>
int main()
{
int myNum = 17;
printf(myNum);   // Error
return 0;
}

This will cause an error because printf() expects a format specifier.

#include <stdio.h>

int main() {
  int myNum = 17;
  printf("%d", myNum);
  return 0;
}

How it works:

Different Data Types Example

#include<stdio.h>
int main()
{
int myNum = 17;          
float myFloatNum = 7.99; 
char myLetter = 'S';     

printf("%d\n", myNum);
printf("%f\n", myFloatNum);
printf("%c\n", myLetter);
return 0;
}

Combining Text + Variables

#include<stdio.h>
int main()
{
int myNum = 15;
printf("My favorite number is: %d", myNum);
return 0;
}

Multiple Variables in One printf()

#include<stdio.h>
int main()
{
int myNum = 15;
char myLetter = 'D';

printf("My number is %d and my letter is %c", myNum, myLetter);
return 0;
}

Print Values Without Variables

You can directly print values using format specifiers:

#include<stdio.h>
int main()
{
printf("My favorite number is: %d", 15);
printf("My favorite letter is: %c", 'D');
return 0;
}

Real-Life Example

#include<stdio.h>
int main()
{
int age = 20;
float price = 99.99;
char grade = 'A';

printf("Age: %d, Price: %f, Grade: %c", age, price, grade);
return 0;
}

CodeKilla Tip

Always use variables when possible:

🔹 What are Format Specifiers?

🔹 Structure

Specifier

Data Type

Description

Example

%d

int

Signed integer

printf("%d", 10);

%i

int

Signed integer (same as %d)

printf("%i", 10);

%u

unsigned int

Unsigned integer (only positive)

printf("%u", 10);

%f

float

Decimal number

printf("%f", 3.14);

%lf

double

Double precision float

printf("%lf", 3.14159);

%c

char

Single character

printf("%c", 'A');

%s

string

String (array of characters)

printf("%s", "Hello");

%x

int

Hexadecimal (lowercase)

printf("%x", 255); → ff

%X

int

Hexadecimal (uppercase)

printf("%X", 255); → FF

%o

int

Octal (base 8)

printf("%o", 8); → 10

%p

pointer

 Memory address

printf("%p", &x);

%e

float/double

Scientific notation (lowercase)

printf("%e", 3.14);

%E

float/double

Scientific notation (uppercase)

printf("%E", 3.14);

%g

float/double

Shortest representation

printf("%g", 3.14000);

%G

float/double

Shortest (uppercase)

printf("%G", 3.14);

%%

Prints % symbol

printf("%%");

Important Notes (Exam Tips)

🔹 Special Formatting in C

Format

Data Type

Description

Example

%.2f

float/double

Print with 2 decimal places

printf("%.2f", 3.14159); → 3.14

%.nf

float/double

Print with n decimal places

printf("%.3f", 3.14159); → 3.142

%5d

int

Width of 5 spaces (right aligned)

printf("%5d", 10);

%-5d

int

Left aligned within width

printf("%-5d", 10);

%05d

int

Zero padding

printf("%05d", 10); → 00010

%+d

int

Show + or - sign

printf("%+d", 10); → +10

% d

int

Space before positive number

printf("% d", 10);

%.3s

string

Print first 3 characters

printf("%.3s", "Hello"); → Hel

%10s

string

Width for string (right aligned)

printf("%10s", "Hi");

%-10s

string

Left aligned string

printf("%-10s", "Hi");

%10.3f

float/double

Width + precision

printf("%10.3f", 3.14159);

%*d

int

Width from runtime

printf("%*d", 5, 10);

%.*f

float/double

Precision from runtime

printf("%.*f", 2, 3.14159);

%#x

int

Hex with prefix (0x)

printf("%#x", 255); → 0xff

%#o

int

Octal with prefix (0)

printf("%#o", 8); → 010

%%

Print % symbol

printf("%%"); → %
#include<stdio.h>
int main()
{
printf("|%5d|", 10);    // |   10|
printf("|%-5d|", 10);   // |10   |
printf("|%05d|", 10);   // |00010|
return 0;
}

Must Remember:

1. Reassigning a Variable

When you give a new value to a variable, it replaces the old value.

#include<stdio.h>
int main()
{
int myNum = 15;  // Initial value
myNum = 10;      // New value replaces old
printf("%d\n",myNum);
return 0;
}

💡 Real-life example: Think of a whiteboard — you erase the old number (15) and write a new one (10).

2. Assigning One Variable to Another

You can copy the value from one variable to another.

#include<stdio.h>
int main()
{
int myNum = 15;
int myOtherNum = 23;

myNum = myOtherNum; 
printf("%d\n",myNum); // 23
return 0;
}
💡 Concept: Value is copied, not linked (Changing myOtherNum later won’t affect myNum)
#include <stdio.h>

int main() {
    int myNum = 15;
    int myOtherNum = 23;

    // Copy value of myOtherNum into myNum
    myNum = myOtherNum;

    // Now change myOtherNum
    myOtherNum = 50;

    printf("myNum = %d\n", myNum);
    printf("myOtherNum = %d\n", myOtherNum);

    return 0;
}

Real-Life Example: Think of it like:

Key Concept In C – Assignment (=) copies value, not reference

3. Assigning One Variable to Another

You can declare a variable first and assign value later.

#include <stdio.h>

int main() {
int myNum = 17;
int myOtherNum;

myOtherNum = myNum;

printf("myOtherNum = %d\n", myOtherNum); //17

    return 0;
}

Real-life example: Empty box → later you put something inside it.

4. Adding Variables

You can perform operations using variables.

#include <stdio.h>

int main() {
int x = 5;
int y = 6;
int sum = x + y;

printf("%d", sum); // 11

    return 0;
}

5. Updating Variable Using Itself

#include <stdio.h>

int main() {
int x = 5;
x = x + 1;

printf("%d", x); //6

    return 0;
}

Key Points (Must Remember)

1. Declaring Multiple Variables in One Line

You can declare multiple variables of the same data type using commas.

#include <stdio.h>

int main() {
int x = 5, y = 6, z = 50;
printf("%d", x + y + z); //61

    return 0;
}

💡 Real-life example: Like writing a list: 3 students → Rahul, Ashok, Shashi

2. Assign Same Value to Multiple Variables

You can assign the same value to multiple variables in one line.

#include <stdio.h>

int main() {
int x, y, z;
x = y = z = 50;

printf("%d", x + y + z); //150

    return 0;
}

How It Works

Real-Life Example – Think of filling bottles:

Key Points

What are Identifiers?

💡 Good vs Bad Naming
#include <stdio.h>

int main() {
  // Good variable name
  int minutesPerHour = 60;

  // OK, but not so easy to understand what m actually is
  int m = 60;
  
  printf("%d\n", minutesPerHour);
  printf("%d", m);
  return 0;
}

Rules for Naming Variables

1️⃣ Allowed Characters

Example: int total_sum = 100;

2️⃣ Must Start With

3️⃣ Case-Sensitive

4️⃣ No Spaces or Special Characters

int my age;   // space not allowed
int my-age;   // '-' not allowed
int total#;   // '#' not allowed

5️⃣ Cannot Use Reserved Words

Valid vs Invalid Names

Valid Names

Invalid Names

 Reason

age

1age

Starts with number

_count

my-age

Contains '-'

totalSum

int

Reserved keyword

total_score

total score

Contains space

Naming Styles : Choose one style and stay consistent

🔹 camelCase (Most Common)

🔹 underscore_style

Key Points