Variable in C

tutorial and example
tutorial and example

Variable is an identifier that holds data in memory. It is used to identify input data in a program. The value of the variable can change at the time of executions.

Let’s consider the syntax of a variable.

We can also declare multiple variables with the same data type by using ( , ) comma.

     Example:

Rules for declaring the variable:

  • Variable name should be start with alphabets or underscore (_).
  • No space are allowed in variable while declaration.
  • Variable must not be any reserve word or keyword.

Let’s see the some example of how to declare the variables.

Valid declaration  Invalid  declaration
int  b; int 2;
char _name; int  a b;
int a4; int  double;

Types of variables in C.

There are various types of variable in C:

  1. local variable
  2. global variable
  3. static variable
  4. automatic variable
  5. external variable

1. Local variable:

Local variable is declared inside of the function or block. It must be declared at the start of the block.

Example:

2. Global variable

Global variable is declared outside of the function or block. The value of the global variable can be changed.

3. Static variable

Static variable is declared with static keyword. It containsits value during the function call.

Example:

4. Automatic variable

Automatic variable is declared inside the block. The auto keyword is used to declare the automatic variables.

5. External variable

External variable is used to share the multiple C source files by using external variables

// save it as abc.h

//save as prog1.c