Variables are memory locations it stores data.Let’s see how to declare the variable:
Data_type variable_name;
int x;
char y
float z
here int, float, char are the data type. The x,y,z are the variables.
Rules for defining variables:-
- Variables can be alphabets and underscore.
- Variables names cannot start with the digit it only starts with alphabets and underscore.
- No commas or blanks are allowed.
- No special symbol other than underscore is allowed.
- Variables names are case-sensitive.
Valid variables names: int a;
int _a;
int a30;
Invalid variables names:
int 8;
int long;
int x y;
Types of Variable:-
Local Variable
variable that is declared inside the function or block. It must be declared at the start of the block.
void function1(){
int x=10; //Local Variable
}
Global variable
The variable which declared outside the function or block.
int value=20; //Global Variable void function1(){
int x=10; //Local Variable
}
Static variable
A variable that declared the static keyword. Static variable can be accessed by any class or function and is present until the program is stopped.
void function1(){
int x=10; //Local Variable
static int y=10 //static variable
}
Automatic variable
All variables in C that are declared inside the block, are automatic variables by default. We can explicitly declare an automatic variable using an auto keyword.
void function1(){
int x=10; //Local Variable
auto int y=10 //automatic variable
}
External variable
We can share a variable in multiple C source files by using an external variable. To declare an external variable, you need to use extern keyword.
void function1(){
extern int x=10; //external variable also global
}