Variables are how you store data in C. Think of them as labeled boxes. You put a value in a box, give it a name, and later you can look inside that box or change what is in it.
Declaring a Variable
In C, you have to declare a variable before you can use it. Declaration tells the compiler: "Hey, I need a box named age that holds an integer."
The syntax is: type name;
int age;
float price;
char letter;
Naming Rules
C has strict rules for variable names:
- Names can contain letters, digits, and underscores
- Names must start with a letter or underscore โ never a digit
- Names are case-sensitive:
ageandAgeare different - No spaces or special characters like
@,#,$ - You cannot use C keywords like
int,return,ifas names
Assigning and Printing
You assign a value with =. You print with printf and format specifiers like %d for integers and %f for floats.
#include
int main() {
int age = 25;
float price = 19.99;
char grade = 'A';
printf("Age: %d\n", age);
printf("Price: %.2f\n", price);
printf("Grade: %c\n", grade);
return 0;
}
Try it Yourself โ