There are different kinds of constants, just as there are different types. For compound data types, constants are lists of constants of the type making up the compound. More will is said about this in the lesson named: Data Initialization. There are four kinds of constants:
If a constant does not fit in the amount of storage for its type the result is undefined - either some bits are truncated or there is an overflow.
Character constants are actually of type int, but in order to map characters from the source alphabet to the target alphabet, there is a special form of character constant which is enclosed within a pair of apostrophes. This avoids having to know the value of these characters in the the different alphabets and makes a program more portable (i.e. computer independent).
Thus if one wants a capital-A, one simply writes:
'A'
This method of enclosing in apostrophes will work for any character except: a newline character, the apostrophe and the backslash. To represent these 3 characters, as well as any character not in the source alphabet, an escape sequenc must be used between the two apostrophes. All escape sequences start with a backslash, and hence the backslash itself needs an escape sequence:
Escape Sequence | Name or Meaning | ASCII Code|
---|---|---|
\a | alert (bell) | 7 |
\b | backspace | 8 |
\t | horizontal tab | 9 |
\n | new line (LF) | 10 |
\v | vertical tab | 11 |
\f | form feed | 12 |
\r | carriage return | 13 |
\" | quote mark | 34 |
\' | apostrophe | 46 |
\\ | backslash | 92 |
\ddd | octal code | 0ddd |
\xdd | hexadecimal code | 0xdd |
A backslash before any other character is usually that character, but it is not guaranteed.
There are three forms for the integer constants - these are constants in base 10 (decimal), base 8 (octal), and base 16 (hexadecimal).
Any, constant using only the 10 digits but not starting with a zero, '0', as the first digit, is in base 10. Such a constant is of type "int". Such a constant can be used with variables of type "short int", but if the value is too big to be represented then the actual value is undefined - high bits are truncated or overflow. Normally, a compiler can be set to give a warning when this occurs.
If the first digit of a digit sequence (which does not include '8's or '9's) is a zero '0', then it is an octal (base 8) constant. Similarly, if the zero is followed by an 'x' or an 'X', then the constant is hexadecimal (base 16), and can include all the digits '0 to '9' and 'a' to 'f' (or 'A' to 'F').
127 /* decimal constant */ 0127 /* octal constant */ 0x127 /* hexadecimal constant */
To make a constant which is of type "long int" it must be followed by the suffix 'L' or 'l' (with no intervening white space). To make a constant "unsigned" it must be followed (also with no intervening) white space) by a 'U' or a 'u'. These two suffixes may be combined in any order and case.
Some example constants are:
229 987345L 40000u 0xFace9l 33999999uL
Note that putting a minus sign before a constant is not part of a constant, it is a constant expression involving the unary minus sign.
In actual practice, one finds that octal and hexadecimal constants are taken as unsigned - contrary to the ANSI language specification!
The floating constants consist of three parts which must follow each other in order without intervening white space. The last two are optional and may not be present:
fractional_part exponent_part [opt] floating_suffix [opt]
The fractional part consists of two digit sequences (either of which but not both may be null) separated by a decimal point, '.'. For example:
0.45 212.0067 720. .99996
These are decimal fractions and are only in base 10. Again, any preceding minus sign is not part of the constant, it is a separate token.
If there is an 'exponent_part' then the constant is in scientific notation, that is the fractional part is multiplied by 10 to some power. The 'exponent_part' consists of: 'e' or 'E, followed by an optional sign, '-' or '+' (implicit '+' if left off), followed by a decimal digit sequence. For example:
scientific | equivalent to |
---|---|
1.0E6 | 1000000.0 |
0.4e-4 | 0.00004 |
33.14e-12 | 0.000000000033144 |
Floating constants are, by default of type "double", unless the optional 'floating_suffix' is present, which consists of either a 'L' or 'l' - in which case the constant is of type long double:
24.7L 2.7798793696925494E-20l
An enumeration constant is an identifier declared when an enumeration tag was declared. Such constants are of type signed int.