Written by - Vanshika Yadav

Variables in C


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:-

  1. Variables can be alphabets and underscore.
  2. Variables names cannot start with the digit it only starts with alphabets and underscore.
  3. No commas or blanks are allowed.
  4. No special symbol other than underscore is allowed.
  5. Variables names are case-sensitive.
Valid variables names:
  1. int a;
  2. int _a;
  3. int a30;

Invalid variables names:

  1. int 8;
  2. int long;
  3. 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
}