x

C – Variable

Prev     Next

  • C variable is a named location in a memory where a program can manipulate the data. This location is used to hold the value of the variable.
  • The value of the C variable may get change in the program.
  • C variable might be belonging to any of the data type like int, float, char etc.

Rules for naming C variable:

  1. Variable name must begin with letter or underscore.
  2. Variables are case sensitive
  3. They can be constructed with digits, letters.
  4. No special symbols are allowed other than underscore.
  5. sum, height, _value are some examples for variable name

Declaring & initializing C variable:

  • Variables should be declared in the C program before to use.
  • Memory space is not allocated for a variable while declaration. It happens only on variable definition.
  • Variable initialization means assigning a value to the variable.
 Type 
Syntax
Variable declaration
data_type variable_name;
Example: int x, y, z; char flat, ch;
Variable initialization
data_type variable_name = value;
Example: int x = 50, y = 30; char flag = ‘x’, ch=’l’;

There are three types of variables in C program They are,

  1. Local variable
  2. Global variable
  3. Environment variable

1. Example program for local variable in C:

  • 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.
  • In the below example, m and n variables are having scope within the main function only. These are not visible to test function.
  • Like wise, a and b variables are having scope within the test function only. These are not visible to main function.

Output:

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

2. Example program for global variable in C:

  • 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 main function and all other sub functions.

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 variables in C:

  • 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()



Example program for getenv() function in C:

This function gets the current value of the environment variable. Let us assume that environment variable DIR is assigned to “/usr/bin/test/”.

Output:

/usr/bin/test/

Example program for setenv() function in C:

This function sets the value for environment variable. Let us assume that environment variable “FILE” is to be assigned “/usr/bin/example.c”

Output:

File = /usr/bin/example.c

Example program for putenv() function in C:

This function modifies the value for environment variable. Below example program shows that how to modify an existing environment variable value.

Output:

Directory name before modifying = /usr/bin/example/
Directory name after modifying = /usr/home/

Difference between variable declaration & definition in C:

Variable declaration
Variable definition
Declaration tells the compiler about data type and size of the variable.
Definition allocates memory for the variable.
Variable can be declared many times in a program.
It can happen only one time for a variable in a program.
The assignment of properties and identification to a variable.
Assignments of storage space to a variable.

Prev     Next



Like it? Please Spread the word!