In C programming, a **Variable** is essentially a name given to a storage area that our programs can manipulate. Each variable in C has a specific type, which determines the size and layout of the variable's memory, the range of values that can be stored within that memory, and the set of operations that can be applied to the variable.
Think of a variable as a labeled container. When you write a program, you often need to store information like a user's age, a price of an item, or a mathematical result. Instead of remembering the complex memory address of where that data is stored, you give it a simple name like age or total.
To use a variable in C, you must first tell the compiler about it. This is a two-step process, though often done in one line.
This tells the compiler the variable's name and its data type. No value is assigned yet.
int studentAge;
This is the process of assigning an initial value to the variable.
studentAge = 20;
C is very strict about how you name your variables. Following these rules is mandatory to avoid Syntax Errors:
age, Age, and AGE are three different variables.int, return, if) as variable names.The type of variable you choose dictates how much space it takes in the computer's RAM. Selecting the right type is key to performance.
| Type | Size | Format Specifier | Usage |
|---|---|---|---|
int |
2 or 4 bytes | %d | For whole numbers. |
float |
4 bytes | %f | For simple decimals. |
char |
1 byte | %c | For single characters. |
As the name suggests, "variables" can vary. You can overwrite the stored value at any time in your code.
If you don't want others (or yourself) to change a variable's value, use the const keyword. This will create a read-only variable.
Variables allow us to create flexible programs. For example, a simple score counter in a game:
int a = 30;, use int user_age = 30;. This makes your code self-explanatory and professional.