Skip to main content
Engineering LibreTexts

4.4: Constants and Variables

  • Page ID
    29036
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    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 
    

    Limits

    Each of the different data types have upper and lower limits depending on the data type, the processor and the compiler. The C++ standard has a defined minimum and maximum for all the different data types. 

    The limits.h header determines various properties of the various variable types. The macros defined in this header, limits the values of various variable types like char, int and long.

    These limits specify that a variable cannot store any value beyond these limits, for example an unsigned character can store up to a maximum value of 255.

    Library Macros

    The following values are implementation-specific and defined with the #define directive, but these values may not be any lower than what is given here.

    Macro Value Description
    CHAR_BIT 8 Defines the number of bits in a byte.
    SCHAR_MIN -128 Defines the minimum value for a signed char.
    SCHAR_MAX +127 Defines the maximum value for a signed char.
    UCHAR_MAX 255 Defines the maximum value for an unsigned char.
    CHAR_MIN -128 Defines the minimum value for type char and its value will be equal to SCHAR_MIN if char represents negative values, otherwise zero.
    CHAR_MAX +127 Defines the value for type char and its value will be equal to SCHAR_MAX if char represents negative values, otherwise UCHAR_MAX.
    MB_LEN_MAX 16 Defines the maximum number of bytes in a multi-byte character.
    SHRT_MIN -32768 Defines the minimum value for a short int.
    SHRT_MAX +32767 Defines the maximum value for a short int.
    USHRT_MAX 65535 Defines the maximum value for an unsigned short int.
    INT_MIN -2147483648 Defines the minimum value for an int.
    INT_MAX +2147483647 Defines the maximum value for an int.
    UINT_MAX 4294967295 Defines the maximum value for an unsigned int.
    LONG_MIN -9223372036854775808 Defines the minimum value for a long int.
    LONG_MAX +9223372036854775807 Defines the maximum value for a long int.
    ULONG_MAX 18446744073709551615 Defines the maximum value for an unsigned long int.

    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


    This page titled 4.4: Constants and Variables is shared under a CC BY-SA license and was authored, remixed, and/or curated by Patrick McClanahan.