C Variables

A variable name is a temporary memory location which is used to store data value. We can change the values stored in a variable while the program is running.

C Variables
C Variables

A variable name is a temporary memory location which is used to store data value. We can change the values stored in a variable while the program is running.

We cannot use any variable without declaring it. The variable name should be meaningful.

Rules for defining Variable Name

  • A variable name must begin with a letter, some systems allow underscore as first character.
  • A variable name may contain letters, digits, and underscore(_) character.
  • ANSI standard recognizes the maximum length of 31 characters. However first eight characters are supported by most of the compilers.
  • White space is not allowed in variable name.
  • Variable name should not be a keyword.
  • Variable name “Hello” is not same as “hello”. Uppercase and lowercase are significant in C variable.

Defining a Variable name

A variable name must be declared before they are used in the program. = operator is used to assign a value to a variable. We can declare variable name in two ways,

  • We should tell the compiler what the variable name is and,
  • What type of data the variable name hold.

Example

#include<stdio.h>
int main()
{
int A;
A = 10;
printf("The value of A is: %d", A);
}

Types of variables in C programming language

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

Local variable

Variable which is defined inside a function or block is called local variables. Local variables cannot be used outside of a function or block.

#include<stdio.h>
void function()
{
    int a = 12;
    printf("The value of A is: %d", a);
}
int main()
{
    function();
}

Output

The value of A is: 12

Global variable

Variable which is declared outside of a function or block is called global variable. The global variable is available to all functions and blocks.

#include <stdio.h>
int a = 10; //Global Variable
void function()
{
    int b = 10, c;
    c = a + b;
    printf(" The sum of the value is: %d\n" , c);
}
int main()
{
  function();
  return 0;
}

Output

The sum of the value is: 20

Static variable

Variables which are declared with static keyword is known as static variable. Static variable returns its value between multiple function calls.

#include <stdio.h>
void function()
{
    int a = 5;
    static int b = 10;
    a = a + 1;
    b = b + 1;
    printf("The value of A and B are: %d,%d \n", a, b);
}
int main()
{
  function();
  function();
  return 0;
}

Output

The value of A and B are: 6,11
The value of A and B are: 6,12

In the above example, when we call the function each time, the local variable print the same value, but the static variable print the incremented value.

Automatic variable

The automatic variable is declared using the auto keyword. Automatic variables are similar to local variables by default and we can declare an automatic variable inside a function or a block.

#include <stdio.h>
void function()
{
  int a = 20, c;
  auto int b = 30;
  c = a + b;
  printf("The sum of the value is: %d", c);
}
int main() {
    function();
    return 0;
}

Output

The sum of the value is: 50

External variable

To declare an external variable extern keyword is used. An external variable is used to share a variable between multiple C files.

example_file.h
extern int a = 10;
example_prog.c
#include "example_file.h"
#include <stdio.h>
void printValue()
{
  printf("The value of A is: %d", global_variable);
}