Skip to main content

4. Variables in C

 

Variables in C

A variable is a name of the memory location. It is used to store data. Its value can be changed, and it can be reused many times.
It is a way to represent memory location through symbol so that it can be easily identified.Let's see the syntax to declare a variable :

type variable_list;

The example of declaring the variable is given below :

int a;
float b;
char c;

Here a, b, c are variables. The int, float, char are the data types.

We can also provides values while declaring the variables as given below :

int a = 10, b = 20;
float f = 20.8;
char c = 'A';

Rules for defining Variables

  • A variable can have alphabets, digits, and underscore.
  • A variable name can start with the alphabet, and underscore only. It can't start with a digit.
  • No whitespace is allowed within the variable name.
  • A varibale name must not be any reserved word or keyword, e.g. int, float etc.

Types of Variables in C

There are many types of variables in C :

  1. Local Variable
  2. Global Variable
  3. Static Variable
  4. Automatic Variable
  5. External Variable

1. Local Variable

A variable that is declared inside the function or block is called a local variable. They can only be accessed by the function they are declared in. They are inaccessible to the functions outside the function they are declared in.
It must be declared at the start of the block. We must have to initialize the local variable before it is used.

void function1(){
int x = 10;     // local variable
}

2. Global Variable

A variable that is declared outside the function or block is called a global variable. Global variables are accessible throughout the entire program from any function. If local and global variable has the same name, the local variable will take preference.
Any function can change the value of the global variable. It is available to all the functions.

int value = 20;     // global variable
void function1(){
int x = 10;     // local variable
}

3. Static Variable

A variable that is declared with the static keyword is called a static variable. Static variable remains in memory throughout the span of the program.
Static variables are initialized to 0 if not initialized explicitly. In C, static variables can only be initialized using constant literals. It retains its value between multiple function calls.

void function1(){
int x = 10;     // local variable
static int y = 10;;     // static variable
x = x + 1;
y = y + 1;
printf("%d, %d", x, y);
}

If we call this function many times, the local variable will print the same value for each function call, e.g. 11, 11 ,11 and so on. But the static variable will print the incremented value in each function call, e.g. 11, 12 ,13, and so on.

4. Automatic Variable

All variables in C that are declared inside the block, are automatic variables by default. We can explicity declare an automatic variable using auto keyword.

void main(){
int x = 10;     // local variable (also automatic)
auto int y = 20;;     // automatic variable

5. External Variable

We can share a variable in multiple C source files by using an external variable. To declare an external variable, we need to use extern keyword.

myfile.h

extern int external_variable = 10;     // external variable (also global)

C file

#include "myfile.h"
#include <stdio.h>

void printValue(){
    printf("External variable : %d", external_variable);
}

Comments

Post a Comment