Introduction to variables in C

A variables in C as like as other programming language can be compared as a container. It can hold a data inside it. We have to declare a name for the variable which represents the memory location. We can give any name to a variable but there are some rules for naming variables in C.

Some most common rules for naming variables in C

A variable should be declared by the type of the data it can hold followed by the name of the variable.

  • It can contain only uppercase and lowercase letter, digits and underscore.
  • First letter can not be a digit. It must be a letter or underscore.
  • We can not use any keyword as a variable name. (We will learn keywords in C later)
  • No spaces are allowed inside the name of a variable.
  • Variable name should always exist in the left side of the assignment operator.

Now, lets see the syntax of variables in C and some general mistakes which people make when declaring a variable.

DataType variable_name;   //syntax of a variable

int a;         // integer type variable
char myChar;    //character type variable
float height;    // floating type variable
double weight;    // double type variable

We can also assign a value when declare a variable. See the following example of declaring a variable.

int a = 10;
char myChar = 's';
float height = 5.10;
double weight = 68.325;

People normally make an error in declaration of a variables. These errors will prevent the program from being compiled and run. Lets see some of such error of declaring a variable.

int number 5;   // correct integer variable
number = 8.2;    // incorrect assigning floating number inside integer
double number;    // we can not change the data type of a variable

int long;    // incorrect, long is a keyword
int a b;    // incorrect, seperate by comma if declare more than one variable
int my variable;    // incorrect use of space inside its name
float 2ndFloat;    // incorrect use of number at first letter
int @name;     // incorrect special character to variable name

Five main types of variables in C

Five main types of variables in C which we will use frequently is given bellow;

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

Local variable : A local variable is declared inside a block or function. Generally, it can’t work outside that block or functions. You have to initialize the variable before use and it must be declared at the starting of that block or functions. Let’s see the example of local variable bellow.

void myFunction(){  
int i = 20;             // here i is a local variable  
} 

Global variable : The variables which are declared outside the function or block is called global variable. Every function uses a global variable. A global variable can change the value of any function you use in your code. So, be careful to declare global variable. It has to declare at the start of the block. Let’s see an example of global variables in C.

int i = 10;           // i is a global variable  

void myFunction(){  
int num = 301;        // num is a local variable  
}  

Static variable : The variable which is declared with static keyword is called static variable. When you will call the following function in several times, the local variable “i” will gives the same output as 21, 21, 21…… where the static variable “j” will print the incremented value like 21, 22, 23…….

void myFunction(){

int i = 20;              //i is a local variable  
static int j = 20;       //j is a static variable

i = i + 1;
j = j + 1;

printf("%d,%d", i, j);
}

Automatic variable : Every variable we use inside a block or functions is automatic variable. They are automatic by default. We can also declare them by auto keyword. Let’s see the example of automatic variable bellow.

void myFunction(){  
int a = 5;        // a is both local and automatic varibale here 
auto int b = 10;   // b is an automatic variable here
}  

External variable : External variable can share in multiple C source file. We have to use an external keyword to declare an external variable. Let’s see the example bellow of external variable.

extern int global_variable = 322;      // external and global variable
#include "external.h"
#include <stdio.h>

void printingValue(){
    printf("%d", global_variable);
}

int main(){
printingValue();    // calling function
return 0;
}

Previous page:  First C program

Next pageData types in C