Data Types, Variables and Constants

Variables in Programming Language

In a program, the data is stored in 2 ways – Either the data is already stored in the program, or the data comes from the user. The data is stored in both ways. The program stores this data in the form of Variables. Every variable has its own data type, a name, and a value assigned to it. The value of that variable might change as time goes by, and hence the name Variable. Let’s look at the concept of Variables in more detail.

Definition

Variables are names given to computer memory locations in order to store data in a program. This data can be known or unknown based on the assignment of value to the variables.

Variables can also be considered as ‘containers’ which are used to hold more than one value. Their sole purpose is to label and store data in the memory and then this Variable can be accessed throughout the program whenever needed.

Generally, the variable name is used as a reference to access the stored value.

Creating Variables

The process of creating a variable is also known as Declaration of Variable. Every programming language has its own way of declaring a variable. Declaration of a variable does 2 things :

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

In general, the declaration consists of 3 parts – the variable type, variable label/name, and semi-colon to end the declaration.

Syntax – data_type variable_name = value ;

For example –

int speed;

float sum = 0;

There are a few conventions which are needed to be followed while declaring 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. Do not use a comma with numbers.
  4. Once a data type is defined for the variable, then only that type of data can be stored in it. For example, if the variable is declared as Int, then it can only store integer values.
  5. A variable name once defined can only be used once in the program. You cannot define it again to store other types of value.
  6. Built-in Data types are frequently used to declare the data type of the Variable.

Browse more Topics Under Data Types, Variables and Constants

Assigning Values to Variables

  • We use the  =  sign to assign values to the declared variables. The variable name and type will come on the left side, while the value to be stored will be on the right side.

For example – int score = 45;

  • You can also get the value to be stored in the variable through user input methods.

For example, in C++ language –

                    

int main()

{

int x;

cout << “Enter a number :”;

cin >> x;

cout << “User entered number is  ” << x;

}

Output –

Enter a number: 5

User entered number is 5

  • If you assign a new value to an existing variable, it will overwrite the previous value and assign the new value.
                    

For example, in Java language –

int number = 15;

number = 10;

System.out.println(number);

Output –

10

 

Variables

Types of Variables

There are a total of 5 different variable types in the programming language. Those are as follows:

  1. Constant Variables 

Constant Variables are used to store data which is not needed to be changed throughout the program. The data cannot be modified in such a type of variable.

  1. Global Variables 

Global variables are declared outside of a function and can be accessed from any part of the program until the execution. In Ruby, the $ sign is used before the variable name to make it global whereas in Python, the ‘global’ keyword is used.

  1. Class Variables 

These variables are accessed within the class only. Class variables must be declared at the class level. They are also called Static Variables as there can only be one copy of each class variable per class.

  1. Instance Variables 

Instance variables are declared inside the class, but outside a method.

  1. Local Variables 

They are the most commonly used Variables that are declared in methods, class, instances. These variables can be accessed from within a block/part of the code.

FAQs on Variables in Programming Language

Q1. Which of the following is true for variable names?

  1. Variable names cannot start with a digit
  2. Variable can be of any length
  3. Reserved Word can be used as a variable name
  4. Can contain alphanumeric characters as well as special characters

Answer. Option A.

Q2. An external variable is?

  1. Also called a global variable
  2. Can be accessed by all functions
  3. Has a declaration
  4. All of these

Answer. Option D.

Q3. Which of the following is not a valid variable name declaration?

  1. int number;
  2. float speed;
  3. double rate;
  4. int 2_main;

Answer. Option D.

Q4. Which of the datatypes have a size that is variable?

  1. struct
  2. int
  3. float
  4. double

Answer. Option A

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
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.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
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.