Variables

1. What is Variable? ⇒ In computer programming, a variable is a...

1. What is Variable?

⇒ In computer programming, a variable is a container which holds some data. As the name suggests “variable” which means it is an entity which values changes. The value of the variable can be changed during the execution.

Before used in the program it must be declared. Declaration of variables specify their name, data types and range of the value that variables can store depends upon their data types.

Example – 

int a;
char value;
float data;

2. What is the naming rule of a variable in C?

A variable name may consist of letters, digits and the underscore (_) characters.

A variable name must begin with a letter. Some systems allow the variable name to start with an underscore as the first character.

ANSI standard recognizes a length of 31 characters for a variable name. However, the length should not normally be more than any combination of eight alphabets, digits, and underscores.

The variable name may not be a C reserved word (keyword).

Examples: int age = 19;


3. How many types of variables are there in C?

⇒ There are mainly three types of variable available in C programming language, Local variable, Global variable, Environment variable let’s discuss one by one briefly.

1. Local variable: –

  • The scope of local variables will be within the function only.
  • These variables are declared within the function and can’t be accessed outside the function.

#include<stdio.h>
void test ();
int main ()
{
int m = 22, n = 44;
// m, n are local variables of main function

/*m and n variables are having scope within this main function only.
These are not visible to test function. */

/* If you try to access a and b in this function,
you will get 'a' undeclared and 'b' undeclared error */

printf("nvalues : m = %d and n = %d", m, n);
test ();
}

void test ()
{
int a = 50, b = 80;

// a, b are local variables of test function

/*a and b variables are having scope
within this test function only.
These are not visible to the main function. */

/* If you try to access m and n in this function,
you will get 'm' undeclared and 'n' undeclared
error */

printf("nvalues : a = %d and b = %d", a, b);
}

Output ⇒ 
values : m = 22 and n = 44
values : a = 50 and b = 80


2. Global variable: –

  • The scope of global variables will be throughout the program. These variables can be accessed from anywhere in the program.
  • This variable is defined outside the main function. So that, this variable is visible to the main function and all other sub functions.

#include<stdio.h>
void test();int m = 22, n = 44;
int a = 50, b = 80;

int main()
{
   printf("All variables are accessed from main function");
   printf("nvalues: m=%d:n=%d:a=%d:b=%d", m,n,a,b);
   test();
}

void test()
{
   printf("nnAll variables are accessed from" 
   " test function");
   printf("nvalues: m=%d:n=%d:a=%d:b=%d", m,n,a,b);
}

Output ⇒ 
All variables are accessed from main function
values : m = 22 : n = 44 : a = 50 : b = 80
All variables are accessed from test function
values : m = 22 : n = 44 : a = 50 : b = 80


3. Environment variable: –

  • Environment variable is a variable that will be available for all C applications and C programs.
  • We can access these variables from anywhere in a C program without declaring and initializing in an application or C program.
  • The inbuilt functions which are used to access, modify and set these environment variables are called environment functions.
  • There are 3 functions which are used to access, modify and assign an environment variable in C. They are,
  1. setenv()
  2. getenv()
  3. putenv()

#include <stdio.h>
#include <stdlib.h>

int main()
{
printf("Directory = %sn",getenv("DIR"));
return 0;
}

Output ⇒ /usr/bin/test/