Variables & declarations
8 min lesson
- A variable is a named storage box in the computer's memory that holds a value you can change later.
- Variables matter because programs need to remember things: a score, a price, a name, or a count of items.
- Think of a variable as a labeled jar in your kitchen. The label is the name, and the contents are the value. You can empty the jar and put something new inside.
- A declaration tells the computer the type and name of the variable, like int score;. The type says what kind of value the jar can hold.
- Initialization means giving the variable its first value, like int score = 0;. It is good practice to always initialize.
- The computer reserves a small piece of memory for the variable and remembers the name so you can use it later in the program.
- The syntax is: type name = value; where type is something like int or float, name is your chosen label, and value is the starting content.
- A realistic use: tracking a player's score in a game by storing it in a variable called playerScore.
- A surprising detail: if you declare a variable without initializing it, it holds an unpredictable 'garbage' value, not zero.
- The example program creates two variables, one for age and one for a price, prints them, then changes the price and prints it again so you can see the value change.
- Always initialize your variables before using them to avoid garbage values.
- Choose clear names like totalPrice instead of single letters like x.
- The type you choose must match the kind of value you store.
- You can change a variable's value as many times as you like after declaring it.
- Practice by changing the values in the example and watching the output change. You are doing great!
#include <stdio.h>
int main(void) {
// Create a box called age that holds a whole number, starting at 25
int age = 25;
// Create a box called price that holds a decimal number, starting at 9.99
float price = 9.99f;
// Print the current age with a label so the reader knows what it is
printf("Age: %d\n", age);
// Print the current price with a label
printf("Price: %.2f\n", price);
// Change the contents of the price box to a new value
price = 4.50f;
// Print the new price to show that variables can change
printf("New price: %.2f\n", price);
return 0;
}What happens: Age: 25 Price: 9.99 New price: 4.50
Breaking it down
Include line
#include <stdio.h>Brings in the standard input/output library so we can use printf to print text. Without it, the compiler would not know what printf is.
Main function
int main(void) {This is where the program starts running. Every C program needs a main function. The computer begins at the first line inside it.
Variable declaration and initialization
int age = 25;Creates a box named age that can hold a whole number, and puts 25 inside it right away. The int type means whole numbers like 25, -3 or 100.
Float variable
float price = 9.99f;Creates a box named price that can hold a decimal number. The f at the end tells the compiler this is a float value, not a double.
First printf
printf("Age: %d\n", age);Prints the word Age: followed by the value inside age. The %d is a placeholder that gets replaced by the integer value.
Changing a value
price = 4.50f;Empties the price box and puts 4.50 inside it. The old value 9.99 is gone. This shows that variables can change while the program runs.
Return statement
return 0;Tells the operating system the program finished successfully. Zero means success. Without it, the program might report an error to the system.
A helpful analogy
A variable is like a labeled jar in your kitchen. The label on the jar is the variable name, and whatever is inside is the value. You can open the jar, empty it, and put something new inside, just like you can change a variable's value. The label stays the same even when the contents change.
Nesting rules
- 1.Declare each variable before you use it. The compiler reads top to bottom, so it must see the declaration first.
- 2.Use a type that matches the value: int for whole numbers, float for decimals. Storing a decimal in an int silently drops the decimal part.
- 3.Initialize variables when you declare them whenever possible, like int score = 0;, to avoid unpredictable garbage values.
Common mistakes
Using a variable without initializing it
Avoid
int score;
printf("%d", score); // prints garbageUse instead
int score = 0;
printf("%d", score); // prints 0An uninitialized variable holds whatever bits were already in that memory, which is unpredictable. Always give it a starting value.
Storing a decimal in an int variable
Avoid
int price = 9.99; // becomes 9Use instead
float price = 9.99f; // keeps 9.99The int type can only hold whole numbers, so the decimal part is silently thrown away. Use float or double for decimals.
Forgetting the semicolon at the end of a declaration
Avoid
int age = 25Use instead
int age = 25;C uses semicolons to mark the end of statements. Without it the compiler shows an error like 'expected ;'.
Quick tips
- •Name variables after what they hold: studentAge is clearer than a or x.
- •Change the value of age in the example and run it again to see the output change.
- •Try declaring a variable without a value and print it to see the garbage value, then fix it by initializing.
- •Keep declarations near the top of main so they are easy to find.
- •Read compiler errors from the top down; the first one is often the real problem.