Constant and Variables

0 mariloufoster62 November 13, 2020

Variables are the symbolic name which is used to store data item, i.e. a numerical quantity or a character constant. Variables are defined in a computer to represent an item of data input by the user,  any intermediate calculations, or the ends results. Unlike a constant, the value of variables can change during the execution of the program. The same variable can store different values at different portions of a program.

Variables name may consist of letters, digits, or underscore characters.

Variable Declaration:

Any program should be defined before using it in a program. The variables are defined or declared using the following syntax:

data-type variable_name = value;

For Example:

int num = 20;
float num = 20;

Rules For Variable Declaration:

  • The variable name should be started with only letters or underscore.
int ab;       //valid
int 1a;      //invalid
  • The variable name should not be a keyword.
int if;           //invalid as if as keyword
init float;   //invalid as float as keyword
  • White spaces are not allowed between characters of variables, but underscores are allowed.
float age of students;      // invalid
float age_of_student;     // valid
  • The variable name is case sensitive, i.e uppercase and lowercase are different. Thus variable temp is not the same as Temp or TEMP.
  • No two variables of the same name are not allowed to be declared in the same scope.

Types of Variable:

Here we have variables category on two bases. Firstly we categorized on the basis of Data Types:

  • int: Variables that hold an integer value
  • char: hold characters such as ‘A’, ‘b’, ‘V’
  • bool: Store a boolean value i.e true or false
  • double: hold a double-precision floating-point value
  • float: Store double-precision floating-point value

Next, We have categorized variables on the basis of scope.

  • Global Variables
  • Local Variables

Global Variables:

The global variables are those variable which are declared outside the function and can be accessed throughout the program. Let’s take an example to clarify this statement.

#include <stdio.h>

// This is the global variabe that is declared outside the function
char chp = 'Y';

int main(){

    printf("Value of chp => %d", chp);

    //Now we assign another value to chp
    chp = 'N';

    printf("Value of chp => %d", chp);

The output of the above program is:

Value of chp => Y
Value of chp => N

In the above program, We have declared a variable chp outside the function but also we access that two times without any issues.

Local Variables:

Local Variables are those variable which are declared inside the user-defined function, main loop, or any control statement that have limited score inside the braces. Let’s take an example to clarify this statement.

#include <stdio.h>

char local_variable(){
    // This is the local variabe that can be access inside this function only
    char chp = 'Y';

int main(){

    printf("Value of chp => %d", chp);
    return 0;

If we compile the above program then we will get an error because we have declared the chp as a local variable so that we can use chp variable inside the local_variable function only. If we need that variable in the main function also, we have to declare inside the main function separately or we can declare the variable globally.