Turning Learners into Developers
- What are Variables?
Variables are containers used to store data values like numbers and characters.
In simple words:
- A variable is like a named box where you store a value for later use.
In C, every variable must have a data type, which tells the program what kind of value it can store.
- int → stores whole numbers (integers), like 127 or -127
- float → stores decimal numbers, like 17.99 or -17.99
- char → stores a single character, like 'a' or 'B' (written inside single quotes)
- Real-Life Examples
🔤 Example 1: School Marks
- Marks = 95
In C:
int marks = 95;🔤 Example 2: Age
- Age = 20
In C:
int age = 20;🔤 Example 3: Temperature
- Today's temperature = 36.5°C
In C:
float temperature = 36.5;🔤 Example 4: Grade
- Student Grade = A
In C:
char grade = 'A';🛒 Example 5: Shopping Price
- Price of item = ₹199.99
In C:
float price = 199.99;- Declaring (Creating) Variables
To create a variable:
- Write data type
- Give a name
✅ Syntax:
- type variableName = value;
- Where type is one of the C data types (such as int), and variableName is the name you choose (for example x or myNum).
- The equal sign = is used to assign a value to the variable.
Example:
int myNum = 17;Declare First, Assign Later
int myNum; // Declaration
myNum = 17; // Assignment- Real-Life Analogy
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'; |
- Output Variables in C
- In C, we use the printf() function to display output on the screen.
- Example:
printf("Hello World!");Printing Variables
- In many programming languages (like Python, Java, and C++), you can directly print variables.
- But in C, this is not allowed.
#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:
- to output the value of an int variable, use the format specifier %d surrounded by double quotes (""), inside the printf() function
- %d tells C → “expect an integer”
- myNum provides the value
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:
- Easy to reuse
- Easy to update
- Makes code clean and readable
- Format Specifiers in C
🔹 What are Format Specifiers?
- Format specifiers are used with the printf() function to print variables.
- A format specifier is a placeholder that tells C what type of value will be printed.
🔹 Structure
- A format specifier always starts with: % + letter
- A format specifier is a placeholder that tells C what type of value will be printed.
| 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) |
|
%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 |
| Shortest representation | printf("%g", 3.14000); |
%G | float/double | Shortest (uppercase) | printf("%G", 3.14); |
%% |
| Prints % symbol | printf("%%"); |
Important Notes (Exam Tips)
- %d and %i are almost the same (both for integers)
- %lf is used for double (especially in scanf())
- %f is commonly used for both float and double in printf()
- %u cannot store negative values
- %x, %o are used for number system conversions
🔹 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 |
| 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:
- %.nf → decimal control
- %5d / %-5d → alignment
- %05d → zero padding
- %+d → show sign
- %*d, %.*f (dynamic formatting)
- %#x, %#o (prefix formats)
- Change Variable Values
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:
- You copy a number from your friend's notebook
- Later, your friend changes their number
- Your copy does NOT change
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)
- Variables can change during program execution
- Latest value overwrites previous value
- You can: Copy values between variables
- You can: Perform calculations
- You can: Update using current value
- Declare Multiple Variables in C
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
- z = 50;
- y = z; // 50
- x = y; // 50
- All variables get 50
Real-Life Example – Think of filling bottles:
- You pour the same juice into 3 bottles
- All bottles have equal quantity
Key Points
- Use , to declare multiple variables
- All must be same type
- You can assign: Different values → int x=1, y=2
- You can assign: Same value → x=y=z=50
- C Variable Names (Identifiers)
What are Identifiers?
- All C variables must be identified with unique names.
- These unique names are called identifiers.
- Identifiers can be short names (like x and y) or more descriptive names (age, sum, totalVolume).
💡 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
- Letters (a–z, A–Z)
- Digits (0–9)
- Underscore (_)
Example: int total_sum = 100;
2️⃣ Must Start With
- Letter OR underscore (_)
- Valid: int age; int _count;
- Invalid: int 1age; // starts with number
3️⃣ Case-Sensitive
- int myVar = 10;
- int myvar = 20;
- These are different variables
4️⃣ No Spaces or Special Characters
- Invalid:
int my age; // space not allowed
int my-age; // '-' not allowed
int total#; // '#' not allowed5️⃣ Cannot Use Reserved Words
- Invalid: int int = 10; // 'int' is keyword
- valid: int number = 10;
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)
- int totalMarks;
- int studentAge;
🔹 underscore_style
- int total_marks;
- int student_age;
Key Points
- Variable names = Identifiers
- Must be unique
- Follow naming rules strictly
- Use meaningful names
- Avoid keywords
01
C Introduction
02
C Get Started
03
C Syntax
04
C Input & Output
05
C Comments
06
C Variables
07
C Data Types
08
C Constants
09
C Operators
10
C Booleans
11
C Type Conversion
12
C If...Else
13
C Switch
14
C For Loop
15
C While Loop
16
C Do/While Loop
17
C Nested For Loop
18
C Break/Continue
19
C Arrays
20
C Strings
21
C User Input
22
C Memory Address
23
C Pointers
24
C Functions
25
C Files
26
C Structures
27
C Enums
28
C Memory
29
C Errors
30
C More
31
C Projects
32
C Reference
33
C Examples
34
DS With C
35
C Exam Questions
36