Saturday, July 2, 2016

Constants

Constants can have different types and representations. This section presents various constant types.
For example:
An integer constant 1234 is of type int.
·        A constant of type long int is suffixed by an L, 1234L; (integer constants too big for int are implicitly taken as long).
·        An unsigned int is suffixed by a U, 1234U, and
·        UL specifies unsigned long.
Integer constants may also be specified by octal (base 8) or hexadecimal (base 16) values, rather than decimal (base 10). Octal numbers are preceded by a 0 and hex by 0x. Thus, 1234 in decimal is equivalent to 02322 and 0x4D2.
It is important to remember that these three constants represent exactly the same value (0101 1101 0010 in binary).
For example, the following code:
int x = 1234, y = 02322, z = 0x4D2;
printf("%d\t%o\t%x\n", x, x, x);
printf("%d\t%d\t%d\n", x, y, z);
Prints:
1234 2322 4d2
1234 1234 1234

Notice that C does not provide a direct binary representation. However, the hex form is very useful in practice as it breaks down binary into blocks of four bits.
·        Floating-point constants are specified by a decimal point after a number. For example, 1. And 1.3 are of type double, 3.14f and 2.f are of type float, and 7.L is of type long double. Floating-point numbers can also be written using scientific notation, such as 1.65e-2 (which is equivalent to 0.0165).
·        Character constants, such as ’a’, ’\n’, ’7’, are specified by single quotes. Character constants are noteworthy because they are, in fact, not of type char, but of int. Most platforms represent characters using the ASCII character set, which associates the integers 0 to 127 with specific characters (e.g., the character ’T’ is represented by the integer 84).
There are certain characters that cannot be represented directly, but rather are denoted by an “escape sequence”. It is important to recognise that these escape characters still represent single characters.
A selection of key escape characters are the following:
·        \0 for NUL (used to terminate character strings),
·        \n for newline,
·        \t for tab,
·        \v for vertical tab,
·        \\ for backslash,
·        \’ for single quotes,
·        \" for double quotes, and
·        \b for backspace.
String constants, such as "This is a string" are delimited by quotes (note, the quotes are not actually part of the string constant). They are implicitly appended with a terminating’\0’ character. Thus, in memory, the above string constant would comprise the following character sequence: This is a string\0.

Symbolic Constants

Symbolic constants represent constant values, from the set of constant types mentioned above, by a symbolic name.
For example,
#define BLOCK_SIZE 100
#define TRACK_SIZE (16*BLOCK_SIZE)
#define HELLO "Hello World\n"
#define EXP 2.7183
Wherever a symbolic constant appears in the code, it is equivalent to direct text-replacement with the constant it defines.
For example,
printf(HELLO);
prints the string Hello World.
The reason for using symbolic constants rather than constant values directly, is that it prevents the proliferation of “magic numbers”—numerical constants scattered throughout the code.
This is very important as magic numbers are error-prone and are the source of major difficulty when attempting to make code-changes. Symbolic constants keep constants together in one place so that making changes is easy and safe.

Another form of symbolic constant is an enumeration, which is a list of constant integer values.
For example,
enum Boolean {FALSE, TRUE};
The enumeration tag Boolean defines the “type” of the enumeration list, such that a variable may be declared of the particular type.
enum Boolean x = FALSE;
If an enumeration list is defined without an explicit tag, it assumes the type int.4
For example,
enum {RED=2, GREEN, BLUE, YELLOW=4, BLACK};
int y = BLUE;

The value of enumeration lists starts from zero by default, and increments by one for each subsequent member (e.g., FALSE is 0 and TRUE is 1). List members can also be given explicit integer values, and non-specified members are each one greater than the previous member (e.g., RED is 2, GREEN is 3, BLUE is 4, YELLOW is 4, and BLACK is 5).

No comments:

Post a Comment