Variables in c programming language

Before you change your mind read about Variables in c blog post.

What are variables?

Variable is an entity which changes it’s value at any time during the execution of a program.

Variable name is a name given to the memory location or storage area where the value will be stored.

Those values may be changed at any time during the execution of a program.


How to declare variables in c programming?

Variables in c can be of any type like integer,float,character.

Variable is declaration as follows

datatype variable_list;

Here datatype indicates type of data which memory location holds.

There may be one or more variables can be declared at a given time.

Example

#include<stdio.h>
int main()
{
 int x;
 x=10;
 printf("\n\n\tValue of x is: %d",x);
 return 0;
}
example for variables in c programming
example for variables in c programming

In the above example x is an variable name given to the specific memory location.

x holds integer data.


Rules to declare variables in c programming

  • Variable in c can be any combination of alphabets,digits.
  • The first character in the variable name should be an alphabet or underscore.
  • No commas or blanks are allowed within variable name.
  • No special symbols should be used within variable name.
  • Keywords should not be used as variable names. It can be a part of variable name.

Types of variables in c programming

  • Local variable
  • Global variable
  • Static variable
  • Automatic variable
  • External variable

Local variables in c

Local variable is a variable which has scope limited to specific block or a function.

It cannot be accessed outside the function or block.

Example

#include<stdio.h>
int main()
{
  int x=10;
  printf("\n\n\tx is an local variable. \n\tX holds the value : %d\n\n",x);
  return 0;
}
example for local variable in c programming
example for local variable in c programming

In the above program x is an local variable whose scope is limited to a function.


Global variables in c

A variable which can be accessed anywhere in program is called as global variable.

It is usually declared on top of the program after includeing header files.

It’s value is initialized to zero during declaration automatically.

Example

#include<stdio.h>
int x=5;
int main()
{
 printf("\n\n\tx is an global variable which holds the value %d \n\n",x);
 return 0;
}
example for global variable in c programming
example for global variable in c programming

In the above example x is an global variable. Therefore it can be accessed in the main function.


Static variables in c

Static variable in c can be accessed anytime during the execution of a program.

It’s scope is not limited to the block or function.

The default value of static variable is zero.

It can be initialized only once but its value may change.

It can be accessed even after the execution of a program.

Static variables are declared using static keyword.

Syntax

static datatype variable=value;

Example

#include<stdio.h>
void func()
{
  static int x=5;
  printf("value is %d \n\n",x);
}
int main()
{
  int a=10;
  printf("\n\n\ta is a local variable. x is a static variable \n\t");
  func();
  return 0;
}
example for static variable in c programming
example for static variable in c programming

In the above program x is a static variable.

X can be accessed anywhere in the program because it is a static variable.


Automatic variables in c

For auto variables memory allocation and de-allocation is done automatically. We dont have to specify datatype. Datatype is specified by compiler.

It’s scope is limited to the specific block where it is declared.

It is declared using auto keyword.

The memory is allocated and deallocated automatically during entry and exit of a program.

Example

#include<stdio.h>
int main()
{
  auto int a=10;
  printf("\n\n\ta is an automatic variable \n");
  return 0;
}
example for auto variable in c programming
example for auto variable in c programming

In the above program a is an automatic variable whose scope is limited to main function.


External variables in c

External variables are variables which are declared outside the function. These variables are declared using extern keyword. It should be declared only once.

It can also be declared inside the function but while declaring it inside the function extern keyword must be used.

Extern keyword stands for meaning external.

External variables are available throughout the execution of a program. It’s value can be modified by any function. These can be shared within many files in c.

Dont confuse with static and extern variables.

Example

//extern.h file
extern int a=0;
#include<stdio.h>
#include "extern.h"
int main()
{
  printf("\n\n\ta is an external variable and value is %d \n",a);
  a=80;
  printf("\tValue of a is modified to new value %d \n\n",a);
  return 0;
}
example for extern variable in c programming
example for extern variable in c programming

In the above program a is an external variable.


Scope of variables in c programming

Scope means lifetime of an entity. Incase of a variable scope can be local or global.

The local variables can be accessed within a function or block.

The global variables scope is not limited to the specific function or a block. It is available throughout the program.

Example

#include<stdio.h>
int a=10;
int main()
{
  int b=20;
  printf("a is a global variable \n");
  printf("b is a local variable \n");
  return 0;
}
scope of a variable in c programming
scope of a variable in c programming

In the above program a is a global variable. Therefore it’s scope is throughout the program. It can be accessed anywhere in the program. Where as b is local variable therefore it’s scope is limited to the main program.


Differences between keywords and variables in c programming.

A variable name can be any combination of alphabets,digits. A keyword is predefined and has specific meaning.

A variable is user defined entity. A keyword is predefined and have specific meaning.

A keyword can not be a variable name. There are total 32 reserved words. A variable name can have keyword as its part.

Keywords are always in lowercase. A variable name can be in uppercase or lowercase.

Keyword can not be initialized. variable can be initialized to any value.

Variables can contain value of the datatypes specified. But keywords are predefined.

keywordvariable
It is predefined.It is user defined.
Cannot be used as variable name. Keywords can be a part of variable name.
Always in lowercase.Can be in lowercase or uppercase.

People also ask

What is the definition of variable in c programming?

A variable is an entity which can change it’s value during the execution of a program.

Why not to use global variable?

Global variables can be modified by any part of a program. Therefore it becomes difficult to remember and maintain.

What does static variable mean in c?

Static variable in c can be accessed anytime during the execution of a program. It can be used even after the execution of a program.

What is automatic variable in c?

The variables which are declared inside the block are known as automatic variables. It’s scope is limited to the specific block where it is declared.

What is constant variable?

A constant variable is one which does not changes it’s value during execution of a program. A constant variables are declared using const keyword.


People also read