2.1: Constants and Variables
- Page ID
- 126476
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\( \newcommand{\dsum}{\displaystyle\sum\limits} \)
\( \newcommand{\dint}{\displaystyle\int\limits} \)
\( \newcommand{\dlim}{\displaystyle\lim\limits} \)
\( \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}}\)
\( \newcommand{\vectorA}[1]{\vec{#1}} % arrow\)
\( \newcommand{\vectorAt}[1]{\vec{\text{#1}}} % arrow\)
\( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vectorC}[1]{\textbf{#1}} \)
\( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)
\( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)
\( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\(\newcommand{\longvect}{\overrightarrow}\)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\(\newcommand{\avec}{\mathbf a}\) \(\newcommand{\bvec}{\mathbf b}\) \(\newcommand{\cvec}{\mathbf c}\) \(\newcommand{\dvec}{\mathbf d}\) \(\newcommand{\dtil}{\widetilde{\mathbf d}}\) \(\newcommand{\evec}{\mathbf e}\) \(\newcommand{\fvec}{\mathbf f}\) \(\newcommand{\nvec}{\mathbf n}\) \(\newcommand{\pvec}{\mathbf p}\) \(\newcommand{\qvec}{\mathbf q}\) \(\newcommand{\svec}{\mathbf s}\) \(\newcommand{\tvec}{\mathbf t}\) \(\newcommand{\uvec}{\mathbf u}\) \(\newcommand{\vvec}{\mathbf v}\) \(\newcommand{\wvec}{\mathbf w}\) \(\newcommand{\xvec}{\mathbf x}\) \(\newcommand{\yvec}{\mathbf y}\) \(\newcommand{\zvec}{\mathbf z}\) \(\newcommand{\rvec}{\mathbf r}\) \(\newcommand{\mvec}{\mathbf m}\) \(\newcommand{\zerovec}{\mathbf 0}\) \(\newcommand{\onevec}{\mathbf 1}\) \(\newcommand{\real}{\mathbb R}\) \(\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}\) \(\newcommand{\laspan}[1]{\text{Span}\{#1\}}\) \(\newcommand{\bcal}{\cal B}\) \(\newcommand{\ccal}{\cal C}\) \(\newcommand{\scal}{\cal S}\) \(\newcommand{\wcal}{\cal W}\) \(\newcommand{\ecal}{\cal E}\) \(\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}\) \(\newcommand{\gray}[1]{\color{gray}{#1}}\) \(\newcommand{\lgray}[1]{\color{lightgray}{#1}}\) \(\newcommand{\rank}{\operatorname{rank}}\) \(\newcommand{\row}{\text{Row}}\) \(\newcommand{\col}{\text{Col}}\) \(\renewcommand{\row}{\text{Row}}\) \(\newcommand{\nul}{\text{Nul}}\) \(\newcommand{\var}{\text{Var}}\) \(\newcommand{\corr}{\text{corr}}\) \(\newcommand{\len}[1]{\left|#1\right|}\) \(\newcommand{\bbar}{\overline{\bvec}}\) \(\newcommand{\bhat}{\widehat{\bvec}}\) \(\newcommand{\bperp}{\bvec^\perp}\) \(\newcommand{\xhat}{\widehat{\xvec}}\) \(\newcommand{\vhat}{\widehat{\vvec}}\) \(\newcommand{\uhat}{\widehat{\uvec}}\) \(\newcommand{\what}{\widehat{\wvec}}\) \(\newcommand{\Sighat}{\widehat{\Sigma}}\) \(\newcommand{\lt}{<}\) \(\newcommand{\gt}{>}\) \(\newcommand{\amp}{&}\) \(\definecolor{fillinmathshade}{gray}{0.9}\)Constants
A constant is a value that cannot be altered by the program during normal execution. Normally, a constant will be associated with a name when it is used in a program. For example, we can write a line of the code in a program to set up a constant which we can use throughout the program:
const double PI=3.1415926;
In programming, we often call a line of the code a statement. Each statement must be ended by a semi-colon. In this statement, const is called a key word which specifies the statement is for setting up a constant. As a key word, it is pre-defined and can not be changed to something else. double is also a key word which specifies that the constant we are setting up is a decimal number. In computer programming, data a program uses, receives, or outputs are categorized into different types: decimals (double), integers (int), characters (char), strings (string), or Boolean values (bool) and so on. For example, if we would like to write a statement that is setting up a constant with 100 as its value, we would write this statement as following:
const int MAX=100;
In above statements, the words PI and MAX are names we used for those constants in the program, and they are also called identifiers. Such constants are also called named constants. In C++, we use ALL CAPS for a named constant. As a coding person of a program, we can use any ALL CAPS string as the name of a constant. However, certain rules are applied:
- We normally don't begin an identifier with a digit, such as 5PGE.
- We normally don't use special character in an identifier, such as $, or PG&E
- We cannot use blank space in an identifier, such as STUDENT ID. If you do want to use multiple words for an identifier, you may use STUDENT_ID
In program that contains above two statements, the values of PI and MAX can not be altered by the program during normal execution, which means if you write a statement:
PI=3.14;
or
MAX=200;
in the same program, then you can't successfully build and run the program, you will encounter syntax errors.
Variables
A variable in a C++ program is an identity that represents a unit in a computer memory for storing data. A variable must be associated with an identifier (a variable must be named before we can use it in a program. For example, if we would like to write a program that is for calculating the area of a rectangle and we would like to use two variables in this program for storing the length and the width of the rectangle, then we may write a statement that associates two identifiers with those two variables as following:
double length, width;
In this statement, the key word double was used to specify that both variables are used in this program for storing decimals. The word length and width are identifiers (name of the variables). Technically, we can use any string for the name of a variable, but certain rules are often apply:
- We don't use ALL CAPS for a variable identifier, such as LENGTH or WIDTH
- We normally don't capitalized the initial of an identifier, such as Length or Width
- We can't use a blank space in an identifier, such as test score, instead, we will use test_score or testScore
- We don't use special characters in a variable identifier, such as @userName
- We don't start an identifier with a digit, such as 20row, instead, we can use row20
The statements used for associating variables to identifiers are called variable declarations. If a statement associates one or more variables to their identifiers, we say such statement "declares" those variables. Each variable must be declared before it can be used in a program.
Now we discuss how to use constants and variables in a C++ program.
Example Write a program that calculates and displays the area of a circle with radius equals to 5
First we will need to create a project, and then add a source file to the project. You have to follow the steps we discuss in the Section 1.5. Following is a screenshot of the program:

Line 1: This line is for documentation purpose. It must begin with // and anything follows // will be treated as documentation and will not be considered as the code of the program.
Line 2: This is a compiler preprocessor directive that tells the compiler to include the iostream library in this program, which is needed to support console input and output. C++ has many libraries which contain pre-defined code for developing C++ programs. The library like iostream is called a header file in C++, because it is usually included at the head of a program.
Line 3: this line of the code tells the compiler to use the standard namespace. (You may not understand what it meams now, no worry, jus put this line in every program you develop in this course)
Line 5: This line sets a block for the code of the program. It begin with the key word int followed by the identifier main with a pair of parentheses. All the code of the program should be placed within { and }. A program can't run without this building block. We call this building block the main() function. (We will discuss C++ functions in later chapter in more details) The main() function begins at line 5 and ends at line 12.
Within main() function,
Line 7: It is a statement that specifies a constant named as PI with 3.1415926 as its value
Line 8: This is a statement that declares a variable with identifier "radius" and specifies that this variable is for storing decimal data. Since radius of a circle is a measurement which are always in the form of fractions or decimals
Line 9: This is a so called "assignment statement", it assigns a value, in this case it is 5.2, to the variable radius
Line 10: This is an output statement, it displays the calculation result (the area of the circle) on the output screen. In this statement, "cout<<" will send the message "The area of the circle is " to the screen first and performs the calculation PI*radius*radius and send the result to the screen through the second "<<" (which is called stream insertion operator). The result follows the message to provide a better user interface. At the end of this statement, "endl" is an end-of-line character and is sent to output window through the third stream insertion operator "<<" to tell the computer that if there is something else to display after this statement, the new display will be at the beginning of the next line.
Line 11: This statement should be placed at end of every main() function to exit the program. The value of 0 indicates that the program has terminated with a successful exit. Some compilers will work fine without this statement, but some may not. To play safe, it is good practice to place this statement at the end of main() function.
After coding the program, press Ctrl+F5 to build and execute the program. Following is a screenshot of the output window of the program:

From above discussions, we know identifiers are the names that identify elements in a program such as constants, variables, and functions. As an identifier, it is case sensitive. For example, abc and ABC are two different identifiers.


