Ch2 Programming_Basics Lec3
Ch2 Programming_Basics Lec3
Constants
Variable = Portion of memory to which we can
Legal
3dGraph
Legal
June1997
Legal
Mixture#3
a = 7;
legal
7 = a;
7 = 7;
Illegal, because the left hand signed of the assignment operator is not
a variable
Constant
Unlike a variable, it is a data item whose value cannot
change during the program’s execution
Includes literals, defined constants and declared
constants
Literals
Are Used to express particular values within the
source code of a program
e.G a = 5;
5 is a literal constant
Literals like variables are considered to have a
specific data type
Literals can be Integer Numerals, floating point
numbers, character and string literals
Literals
Integer Numerals
Numerical constants that identify integer decimal
values
C++ allows use of octal(base 8) and
hexadecimal(base 16) numbers as string literals
e.g.
Literals
Integer Literals
are by default of type Int
Can be forced to be unsigned by appending the u
character to it, or long by appending l
e.g. 75 //int
75u //unsinged int
75l //long
75ul //unsigned long
Literals
Floating point numbers
Express numbers with decimals and or exponents
e.g.
Boolean literals
There are two valid boolean values: true and false
Can be expressed in C++ as values of type bool by using the
boolean literals true and false
Defined Constants
can define your own names for constants that you use very
often without having to resort to memory-consuming
variables, simply by using the #define preprocessor
directive
e.g.
#include<iostream>
using namespace std;
#define PI 3.14159
#define NEWLINE ‘\n’
Int main()
{
cout<<PI<<NEWLINE;
return 0;
}