Saturday 4 April 2020

Literal Constants


Constants: Literals

A constant is any expression that has a fixed value. They can be divided in Integer Numbers, Floating-Point Numbers, Characters and Strings.

1.Integer Numbers
1776
707
-273
They are numerical constants that identify integer decimal numbers. Notice that to express a numerical constant we do not need to write quotes (") nor any special character. There is no doubt that it is a constant: whenever we write 1776 in a program we will be referring to the value 1776.
In addition to decimal numbers (those that all of us already know) C++ allows the use as literal constants of octal numbers (base 8) and hexadecimal numbers (base 16). If we want to express an octal number we must precede it with a 0 character (zero character). And to express a hexadecimal number we have to precede it with the characters 0x (zero, x). For example, the following literal constants are all equivalent to each other:
75         // decimal
0113       // octal
0x4b       // hexadecimal

All of them represent the same number: 75 (seventy five) expressed as a radix-10 number, octal and hexdecimal, respectively.

2. Floating Point Numbers:-They express numbers with decimals and/or exponents. They can include a decimal point, an e character (that expresses "by ten at the Xth height", where X is the following integer value) or both.
3.14159    // 3.14159
6.02e23    // 6.02 x 1023
1.6e-19    // 1.6 x 10-19
3.0        // 3.0

These are four valid numbers with decimals expressed in C++. The first number is PI, the second one is the number of Avogadro, the third is the electric charge of an electron (an extremely small number) -all of them approximated- and the last one is the number 3 expressed as a floating point numeric literal.

3. Characters and strings:-There also exist non-numerical constants, like:
'z'
'p'
"Hello world"
"How do you do?"

The first two expressions represent single characters, and the following two represent strings of several characters. Notice that to represent a single character we enclose it between single quotes (') and to express a string of more than one character we enclose them between double quotes (").
When writing both single characters and strings of characters in a constant way, it is necessary to put the quotation marks to distinguish them from possible variable identifiers or reserved words. Notice this:
x
'x'
x refers to variable x, whereas 'x' refers to the character constant 'x'.

Character constants and string constants have certain peculiarities, like the escape codes. These are special characters that cannot be expressed otherwise in the source code of a program, like newline (\n) or tab (\t). All of them are preceded by an inverted slash (\). Here you have a list of such escape codes:
\n
Newline
\r
carriage return
\t
Tabulation
\v
vertical tabulation
\b
Backspace
\f
page feed
\a
alert (beep)
\'
single quotes (')
\"
double quotes (")
\?
question (?)
\\
inverted slash (\)
For example:
'\n'
'\t'
"Left \t Right"
"one\ntwo\nthree"
Additionally, you can express any character by its numerical ASCII code by writing an inverted slash bar character (\) followed by the ASCII code expressed as an octal (radix-8) or hexadecimal (radix-16) number. In the first case (octal) the number must immediately follow the inverted slash (for example \23 or \40), in the second case (hexacedimal), you must put an x character before the number (for example \x20 or \x4A).

Constants of string of characters can be extended by more than a single code line if each code line ends with an inverted slash (\):
"string expressed in \
two lines"

You can also concatenate several string constants separating them by one or several blankspaces, tabulators, newline or any other valid blank character:
 
"we form" "a single" "string" "of characters"
 

How to Defined constants (#define)

 You can define your own names for constants that you use quite often without having to resort to variables, simply by using the #define preprocessor directive. This is its format:
#define identifier value
For example:
#define PI 3.14159265
#define NEWLINE '\n'
#define WIDTH 100

They define three new constants. Once they are declared, you are able to use them in the rest of the code as any if they were any other constant, for example:
circle = 2 * PI * r;
cout << NEWLINE;

In fact the only thing that the compiler does when it finds #define directives is to replace literally any occurrence of the them (in the previous example, PI, NEWLINE or WIDTH) by the code to which they have been defined (3.14159265, '\n' and 100, respectively). For this reason, #define constants are considered macro constants.

The #define directive is not a code instruction, it is a directive for the preprocessor, therefore it assumes the whole line as the directive and does not require a semicolon (;) at the end of it. If you include a semicolon character (;) at the end, it will also be added when the preprocessor will substitute any occurence of the defined constant within the body of the program.


How to Declared constants (const)

 With the const prefix you can declare constants with a specific type exactly as you would do with a variable:

const int width = 100;
const char tab = '\t';
const zip = 12440;

In case that the type was not specified (as in the last example) the compiler assumes that it is type int.

No comments:

Post a Comment