Variables in C

Variables in C Program

In this article, I am going to discuss Variables in C Program with examples. Please read our previous article, where we discussed the Operators in C program. As part of this article, you will learn what are Variables in C, it’s type, and when and how to use Variables in C Program with examples.

Variables in C:

A name that is given for any computer memory location is called a variable name. The purpose of the variable is to store some data. The user will access by the variable name and the compiler will access by the address. 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 a change in the program. C variable might be belonging to any of the data types like int, float, char, etc.

Note: In C Language, variables are the data objects which are used to store a value. These values can be then manipulated during the execution of the program. You must declare the variable before using it in your program.

Rules for variable declaration in C:
  1. A variable name must begin with a letter or underscore.
  2. Variables are case sensitive
  3. They can be constructed with digits, letters.
  4. No special symbols are allowed other than underscores.
  5. sum, height, _value are some examples for variable name
Declaring and 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 the variable definitions.
  • Variable initialization means assigning a value to the variable.
How to Declare a Variable in C?

Declaration tells the compiler about data type and size of the variable. Variable can be declared many times in a program. The variable declaration specifies the name and type of the variable. 

Syntax: data_type variable_name;
Example: int x, y, z;

Here x, y, and z are variable names and ‘int‘ is the datatype which specifies that x, y, and z can only store integer type values. In our next article, we will discuss DataTypes in detail.

When we declare a variable in C#, by default it stores some garbage value. Here, garbage value means an unexpected value (it might be zero also). So after the declaration, when we assign some value to the variable. It is called Variable Initialization. Let us see how to initialize a variable in C.

Defining a Variable in C:

When we declare a variable in C#, by default it stores some garbage value. Here, garbage value means an unexpected value (it might be zero also). So after the declaration, when we assign some value to the variable. It is called Variable Initialization.

Syntax: data_type variable_name;
              variable_name = value;
Example: int x, y;
                x = 50, y = 30;   //Here x is defined with a value 50 and y is defined with a value 30

Why do we get garbage value by default in C?

Let us understand this with an example. When we declare an integer variable in C, then 4 bytes of memory will be allocated for it. If there were already 0’s in those 4 bytes then there will be no problem, else if the 4 bytes contain info which we have used earlier then it will be garbage. so we need to clear it. It can be done by initializing the variable with 0.

Variable initialization in C:

Defining a variable with a value at the time of its declaration is known as variable initialization in C. So, we can say that Declaration + Definition = Initialization;

Syntax: data_type variable_name = value;
Example: int x = 0;

Note: It is always a good programming practice to initialize a variable with zero after declaration.

Types of Variables in C Language:

Based on the scope and lifetime of a variable, variables categorized into three types. They are as follows

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

Note: The Scope tells about the visibility (i.e., from where this variable is visible), whereas lifetime tells about the durability (i.e. how much time the value in the variable is valid).

LOCAL VARIABLE IN C:

The variables declared inside a function is known as a local variable in C.

The scope of local variables will be within the function only i.e. we can’t access a local variable from outside the function in which it is declared. These variables are declared within the function and can’t be accessed outside the function.

The Life-time of a local variable is throughout the function i.e. memory to the local variables allocated when the function execution started and will become invalid once the function completes its execution.

Example:

In the following example, m and n variables are having scope within the main function only. These are not visible to test function. Likewise, a and b variables are having scope within the test function only. These are not visible to the main 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 ("\n values : 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 main function.*/
/* If you try to access m and n in this function, you will get 'm' undeclared and 'n' undeclared error */
  printf ("\n values : a = %d and b = %d", a, b);
}

OUTPUT:

LOCAL VARIABLE IN C

GLOBAL VARIABLE IN C:

The variables which are declared outside the function are known as global variables in C.

The scope of global variables will be throughout the program. These variables can be accessed from anywhere in the program.

The Life-time of a global variable is throughout the program, i.e. memory to the global variables will be allocated when the execution of the program is started and will become invalid after finishing the execution of the program.

Example:

In the following example, the variables a, b, m, and n are declared outside the scope of any function. So, these variables are 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("\n values: m=%d: n=%d: a=%d: b=%d", m, n, a, b);
   test();
}
void test() 
{
   printf("\n\n All variables are accessed from" \ " test function");
   printf("\n values: m=%d: n=%d: a=%d: b=%d", m ,n, a, b);
}

OUTPUT:

GLOBAL VARIABLE IN C

ENVIRONMENT VARIABLES IN C:

The 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 that are used to access, modify, and assign an environment variable in C. They are,

1. setenv()
2. getenv()
3. putenv()

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/”.

PROGRAM:

#include <stdio.h>
#include <stdlib.h>
int main() {
  printf("Directory = %s\n", getenv("DIR"));
  return 0;
}

OUTPUT:

GETENV() FUNCTION IN C

SETENV() FUNCTION IN C:

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

PROGRAM:

#include <stdio.h>
#include <stdlib.h>
int main()
{
   setenv("FILE","/usr/bin/example.c",50);
   printf("File = %s\n", getenv("FILE"));
   return 0;
}

OUTPUT:

File = /usr/bin/example.c

PUTENV() FUNCTION IN C:

This function modifies the value of the environment variable. The below example program shows how to modify an existing environment variable value.

PROGRAM:

#include <stdio.h>
#include <stdlib.h>
int main()
{
   setenv("DIR","/usr/bin/example/",50);
   printf("Directory name before modifying = " \ "%s\n", getenv("DIR"));
 
   putenv("DIR=/usr/home/");
   printf("Directory name after modifying = " \ "%s\n", getenv("DIR"));
   return 0;
}

OUTPUT:

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

Interview Questions:
Question 1: What will be the output of the below program
#include <stdio.h>
int main()
{
    printf("%d", number);
    int number; 
    return 0;
}

Output: Compilation Error. This is because the scope of the local variable starts from the point where we declared. In the above example, we are trying to access the variable before we are declaring it.

Question 2: What will be the output of the below program?
#include <stdio.h>
int main()
{
 int number = 10;
 int number = 20;
 printf("%d", number);
 return 0;
}

Output: Compilation Error. This is because Multiple declarations of the local variables with the same name and in the same scope are not allowed. In the above example, we are trying to declare a Local variable with the same name more than once

Question 3: What will be the output of the below program?
#include <stdio.h>

int main()
{
    printf("%d", number);
    return 0;
}

int number = 10;

Output: undeclared variable error. This is because the scope of the global variable also starts from the point where it is declared. In the above example, we are declaring the Global variable after we are using it in the “main” function.

Question 4: What will be the output of the below program?
#include <stdio.h>

int number = 10;
number = 20;

int main()
{
    printf("%d", number);
    return 0;
}

Output: re-definition error. This is because the Re-definition of Global variables is not allowed in C language. In the above example, we are trying to redefine the Global variable with number= 20.

Question 4: What will be the output of the below program?
#include <stdio.h>

int number1 = 10;
int number2 = number1 ;
int main()
{
    printf("%d", number1);
    return 0;
}

Output: Compilation Error. This is because we cannot assign directly one “Global variable” to another “Global variable” outside the function. In the above example, we are trying to assign the Global variable number1 to another Global variable number2

In the next article, I am going to discuss Data types in C Program with examples. Here, in this article, I try to explain Variables in C Program in detail with examples and I hope you enjoy this article. I would like to have your feedback. Please post your feedback, question, or comments about this article.

Leave a Reply

Your email address will not be published. Required fields are marked *