What are Variables and Constants? 

Constants and variables are widely used in mathematical equations and expressions. They are also commonly found in the domain of programming, where each language has its own way to differentiate between a constant and a variable. Since they have all sorts of applications, knowing their differences is a good place to start.  


Constants are values that don’t undergo any change. They are used in algebraic expressions and do not change with respect to time (fixed values). Some real-world examples would be the number of pages in a physical book or the number of words in a newspaper. These values never change no matter how much time has passed.

A frequently used constant in mathematical calculations is pi π .

In programming, constants refer to variables whose values cannot be changed once they are declared. They remain unalterable throughout the program’s execution.  


A variable is, however, a changeable value. An everyday example would be the variables used to measure weather, such as air temperature, pressure, and density. These variables can vary continuously with respect to time.  

In programming, a variable is an entity that stores some type of data. Though, this data can take on many forms throughout the program’s execution. Depending on the type of the variable, it has a fixed-size memory assigned to it. A programmer can perform a host of operations based on the data-type of the variable they encounter.  

Types of Constants   

Constants can be classified into three types: 

  • Literal constants 
  • Qualified constants 
  • Symbolic constants  

Literal constant: 

They refer to a fixed value of the type integer, floating-point, character, or string.  

An integer literal stores values like 2, 3, -5, and so on. An integer literal cannot have a decimal and must not contain any special characters.  

A floating-point literal contains values like -2.5, 5.6, -1.2e5, and so on. They can be written as a fraction or an exponent. In fractional form, they are written with a decimal point. It can either be positive or negative. But, in the exponential form, it has two parts: a mantissa and the exponent part. They are both separated by an e. The mantissa can have a decimal point and must be more than one digit. It can carry a positive or negative sign.  

A character literal can have one (at most two) characters stored inside of it with single quotes. E.g.: ‘F’, ‘f’, ‘n’, etc.  

A string literal consists of a series of characters in double-quotes. E.g.: “Computer” 

Qualified Constants:  

The keyword const is used to create qualified constants. E.g.: const int age= 5. This statement declares the variable ‘age’ as a constant. So, the value ‘5’ is unchangeable throughout the program.  

Symbolic Constants: 

The preprocessor directive #define is used to create symbolic constants. E.g.: #define PI 3.14124. During the preprocessing stage, a symbolic constant is replaced by the value declared in it.  

An Expanded Look at Variables

In a program, the three main steps to include a variable are:  

Declaration: This is the method of marking a space for a variable in the system’s memory. Constants must also be declared.  

Assignment: Involves assigning a value to the variable. 
Initialization: Initialization is declaring and assigning a value to the variable in one statement of the program.  


Identifiers are otherwise known as the name of the variable. It is a unique label that references a variable and provides the programmer access to it. This is so that the programmer can read the value of the variable, write something into it, or modify its existing value.  

It is important to establish a clear and relevant name for the identifier. The name of the variable could signify the kind of data it is holding. For instance, if the variable is titled studentAge= 18, it is easy to understand that the value 18 denotes the student’s age. While a programmer has the flexibility to name a variable as they please, the preferred method is to come up with a meaningful identifier that reveals the value held by the variable.  

Here are three conventions followed while coming up with identifiers:  

  • Capitalization: The word ‘age’ is not the same as ‘Age’ or ‘AGE’. All of these may indicate a different variable. So it’s important to stick to one form of capitalization throughout the program.  
  • Format: If your identifier is made up of more than one word, do not add a space in between the word. Instead, opt for an underscore or camel casing. Eg: studentAge or student_age 
  • Numbers: Variable identifiers do not start with numerical values.  

Data types 

A variable can be of many types. The size of the variables can also vary based on their types. Here are the commonly used data types and their sizes, as seen in C++:  

The data types to know are: 

  • Integer (or int): Used to store numbers without decimals. They are usually 4 bytes in length. 
  • Float (or Real): Used to store fractions or numbers with decimals. They are usually 4 bytes in length. 
  • Character (or char): Used to store single letters. Upper and lower case alphabetic characters are recognized. They can store 1 byte of data.  
  • String (or str): Used to store a sequence of characters such as letters, special characters, numbers, and so on.  
  • Boolean (or bool): Used to store logical data (like True or False). They usually store 1 bit of data. 


The scope of a variable refers to the part of the program in which the variable can be used.      In other words, it refers to the visibility of a variable. Usually, variables have global scope (available anywhere in the program). But there are variables with local scope as well (when they are declared inside a function or block). Local variables can only be used in the statements present inside their own function. 

To summarize, these are the differences between variables and constants:  

Constants Variables 
Constants cannot be changed. Its remains the same throughout a program’s execution.  Variables can be changed. They can undergo modification throughout the program’s execution.  
Its value is fixed. Its values vary. 
It is similar to a variable, but once defined, it cannot be changed. It is also a storage area to contain data, given a name called an identifier. 

Context and Applications    

This topic is significant in the professional exams for both undergraduate and graduate courses, especially for  

  • Bachelor’s in computer science 
  • Master’s in computer science   

Want more help with your computer science homework?

We've got you covered with step-by-step solutions to millions of textbook problems, subject matter experts on standby 24/7 when you're stumped, and more.
Check out a sample computer science Q&A solution here!

*Response times may vary by subject and question complexity. Median response time is 34 minutes for paid subscribers and may be longer for promotional offers.

Search. Solve. Succeed!

Study smarter access to millions of step-by step textbook solutions, our Q&A library, and AI powered Math Solver. Plus, you get 30 questions to ask an expert each month.

Tagged in
EngineeringComputer Science


Fundamentals of Programming

Constants and Variables

Variables and Constant Homework Questions from Fellow Students

Browse our recently answered Variables and Constant homework questions.

Search. Solve. Succeed!

Study smarter access to millions of step-by step textbook solutions, our Q&A library, and AI powered Math Solver. Plus, you get 30 questions to ask an expert each month.

Tagged in
EngineeringComputer Science


Fundamentals of Programming

Constants and Variables