Variable Declaration in C Programming

In this post, we will learn about variable declaration in c. We will see how to assign values to variable, how to declare constant variable, volatile variable in c etc.

Getting Started

Variable declaration in c specify two things –

  1. It tells compiler what the variable name is.
  2. It specifies what type of data the variable will hold.

1. Variable Declaration in C

  • A variable can be used to store a value of any data type. That is, the name has nothing to do with its type.
  • The declaration of variables must be done before it is used in the program.
Syntax for declaring a variable in c:
data-type v1,v2,....vn;

Here, v1,v2,…vn are the names of variables. Variables are separated by commas. A declaration statement must end with a semicolon.

Valid variable declaration in c are:

int number;       // number is a variable of Integer Type
float marks;      // marks is a variable of float Type
char first_name;      // first_name is a variable of Character Type

Example to understand declaration of variables

main()  // Program Start 1
{
  //Declaration of Variable
  int number;
  float marks;
  char first_name;
  double x;
  // Computation
  ........
  ........
} // Program Ends

2. Assigning Values to Variables or Variable Initialization

Values can be assigned to variables using assignment operator ‘=’.

Example:

number = 20;

marks = 80.5;

Program to understand Declaration of Variables and Assigning Values to Variables

#include<stdio.h>

int main()

{

  /* Declaration of  Variables */

  int x;

  char a;

  float marks;

  /* Declaration and Variable Initialization*/

  int y = 123;

  float i = 1.425;

  char b = 'b';

  /*Assigning Values or  Variable  Initialization*/

  x = 456;

  a = ’A’;

  marks = 98.99;

  /*Printing*/

  printf("x = %d\n",x);

  printf("y = %d\n",y);

  printf("a = %c\n",a);

  printf("b = %c\n",b);

  printf("i = %f\n",i);

  printf("marks = %f\n",marks);

  return 0;

}

Output:

x = 456

y = 123

a = A

b = b

i = 1.425000

marks = 98.989998



3.1 Variable Declaration in c as Constant

  • Sometimes,We want the value of certain variables to remain constant during the execution of a program.
  • We can achieve this by declaring the variable with the qualifier const at the time of initialization.

Syntax

const data_type variable_name = value;

Example

const int class_size = 40;

const is a new data type qualifier that tells the computer that the value of the int variable class_size must not be modified by the program.

3.2 Declaring a Variable as volatile

volatile is another qualifier that could be used to tell explicitly the compiler that a variable’s value may be changed at any time by some external sources (from outside the program).

Syntax

volatile data_type variable_name;

Example

volatile int date;

Some keypoints to remember –

  • When we declare a variable as volatile, then the compiler will examine the value of a variable each time it is encountered to see whether any external alteration has changed the value.
  • The value of a variable declared as volatile can be modified by its own program as well.
  • If we wish that the value must not be modified by the program while it may be altered by some other process, then we may declare the variable as both const and volatile as shown below:
volatile const int location = 100;

Leave a Reply