2.9. Constants2.9.1. Integer constantsThe normal integral constants are obvious: things
like Integer constants can be written in hexadecimal by preceding the
constant with Be careful about octal constants. They are indicated by starting the
number with The Standard has now invented a new way of working out what type an
integer constant is. In the old days, if the constant was too big for an
int long unsigned long that can hold the value. Plain octal or hexadecimal constants will use this list int unsigned int long unsigned long If the constant is suffixed by unsigned int unsigned long If it is suffixed by long unsigned long and finally, if it suffixed by both All that was done to try to give you ‘what you meant’; what it
does mean is that it is hard to work out exactly what the type of a
constant expression is if you don't know something about the hardware.
Hopefully, good compilers will warn when a constant is promoted up to
another length and the A nasty bug hides here:
printf("value of 32768 is %d\n", 32768);
On a 16-bit two's complement machine,
printf("value of 32768 is %d\n", (int)32768);
It might interest you to note that there are no negative constants;
writing Character constants actually have type 'a' 'b' 'like this' Wide character constants are written just as above, but prefixed
with L'a' L'b' L'like this' Regrettably it is valid to have more than one character in the
sequence, giving a machine-dependent result. Single characters are the
best from the portability point of view, resulting in an ordinary integer
constant whose value is the machine representation of the single
character. The introduction of extended characters may cause you to
stumble over this by accident; if To ease the way of representing some special characters that would
otherwise be hard to get into a character constant (or hard to read; does
It is also possible to use numeric escape sequences to specify a
character in terms of the internal value used to represent it. A sequence
of either "\xff" "f" The string "\xfff" only contains one character, with all three of the Some of the escape sequences aren't too obvious, so a brief explanation
is needed. To get a single quote as a character constant you
type There are two distinct purposes behind the escape sequences. It's obviously necessary to be able to represent characters such as single quote and backslash unambiguously: that is one purpose. The second purpose applies to the following sequences which control the motions of a printing device when they are sent to it, as follows:
For It is guaranteed that each escape sequence has a unique integral value
which can be stored in a 2.9.2. Real constantsThese follow the usual format: 1.0 2. .1 2.634 .125 2.e5 2.e+5 .125e-3 2.5e5 3.1E-6 and so on. For readability, even if part of the number is zero, it is a good idea to show it: 1.0 0.1 The exponent part shows the number of powers of ten that the rest of the number should be raised to, so 3.0e3 is equivalent in value to the integer constant 3000 As you can see, the For completeness, here is the formal description of a real constant: A real constant is one of:
In either case followed by an optional one of
|
The C BookThis book is published as a matter of historical interest. Please read the copyright and disclaimer information. GBdirect Ltd provides up-to-date training and consultancy in C, Embedded C, C++ and a wide range of other subjects based on open standards if you happen to be interested. |
|||||||||||||||||||||||||
West Yorkshire Office
GBdirect Ltd
Training: 0800 651 0338 Please call between 0900 and 1700 (UK time) on Monday to Friday South East Regional Office
GBdirect Ltd
Training: 0800 651 0338 Please call between 0900 and 1700 (UK time) on Monday to Friday Please note: |