Programming in C: Unit I (b): Introduction to C

Constants

C Program

Constants are identifiers whose values do not change. While values of variables can be changed at any time, values of constants can never be changed.

CONSTANTS

Constants are identifiers whose values do not change. While values of variables can be changed at any time, values of constants can never be changed. Constants are used to define fixed values like pi or the charge on an electron so that their value does not get changed in the program even by mistake.

A constant is an explicit data value specified by the programmer. The value of the constant is known to the compiler at the compile time. C allows the programmer to specify constants of integer type, floating point type, character type, and string type (Figure 2.10).

Integer Constants

A constant of integer type consists of a sequence of digits. For example, 1, 34, 567, 8907 are valid integer constants. A literal integer like 1234 is of type int by default. For a long integer constant the literal is succeeded with either 'L' or 'l' (like 1234567L). Similarly, an unsigned int literal is written with a 'U' or 'u' suffix (ex, 12U). Therefore, 1234L, 1234I, 1234U, 1234u, 1234LU, 1234ul are all valid integer constants.

Integer literals can be expressed in decimal, octal or hexadecimal notation. By default an integer is expressed in decimal notation. Decimal integers consist of a set of digits, 0 through 9, preceded by an optional - or + sign. Examples of decimal integer constants include: 123,-123, +123, and 0.

While writing integer constants, embedded spaces, commas, and non-digit characters are not allowed. There- fore, integer constants given below are totally invalid in C.

123 456

12,34,567

$123

An integer constant preceded by a zero (0) is an octal number. Octal integers consist of a set of digits, 0 through

7. Examples of octal integers include

012 0 01234

Similarly, an integer constant is expressed in hexadecimal notation if it is preceded with Ox or OX. Hexadecimal numbers contain digits from 0-9 and letters A through F, which represent numbers 10 through 15. For example, decimal 72 is equivalent to 0110 in octal notation and Ox48 in hexadecimal notation. Examples of hexadecimal integers are 0X12, 0x7F, 0xABCD, 0X1A3B.

Note

In C, a decimal integer constant is treated as an unsigned long if its magnitude exceeds that of signed long. An octal or hexadecimal integer that exceeds the limit of int is taken to be unsigned. If even this limit is exceeded, it is taken as long; and in case this limit is exceeded, it is treated as unsigned long.

Floating Point Constants

Integer numbers are inadequate to express numbers that have a fractional part. A floating point constant therefore consists of an integer part, a decimal point, a fractional part, and an exponent field containing an e or E (e means exponent) followed by an integer where the fraction part and integer part are a sequence of digits. However, it is not necessary that every floating point constant must contain all these parts. Some floating point numbers may have certain parts missing. Some valid examples of floating point numbers are: 0.02, -0.23, 123.456, +0.34 123, 0.9, -0.7, +0.8 etc.

A literal like 0.07 is treated as of type double by default. To make it a float type literal, you must specify it using suffix 'F' or 'f'. Consider some valid floating point literals given below. (Note that suffix L is for long double.)

 0.02F 0.34f 3.141592654L 0.002146 2.146E-3

A floating point number may also be expressed in scientific notation. In this notation, the mantissa is either a floating point number or an integer and exponent is an integer with an optional plus or minus sign. Therefore, the numbers given below are valid floating point numbers

0.5e2 14E-2 1.2e+3 2.1E-3 -5.6e-2

Thus, we see that scientific notation is used to express numbers that are either very small or very large. For example,

120000000= 1.2E8 and -0.000000025= -2.5E-8

Character Constants

A character constant consists of a single character enclosed in single quotes. For example, 'a' and `@' are character constants. In computers, characters are stored using machine's character set using ASCII codes. All escape sequences mentioned in Table 2.1 are also character constants.

String Constants

A string constant is a sequence of characters enclosed in double quotes. So "a" is not the same as 'a'. The characters comprising the string constant are stored in successive memory locations. When a string constant is encountered in a C program, the compiler records the address of the first character and appends a null character ('\0') to the string to mark the end of the string. Thus, length of a string constant is equal to number of characters in the string plus 1 (for the null character). Therefore, the length of string literal "hello" is 6.

Declaring Constants

To declare a constant, precede the normal variable declaration with const keyword and assign it a value. For example,

const float pi = 3.14;

The const keyword specifies that the value of pi cannot change.

However, another way to designate a constant is to use the pre-processor command define. Like other preprocessor commands, define is preceded with a # symbol. Although #define statements can be placed anywhere in a C program, it is always recommended that these statements be placed at the beginning of the program to make them easy to find and modify at a later stage. Look at the example given below which defines the value of pi using define.

#define pi 3.14159

#define service_tax 0.12

In these examples, the value of pi will never change but service tax may change. Whenever the value of the service tax is altered, it needs to be corrected only in the define statement.

When the preprocessor reformats the program to be compiled by the compiler, it replaces each defined name (like pi, service_tax) in the source program with its corresponding value. Hence, it just works like the Find- and-Replace command available in a text editor.

Let us take a look at some rules that need to be applied to a #define statement which defines a constant.

Rule 1: Constant names are usually written in capital letters to visually distinguish them from other variable names which are normally written in lower case characters. Note that this is just a convention and not a rule.

Rule 2: No blank spaces are permitted between the # symbol and define keyword.

Rule 3: Blank space must be used between #define and constant name and between-0 constant name and constant value.

Rule 4: #define is a pre-processor compiler directive and not a statement. Therefore, it does not end with a semi-colon.

Programming in C: Unit I (b): Introduction to C : Tag: : C Program - Constants