Tuesday 22 January 1985

Different type of variable in C

 


  1. Variable is known as memory location.
  2. It is used to store data.
  3. Its value can be changed and can reused many times.
Syntax
  1. type variable_list;  
Example
  1. int a;
  2. char b;
  3. float c;

Rules for defining variables

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

Types of Variables in C

  1. local variable
  2. global variable
  3. static variable
  4. automatic variable
  5. external variable

1. Local variable

  • Variable that is declare inside the function or block is known as local variable.
  • It is also known internal variable.
  • You must have to initialize the local variable before it is used.
Syntax
  1. void function()
  2. {
  3. int a;
  4. }

2. Global variable

  • Variable that is declare outside the function or block is known as global variable.
  • It is also known as external variable.
  • Syntax
  1. int a;
  2. void function()
  3. {
  4. a=10;
  5. }

3. Static variable

  • Variable that is declare using static keyword is known as static variable.
  • Static variables can be defined inside or outside the function.
  • Static variable value can not be changed during the execution of the program.
  • Syntax
  1. void function()
  2. {
  3. static int a=10;
  4. }

4. Automatic variable

  • Variables that can be defined using keyword auto.
  • Variables allocate memory automatically.
  • Syntax
  1. void function()
  2. {
  3. auto int a=10;
  4. }

5. External variable

  • Variable that is accessible in multiple c source file .
  • We can declare external variable using external keyword.
  • Syntax
  1. external_var.h
  2. external int a=20;
  • Example
#include <external_var.h>
#include <stdio.h>
  1. void function()
  2. {
  3. printf("External variable %d",a);
  4. }

Share This
Previous Post
Next Post

0 Comments: