Variables In C

In any programming language, a variable is a name assigned to a storage location that has some memory allocated to it. A variable is required to store data.

Values given to these memory locations can be changed, therefore these are called variable names. Different types of variables are allocated different amounts of memory.

Rules for Constructing Variable Names are as follows

  • A variable name is a combination of alphabets (both uppercase and lowercase), digits, and underscores. Some compilers allow variable names with lengths up to 247 characters. While you should try to not go more than 31 characters. Creating unnecessarily long variables is inconvenient both to the programmer and reader.
  • The variable name must start with an alphabet or underscore.
  • Commas or blanks spaces are not allowed within a variable name.
  • No special symbol apart from an underscore (as in net_sal) can be used in a variable name.
  • No reserved word can be used as a variable name.

Declaring A variable In C

  1. Single variable: type var_name;
  2. Multiple variable: type var_name1, var_name2, var_name3;
#include <stdio.h>

int main() {
  int a; // declaring a variable
}

Defining A Variable In C

Defining a variable means assigning a value to a memory location assigned to the variable.

#include <stdio.h>

int main() {
  int a; // declaring a variable
  a = 4; //defining a variable;
}

Types of Variables in C

1. Local Variable

If a variable is declared inside a function or a block, it is called a local variable of that function or the block. It cannot be accessed outside that function or the block.

Example –

#include <stdio.h>

void
function () {
  int y = 50; // local variable
}
int main() {
  function ();
}

In the above code y can be used only in the scope of function(). We cannot use y in main() function.

2. Global Variable

If a variable is declared outside any function or a block, it is called a global variable. By declaring the global variable at the starting of the function we can access it from all functions.

Example – 

#include <stdio.h>

int y = 10; //global variable
void function1() {
  printf("%d\n", y);
}
void function2() {
  printf("%d\n", y);
}
int main() {
  function1(); //function call
  function2();
  printf("%d\n", y);
  return 0;
}

Output:

20
20
20

As we already know global variables are accessible by all the functions, therefore in the above example y can be accessed by all three functions.

3. Static Variable

If a variable preserves its value between multiple function calls, it is called a static variable. It is declared with the static prefix.

Example- 

#include <stdio.h>

int
function () {
  int x = 20; //local variable
  static int y = 30; //static variable
  x = x + 10;
  y = y + 10;
  printf("\n%d,%d", x, y);
  return 0;
}
int main() {
  function ();
  function ();
  function ();
  return 0;
}

Output:

30,40
30,50
30,60

Therefore local variable will always print the same value whenever the function will be called whereas the static variable will print the incremented value in every new function call.

Do you know?
Variables In C This article is contribute by Hitesh Marwaha If you want share your knowledge on Study4Geeks then go to login section.