Monday, 4 June 2012

Constants

C Constants :
Constants in C refer to fixed values that do not change during the execution of program. C suports several types of constants as illustrated .





An integer constant refers to a sequence of digits. There are three types of integers, namely, decimal integer, octal integer and hexadecimal integer.
Decimal integers consist of a set of digits, 0 through 9, preceded by an optional — or + sign. Valid examples of decimal integer constants are:


123 — 321 0 654321 +78


Embedded spaces, commas, and non-digit characters are not permitted between digits. For exam¬ple,


15 750 20,000 S1000


are illegal numbers. Note that  C supports unary plus which was not defined earlier.
An octal integer constant consists of any combination of digits from the set 0 through 7, with a leading (). Some examples of octal integer are:


037 0 0435 0551

A sequence of digits preceded by Ox or OX is considered as hexadecimal integer. They may also include alphabets A through F or a through f. The letter A through F represent the numbers 10 through 15. Following are the examples of valid hex integers.

OX' Ox9F OXbcd Ox

We rarely use octal and hexadecimal numbers in programming.
The lamest integer value that can be stored is machine-dependent. It is 32767 on 16-bit machines and 2.147.483.647 on 32-bit machines. It is also possible to store larger integer constants on these machines by appending qualifiers such as U,L and LIL to the constants. For examples:

56789U or 56789u (unsigned integer)
987612347UL or 98761234111 (unsigned long integer)
9876543L or 98765431 (long integer)
The concept of unsigned and lone integers are discussed in detail in Section 2.7.

Example Representation of integer constants on a 16-bit computer.

The program in Fig. illustrates the use of integer constants on a 16-bit machine. The output in Fig. shows that the integer values larger than 32767 are not properly stored on a 16-bit machine. However. when they are qualified as long integer (by appending L), the values are correctly stored.


Real Constants :

Integer numbers are inadequate to represent quantities that very continuously, such as distances, height, temperatures, price and so on. These quantities are represented by numbers containing  frictional parts like I 7.548. Such numbers are called real (or floating poini) constants. Further ex¬amples of real constants are:
0.0083 -0.75 435.36 +247.0
These numbers are shown in decimal notation, having a whole number followed by a decimal point and the fractional part. It is possible to omit digits before the decimal point, or digits after the decimal point. That is,
215. .95 -.71 +.5
are all valid real numbers.
A real number may also be expressed in exponential (or scientific) notation. For example, the value 215.65 may be written as 2.1565e2 in exponential notation. e2 means multiply by 102. The general form is:
mantissa C exponent
The mantissa is either a real number expressed in decimal notation or an integer. The exponent is an integer number with an optional plus or minus sign. The letter e separating the mantissa and the exponent can be written in either lowercase or uppercase. Since the exponent causes the decimal point to "float-, this notation is said to represent a real number in floating poin(lin-m. Examples of legal floating-point constants are:
0.65e4 I 2e-2 I .5e+5 3.18E3 -1.2E-1
Embedded white space is not allowed.
Exponential notation is useful for representing numbers that are either very large or very small in magnitude. For example, 7500000000 may be written as 7.5E9 or 75E8. Similarly, -0.000000368 is equivalent to -3.68E-7.

Floating-point constants are normally represented as double-precision quantities. However, the suffixes f or F may be used to force single-precision and I or L to extend double precision further. Some examples of valid and invalid numeric constants are given in Table .



Single Character Constants
A single character constant (or simply character constant) contains a single character enclosed within a pair of single quote marks. Example of character constants are:
'5'
Note that the character constant '5' is not the same as the number 5. The last constant is a blank
space.
Character constants have integer values known as ASCII values. For example. the statement
printf("%d", ' a ) ;
would print the number 97. the ASCII value of the letter a. Similarly, the statement

printf("%c", '97');
would output the letter 'a'. ASCII values for all characters are given in Appendix II.
Since each character constant represents an integer value, it is also possible to perform arithmetic operations on character constants.
String Constants
A string constant is a sequence of characters enclosed in double quotes. The characters may be letters, numbers, special characters and blank space. Examples are:
"Hello!" "1987" "WELL DONE" "?...!" "5+3" "X"
Remember that a character constant (e.g., 'X') is not equivalent to the single character string constant (e.g., "X"). Further, a single character string constant does not have an equivalent integer value while a character constant has an integer value. Character strings are often used in programs to build meaningful programs.
Backslash Character Constants
C supports some special backslash character constants that are used in output functions. For example, the symbol `s\n' stands for newline character. A list of such backslash character constants is given in Table. Note that each one of them represents one character, although they consist of two characters. These characters combinations are known as escape sequences.





No comments:

Post a Comment