Sei sulla pagina 1di 5

LECTURE NUMBER 7 Constants Constants are expressions with a fixed value.

Literals Literals are used to express particular values within the source code of a program. We have already used these previously to give concrete values to variables or to express messages we wanted our programs to print out, for example, when we wrote:

the 5 in this piece of code was a literal constant. Literal constants can be divided in : Integer umerals, !loating"#oint umerals, Characters, $trings and %oolean &alues. Integer Numerals ,--. -1"5-6 'hey are numerical constants that identify integer decimal values. otice that to express a numerical constant we do not have to write (uotes )*+ nor any special character. 'here is no doubt that it is a constant: whenever we write ,--. in a program, we will be referring to the value ,--.. In addition to decimal numbers )those that all of us are used to use every day+ C// allows the use as literal constants of octal numbers )base 0+ and hexadecimal numbers )base ,.+. If we want to express an octal number we have to precede it with a 1 )2ero character+. 3nd in order to express a hexadecimal number we have to precede it with the characters 1x )2ero, x+. !or example, the following literal constants are all e(uivalent to each other:
-5 1,,6 1x8b 77 decimal 77 octal 77 hexadecimal

3ll of these represent the same number: -5 )seventy"five+ expressed as a base",1 numeral, octal numeral and hexadecimal numeral, respectively.

Literal constants, li9e variables, are considered to have a specific data type. %y default, integer literals are of type int. :owever, we can force them to either be unsigned by appending the u character to it, or long by appending l:
-5 -5u -5l -5ul 77 int 77 unsigned int 77 long 77 unsigned long

In both cases, the suffix can be specified using either upper or lowercase letters. Floating Point Numbers 'hey express numbers with decimals and7or exponents. 'hey can include either a decimal point, an e character )that expresses *by ten at the ;th height*, where ; is an integer value that follows the e character+, or both a decimal point and an e character:
6.,8,5< 77 6.,8,5< ..15e56 77 ..15 x ,156 ,..e",< 77 ,.. x ,1",< 6.1 77 6.1

'hese are four valid numbers with decimals expressed in C//. 'he first number is #I, the second one is the number of 3vogadro, the third is the electric charge of an electron )an extremely small number+ "all of them approximated" and the last one is the number three expressed as a floating" point numeric literal. 'he default type for floating point literals is double. If you explicitly want to express a float or long double numerical literal, you can use the f or l suffixes respectively: 6.,8,5<L 77 long double ..15e56f 77 float 3ny of the letters than can be part of a floating"point numerical constant )e, f, l+ can be written using either lower or uppercase letters without any difference in their meanings. Character and String Literals 'here also exist non"numerical constants, li9e:
=2= =p= *:ello world*

*:ow do you do>*

'he first two expressions represent single character constants, and the following two represent string literals composed of several characters. otice that to represent a single character we enclose it between single (uotes )=+ and to express a string )which generally consists of more than one character+ we enclose it between double (uotes )*+. When writing both single character and string literals, it is necessary to put the (uotation mar9s surrounding them to distinguish them from possible variable identifiers or reserved 9eywords. otice the difference between these two expressions:
; @;A

x alone would refer to a variable whose identifier is x, whereas =x= )enclosed within single (uotation mar9s+ would refer to the character constant =x=. Character and string literals have certain peculiarities, li9e the escape codes. 'hese are special characters that are difficult or impossible to express otherwise in the source code of a program, li9e newline )?n+ or tab )?t+. 3ll of them are preceded by a bac9slash )?+. :ere you have a list of some of such escape codes:
\n \r \t \v \b \f \a \' \" \? \\
newline carriage return tab vertical tab backspace form feed (page feed) alert (beep) single quote (') double quote (") question mark (?) backslash (\)

!or example:
=?n= =?t= *Left ?t Bight* *one?ntwo?nthree*

3dditionally, you can express any character by its numerical 3$CII code by writing a bac9slash character )?+ followed by the 3$CII code expressed as an octal )base"0+ or hexadecimal )base",.+ number. In the first case )octal+ the digits must immediately follow the bac9slash )for example ? 56 or ?81+, in the second case )hexadecimal+, an x character must be written before the digits themselves )for example ?x51 or ?x83+. $tring literals can extend to more than a single line of code by putting a bac9slash sign )?+ at the end of each unfinished line.

*string expressed in ? two lines*

Cou can also concatenate several string constants separating them by one or several blan9 spaces, tabulators, newline or any other valid blan9 character: *this forms* *a single* *string* *of characters* !inally, if we want the string literal to be explicitly made of wide characters )wcharDt+, instead of narrow characters )char+, we can precede the constant with the L prefix: L*'his is a wide character string* Wide characters are used mainly to represent non"English or exotic character sets. Boolean literals 'here are only two valid %oolean values: true and false. 'hese can be expressed in C// as values of type bool by using the %oolean literals true and false. Defined Constants (#define) Cou can define your own names for constants that you use very often without having to resort to memory"consuming variables, simply by using the Fdefine preprocessor directive. Its format is: Fdefine identifier value !or example: Fdefine #I 6.,8,5<5.5 Fdefine EWLI E =?n= 'his defines two new constants: #I and EWLI E. Gnce they are defined, you can use them in the rest of the code as if they were any other regular constant, for example:
77 defined constants: calculate circumference Finclude HiostreamI using namespace stdJ Fdefine #I 6.,8,5< Fdefine EWLI E =?n=J int main )+ K double r45.1J double circleJ circle 4 5 L #I L rJ cout HH circleJ cout HH EWLI EJ return 1J M


77 radius

In fact the only thing that the compiler preprocessor does when it encounters Fdefine directives is to literally replace any occurrence of their identifier )in the previous example, these were #I and EWLI E+ by the code to which they have been defined )6.,8,5<5.5 and =?n= respectively+. 'he Fdefine directive is not a C// statement but a directive for the preprocessorJ therefore it assumes the entire line as the directive and does not re(uire a semicolon )J+ at its end. If you append a semicolon character )J+ at the end, it will also be appended in all occurrences within the body of the program that the preprocessor replaces. Declared Constants (const) With the const prefix you can declare constants with a specific type in the same way as you would do with a variable: const int pathwidth 4 ,11J const char tabulator 4 =?t=J const 2ipcode 4 ,5881J In case that no type is explicitly specified )as in the last example+ the compiler assumes that it is of type int.