Definition
📌 Variables
A variable is a named piece of memory that stores a value your program can read or change.
Syntax
syntax
type variableName = value;
// declare then assign
type variableName;
variableName = value;Example
c
#include <stdio.h>
int main() {
int age = 20;
float price = 49.99;
char grade = 'A';
printf("Age: %d\n", age);
printf("Price: %.2f\n", price);
printf("Grade: %c\n", grade);
return 0;
}Output
outputAge: 20
Price: 49.99
Grade: A
Important Points
- Variable names can contain letters, digits and underscores, but can't start with a digit.
- C is case-sensitive —
ageandAgeare different variables. - Uninitialized local variables contain garbage values, not zero.
- Use meaningful names (
totalPrice, nottp) for readability.
Common Mistakes
- Using a variable before declaring or initializing it.
- Redeclaring the same variable name in the same scope.
- Mismatching the format specifier and the variable's type (e.g.
%dfor afloat).
Interview Tip
🎯 Before your interview
Know the difference between declaration (
int x;) and definition/initialization (int x = 5;) — it's a common interview distinction.
