Skip to main content
Engineering LibreTexts

4.4: Constants and Variables

  • Page ID
    29036
  • Understanding Constants vs Variables

    Various textbooks describe constants using different terminology. Added to the complexity are the explanations from various industry professionals will vary greatly. Let's see if we can clear it up.

    A constant is a data item whose value cannot change during the program's execution. Thus, as its name implies – their value is constant.

    A variable is a data item whose value can change during the program's execution. Thus, as its name implies – their value can vary.

    Constants are used in two ways within C++. They are:

    1. defined constant
    2. memory constant

    Most text books refer to either symbolic constants or named constants but these two refer to the same concept. A symbolic constant is represented by a name similar to how we name variables. Let's say it backwards; the identifier name is the symbol that represents the data item. Within C++ identifier names have some rules. One of the rules says those names should be meaningful. Another rule about using ALL CAPS FOR CONSTANTS is an industry rule. There are two ways to create symbolic or named constants:

    #define PI 3.14159

    Called a defined constant, we have discussed this concept when we talked about preprocessor directives in Lesson 1, because it uses a textual substitution method controlled by the compiler pre-processor command word "define".

    const double PI = 3.14159;

    The second one is called sometimes called constant variable but that name is contradictory all by itself. How can it be constant and vary at the same time? The better name for the second one is a memory constant because they have a "specific storage location in memory".

    As the name suggests the name constants is given to such variables or values in C++ programming language which cannot be modified once they are defined. They are fixed values in a program. There can be any types of constants like integer, float, octal, hexadecimal, character constants etc. Every constant has some range. The integers that are too big to fit into an int will be taken as long. Now there are various ranges that differ from unsigned to signed bits. Under the signed bit, the range of an int varies from -128 to +127 and under the unsigned bit, int varies from 0 to 255.

    Defining Constants:

    In C/C++ program we can define constants in two ways as shown below:

    1. Using #define preprocessor directive
    2. Using a const keyword

    Let us now learn about above two ways in details:

    1. Using #define preprocessor directive: This directive is used to declare an alias name for existing variable or any value. We can use this to declare a constant as shown below:
      #define identifierName value
      • identifierName: It is the name given to constant.
      • value: This refers to any value assigned to identifierName.

      Example

    #include <iostream>
    using namespace std; 
    
    #define val 10 
    #define floatVal 4.5 
    #define charVal 'G' 
    
    int main() { 
    	cout << "Integer Constant: " << val << "\n"; 
    	cout << "Floating point Constant: " << floatVal << "\n"; 
    	cout << "Character Constant: "<< charVal << "\n"; 
    	
    	return 0; 
    } 
    

    Output:

    Integer Constant: 10
    Floating point Constant: 4.5
    Character Constant: G
    

    Refer Macros and Preprocessors in C for details.

    1. using a const keyword: Using const keyword to define constants is as simple as defining variables, the difference is you will have to precede the definition with a const keyword.

    You must set a value on a const variable at the time you create it:

    RIGHT: You MUST set the value at the time of creation
    const int myAmount = 10;
    
    WRONG: you can not attempt to set a const variable after it is defined
    const int myAmount;
    myAmount = 10;
    

    Below program shows how to use const to declare constants of different data types:

    #include <iostream>
    using namespace std; 
    
    int main() { 
    	// int constant 
    	const int intVal = 10; 
    
    	// Real constant 
    	const float floatVal = 4.14; 
    
    	// char constant 
    	const char charVal = 'A'; 
    
    	// string constant 
    	const string stringVal = "ABC"; 
    	
    	cout << "Integer Constant: " << intVal << "\n"; 
    	cout << "Floating point Constant: " << floatVal << "\n"; 
    	cout << "Character Constant: "<< charVal << "\n"; 
    	cout << "String Constant: "<< stringVal << "\n"; 
    	
    	return 0; 
    } 
    

    Output:

    Integer constant: 10 
    Floating point constant: 4.14
    Character constant: A 
    String constant: ABC 
    

     

    Definitions

    Constant
    A data item whose value cannot change during the program's execution.
    Variable
    A data item whose value can change during the program's execution.

    Adapted from:
    "Constants in C/C++" by Chinmoy LenkaGeeks for Geeks
    "Understanding Constants" by Kenneth Leroy Busbee, (Download for free at http://cnx.org/contents/303800f3-07f...93e8948c5@22.2) is licensed under CC BY 4.0