Symbolic Constants
Depending on the symbolic name, the symbolic constants from the set of fixed type represent the fixed value. 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 instead of constant values is that it prevents the spread of "magic numbers" - the numerical constant, which is spread in code 3. This is very important because the magic number is error-prone and sources are the main director. Poetry when trying to code-change.The symbolic constants are fixed together at one place, making the transformation easier and safer.
For example, refer to the Fahrenheit to Celcius examples from Sections 1.5 and 1.6. The first example uses magic numbers, while the second uses symbolic constants.
Note. The #define symbol, like the #include symbol for file inclusion, is a preprocessor command (see Section 10.2). As such, it is subject to di erent rules than the core C language. Importantly, the # must be the first character on a line; it must not be indented.
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).
Style Note. Symbolic constants and enumerations are by convention given uppercase names. This makes them distinct from variables and functions, which, according to good practice, should always begin with a lowercase letter. Variables qualified by const behave like constants5 and so should also be identified with uppercase names, or with the first letter uppercase.
printf Conversion Specifiers
The standard function printf() facilitates formatted text output. It merges numerical values of any type into a character string using various formatting operators and conversion specifiers.
printf("Character values %c %c %c\n", ’a’, ’b’, ’c’);
printf("Some floating-point values %f %f %f\n", 3.556, 2e3, 40.1f); printf("Scientific notation %e %e %e\n", 3.556, 2e3, 40.1f); printf("%15.10s\n", "Hello World\n"); /* Right-justify string with space for
15 chars, print only first 10 letters */
A more complete discussion of printf() and its formatting fields and conversion specifiers is given in Section 13.1.1 (see also [KR88, pages 154, 243–246] and [HS95, page 372]).
Important. A conversion specifier and its associated variable must be of matching type. If they are not, the program will either print garbage or crash. For example,
printf("%f", 52); /* Mismatch: floating point specifier, integer value */
All enumerations are compatible with type int. For example, int j = TRUE; is valid, as is enum Boolean k = -4;.
5There are some important di erences between the behaviour of symbolic constants, enumerations and const
qualified variables, as explained in Section 10.2.
No comments:
Post a Comment