Variable in C Language
A variable is a name of the memory location. It is used to store data. Its value can be changed, and it can be reused many times.
It can take different values but one at a time. These values can be changed during execution of the program.
A data types is associated with each variable and it decides what values the variables can take.
Declaration of Variable in C
See the syntax to declare a variable:
data_type variable_name;
example of declaring the variable:
int num ;
float num1;
char num2;
Here num, num1, num2 are variable. The int, float, char are the data type.
Initialization of variables:
int num=10;
float number=20.5;
char numbers='A';
Type of Variable :-
1. local variable
2. global variable
3. static variable
local variable
A variable that is declared inside the function or block is called a local variable.
global variable
A variable that is declared outside the function or block is called a local variable.
static variable
A variable that is declared with the static keyword is called static variable.
Whats cant's declare in a variable:
- A variable can have alphabets, digits, and underscore.
- A variable name can start with the alphabet, and underscore only. It can't start with a digit.
- No space is allowed within the variable name.
- A variable name must not be any reserved word or keyword, e.g. int, float, etc.
Valid variable names:
int num;
int _number;
int numbers;
Invalid variable names:
int 4;
int num number;
int char;