section 2.3: Constants

page 37

We write constants in decimal, octal, or hexadecimal for our convenience, not the compiler's. The compiler doesn't care; it always converts everything into binary internally, anyway. (There is, however, no good way to specify constants in source code in binary.)

pages 37-38

Read the descriptions of character and string constants carefully; most C programs work with these data types a lot, and their proper use must be kept in mind. Note particularly these facts:

  1. The character constant 'x' is quite different from the string constant "x".
  2. The value of a character is simply ``the numeric value of the character in the machine's character set.''
  3. Strings are terminated by the null character, \0. (This applies to both string constants and to all other strings we'll build and manipulate.) This means that the size of a string (the number of char's worth of memory it occupies) is always one more than its length (i.e. as reported by strlen) appears to be.

As we saw in section 1.6 on page 23, it's possible to switch rather freely between thinking of a character as a character and thinking of it as its value. For example, the character '0' (that is, the character that can print on your screen and looks like the number zero) has in the ASCII character set the internal value 48. Another way of saying this is to notice that the following expressions are all true:

	'0' == 48
	'0' == '\060'
	'0' == '\x30'
We'll have a bit more to say about characters and their small integer representations in section 2.7.

Note also that the string "48" consists of the three characters '4', '8', and '\0'. Also in section 2.7 we'll meet the atoi function which computes a numeric value from a string of digits like this.

page 39

We won't be using enumerations, so you don't have to worry too much about the description of enumeration constants.


Read sequentially: prev next up top

This page by Steve Summit // Copyright 1995, 1996 // mail feedback