Variable in C

Facebooktwitterredditpinterestlinkedinmailby feather

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 start with alphabets or underscore (_).
  • No space is allowed in a variable while declaration.
  • Variable must not be any reserved word or keyword.

Let’s see 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:

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

Example:

2. Global variable

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

3. Static variable

A static variable is declared with the static keyword. It contains its value during the function call.

Example:

4. Automatic variable

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

5. External variable

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

// save it as abc.h

//save as prog1.c

Facebooktwitterredditpinterestlinkedinmailby feather

Leave a Comment