Data Types, Variables and Constants

Declaration of Variables

Variables are the basic unit of storage in a programming language. These variables consist of a data type, the variable name, and the value to be assigned to the variable. Unless and until the variables are declared and initialized, they cannot be used in the program. Let us learn more about the Declaration and Initialization of Variables in this article below.

What is Declaration and Initialization?

  • Declaration of a variable in a computer programming language is a statement used to specify the variable name and its data type. Declaration tells the compiler about the existence of an entity in the program and its location. When you declare a variable, you should also initialize it.
  • Initialization is the process of assigning a value to the Variable. Every programming language has its own method of initializing the variable. If the value is not assigned to the Variable, then the process is only called a Declaration.

Basic Syntax

The basic form of declaring a variable is:

            type identifier [= value] [, identifier [= value]]…];


            data_type variable_name = value;


type = Data type of the variable

identifier = Variable name

value = Data to be stored in the variable (Optional field)

Note 1: The Data type and the Value used to store in the Variable must match.

Note 2: All declaration statements must end with a semi-colon (;)



int a, b, c;                       // declare 3 variables.

int z = 35;                      // declare and initialize variable z with value 35.

double pi = 3.14159;   // declare an approximation of pi.

char x, y = ‘y’, z;           // declare 3 variables, initialize char y with value ‘y’.

char str[30];                 // declare an array named ‘str’ which holds 30 characters.

Browse more Topics Under Data Types, Variables and Constants

Rules to Declare and Initialize Variables

There are few conventions needed to be followed while declaring and assigning values to the Variables –

  1. Variable names must begin with a letter, underscore, non-number character. Each language has its own conventions.
  2. Few programming languages like PHP, Python, Perl, etc. do not require to specify data type at the start.
  3. Always use the ‘=’ sign to initialize a value to the Variable.
  4. Do not use a comma with numbers.
  5. Once a data type is defined for the variable, then only that type of data can be stored in it. For example, if a variable is declared as Int, then it can only store integer values.
  6. A variable name once defined can only be used once in the program. You cannot define it again to store another type of value.
  7. If another value is assigned to the variable which already has a value assigned to it before, then the previous value will be overwritten by the new value.

Types of Initialization

  1. Static Initialization –

In this method, the variable is assigned a value in advance. Here, the values are assigned in the declaration statement. Static Initialization is also known as Explicit Initialization.

Example –


int a;

a = 5;

int b = 10;

int x = 4, y = 5;

  1. Dynamic Initialization –

In this method, the variable is assigned a value at the run-time. The value is either assigned by the function in the program or by the user at the time of running the program. The value of these variables can be altered every time the program runs. Dynamic Initialization is also known as Implicit Initialization.

Example –

In C programming language –


int speed;

printf("Enter the value of speed");

scanf("%d", &speed);

In Java Programming Language –


class DynInit {

public static void main (String args[])


// a is dynamically initialized

double a = Math.sqrt(25);

System.out.println(“Sqrt of 25 is: ” +a );




Sqrt of 25 is: 5

FAQs on Declaration of Variables

Q1. Is the following statement a declaration or definition?

extern int i;

  1. Definition
  2. Declaration
  3. Error
  4. None

Answer – Option B

Q2. Which declaration is correct?

  1. int length;
  2. float double;
  3. float long;
  4. long int;

Answer – Option A, double, long and int are all keywords used for declaration and keywords cannot be used for declaring a variable name.

Q3. Which statement is correct for a chained assignment?

  1. int x = 5;
  2. int x, y = 10;
  3. int x = y = 10;
  4. Both B and C

Answer – Option C

Share with friends

Customize your course in 30 seconds

Which class are you in?
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
Ashhar Firdausi
IIT Roorkee
Dr. Nazma Shaik
Gaurav Tiwari
Get Started

Leave a Reply

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

Download the App

Watch lectures, practise questions and take tests on the go.

Customize your course in 30 seconds

No thanks.