C Variables

3 min read ·

Variables are containers used to store data values. Each variable has a name, a data type, and a value.
In C, you must declare a variable before using it. This tells the compiler what type of data the variable will store.

Explanation

int is the data type
age is the variable name
25 is the value stored
Note

You cannot use a variable without declaring it first in C.

Declaring Variables

You can declare variables by specifying the data type followed by the variable name.

Explanation

int stores integer values
float stores decimal numbers
char stores single characters
Pro Tip

Always initialize variables to avoid using garbage values.

Declaring Multiple Variables

You can declare multiple variables of the same type in one line.

Explanation

Multiple variables can be declared using commas
Each variable can have its own value

Data Types in C

Different data types are used to store different kinds of data.
Common data types include
int for integers float for decimal numbers double for larger decimal numbers char for characters

Explanation

double provides more precision than float
%lf is used for double
Note

Choosing the correct data type helps in efficient memory usage.

Changing Variable Values

You can change the value of a variable after declaring it.

Explanation

The old value is replaced by the new value

Naming Rules for Variables

There are some rules you must follow while naming variables
Variable names can contain letters, digits, and underscore
They must start with a letter or underscore
They cannot be C keywords
They are case sensitive

Example of Valid and Invalid Names

Caution

Using invalid variable names will cause compilation errors.

Constants in C

Constants are variables whose values cannot be changed.

Explanation

const keyword makes the variable value fixed
Trying to change it will result in an error
Real World Scenario

Constants are useful for values that should never change, such as fixed prices, days in a week, or mathematical values.

Goal Achieved

You now understand variables in C, including declaration, data types, naming rules, and constants.

Exercise

Write a C program that stores your name, age, and marks in variables and prints them on the screen.
Learn C C Variables | C Course