📄 chapter2.html
字号:
<html><head><title>Chapter 2 - Types, Operators and Expressions</title></head><body><hr><p align="center"><a href="chapter1.html">Back to Chapter 1</a> -- <a href="kandr.html">Index</a> -- <a href="chapter3.html">Chapter 3</a><p><hr><h1>Chapter 2 - Types, Operators and Expressions</h1>Variables and constants are the basic data objects manipulated in a program.Declarations list the variables to be used, and state what type they have andperhaps what their initial values are. Operators specify what is to be doneto them. Expressions combine variables and constants to produce new values. Thetype of an object determines the set of values it can have and what operationscan be performed on it. These building blocks are the topics of this chapter.<p>The ANSI standard has made many small changes and additions to basic types andexpressions. There are now <tt>signed</tt> and <tt>unsigned</tt> forms of allinteger types, and notations for unsigned constants and hexadecimal characterconstants. Floating-point operations may be done in single precision; there isalso a <tt>long</tt> double type for extended precision. String constants may be concatenated at compile time. Enumerations have become part of the language,formalizing a feature of long standing. Objects may be declared <tt>const</tt>,which prevents them from being changed. The rules for automatic coercionsamong arithmetic types have been augmented to handle the richer set of types.<h2><a name="s2.1">2.1 Variable Names</a></h2>Although we didn't say so in <a href="chapter1.html">Chapter 1</a>, there are somerestrictions on the names of variables and symbolic constants. Names are madeup of letters and digits; the first character must be a letter. The underscore``<tt>_</tt>'' counts as a letter; it is sometimes useful for improving thereadability of long variable names. Don't begin variable names with underscore,however, since library routines often use such names. Upper and lower caseletters are distinct, so <tt>x</tt> and <tt>X</tt> are two different names.Traditional C practice is to use lower case for variable names, and all uppercase for symbolic constants.<p>At least the first 31 characters of an internal name are significant. Forfunction names and external variables, the number may be less than 31, becauseexternal names may be used by assemblers and loaders over which the languagehas no control. For external names, the standard guarantees uniqueness only for6 characters and a single case. Keywords like <tt>if</tt>, <tt>else</tt>,<tt>int</tt>, <tt>float</tt>, etc., are reserved: you can't use them asvariable names. They must be in lower case.<p>It's wise to choose variable names that are related to the purpose of thevariable, and that are unlikely to get mixed up typographically. We tend touse short names for local variables, especially loop indices, and longer namesfor external variables.<h2><a name="s2.2">2.2 Data Types and Sizes</a></h2>There are only a few basic data types in C:<p><table align="center" border=0><td><tt>char</tt></td><td>a single byte, capable of holding one character in the local character set</td><tr><td><tt>int</tt></td><td>an integer, typically reflecting the natural size of integers on the host machine</td><tr><td><tt>float</tt></td><td>single-precision floating point</td><tr><td><tt>double</tt></td><td>double-precision floating point</td></table><p>In addition, there are a number of qualifiers that can be applied to thesebasic types. <tt>short</tt> and <tt>long</tt> apply to integers:<pre> short int sh; long int counter;</pre>The word <tt>int</tt> can be omitted in such declarations, and typically it is.<p>The intent is that <tt>short</tt> and <tt>long</tt> should provide differentlengths of integers where practical; <tt>int</tt> will normally be the naturalsize for a particular machine. <tt>short</tt> is often 16 bits long, and<tt>int</tt> either 16 or 32 bits. Each compiler is free to choose appropriate sizesfor its own hardware, subject only to the the restriction that <tt>short</tt>sand </tt>int</tt>s are at least 16 bits, <tt>long</tt>s are at least 32 bits, and<tt>short</tt> is no longer than <tt>int</tt>, which is no longer than <tt>long</tt>.<p>The qualifier <tt>signed</tt> or <tt>unsigned</tt> may be applied to <tt>char</tt> orany integer. <tt>unsigned</tt> numbers are always positive or zero, and obey thelaws of arithmetic modulo <em>2<sup>n</sup></em>, where <em>n</em> is the number of bits in the type.So, for instance, if <tt>char</tt>s are 8 bits, <tt>unsigned char</tt> variableshave values between 0 and 255, while <tt>signed char</tt>s have values between-128 and 127 (in a two's complement machine.) Whether plain <tt>char</tt>s aresigned or unsigned is machine-dependent, but printable characters are alwayspositive.<p>The type <tt>long double</tt> specifies extended-precision floating point. Aswith integers, the sizes of floating-point objects are implementation-defined;<tt>float</tt>, <tt>double</tt> and <tt>long double</tt> could represent one,two or three distinct sizes.<p>The standard headers <tt><limits.h></tt> and <tt><float.h></tt> contain symbolicconstants for all of these sizes, along with other properties of the machineand compiler. These are discussed in <a href="appb.html">Appendix B</a>.<p><strong>Exercise 2-1.</strong> Write a program to determine the ranges of <tt>char</tt>,<tt>short</tt>, <tt>int</tt>, and <tt>long</tt> variables, both <tt>signed</tt> and<tt>unsigned</tt>, by printing appropriate values from standard headers and by directcomputation. Harder if you compute them: determine the ranges of the variousfloating-point types.<h2><a name="s2.3">2.3 Constants</a></h2>An integer constant like <tt>1234</tt> is an <tt>int</tt>. A <tt>long</tt> constant iswritten with a terminal <tt>l</tt> (ell) or <tt>L</tt>, as in <tt>123456789L</tt>; aninteger constant too big to fit into an <tt>int</tt> will also be taken as along. Unsigned constants are written with a terminal <tt>u</tt> or <tt>U</tt>, andthe suffix <tt>ul</tt> or <tt>UL</tt> indicates <tt>unsigned long</tt>.<p>Floating-point constants contain a decimal point (<tt>123.4</tt>) or an exponent(<tt>1e-2</tt>) or both; their type is <tt>double</tt>, unless suffixed. Thesuffixes <tt>f</tt> or <tt>F</tt> indicate a <tt>float</tt> constant; <tt>l</tt>or <tt>L</tt> indicate a <tt>long double</tt>.<p>The value of an integer can be specified in octal or hexadecimal instead ofdecimal. A leading <tt>0</tt> (zero) on an integer constant means octal; aleading <tt>0x</tt> or <tt>0X</tt> means hexadecimal. For example, decimal 31 can be written as <tt>037</tt> in octal and <tt>0x1f</tt> or <tt>0x1F</tt> inhex. Octal and hexadecimal constants may also be followed by <tt>L</tt> to makethem <tt>long</tt> and <tt>U</tt> to make them <tt>unsigned</tt>: <tt>0XFUL</tt>is an <em>unsigned long</em> constant with value 15 decimal.<p>A <tt>character constant</tt> is an integer, written as one character withinsingle quotes, such as <tt>'x'</tt>. The value of a character constant is thenumeric value of the character in the machine's character set. For example,in the ASCII character set the character constant <tt>'0'</tt> has the value 48,which is unrelated to the numeric value 0. If we write <tt>'0'</tt> instead ofa numeric value like 48 that depends on the character set, the program isindependent of the particular value and easier to read. Character constantsparticipate in numeric operations just as any other integers, although theyare most often used in comparisons with other characters.<p>Certain characters can be represented in character and string constants byescape sequences like <tt>\n</tt> (newline); these sequences look like twocharacters, but represent only one. In addition, an arbitrary byte-sized bitpattern can be specified by<pre> '\<em>ooo</em>'</pre>where <em>ooo</em> is one to three octal digits (0...7) or by<pre> '\x<em>hh</em>'</pre>where <em>hh</em> is one or more hexadecimal digits (<tt>0...9, a...f, A...F</tt>).So we might write<pre> #define VTAB '\013' /* ASCII vertical tab */ #define BELL '\007' /* ASCII bell character */</pre>or, in hexadecimal,<pre> #define VTAB '\xb' /* ASCII vertical tab */ #define BELL '\x7' /* ASCII bell character */</pre>The complete set of escape sequences is<p><table align="center" border=1><td> <tt>\a</tt> </td><td> alert (bell) character </td><td> <tt>\\</tt></td><td> backslash</td><tr><td> <tt>\b</tt> </td><td> backspace</td><td> <tt>\?</tt></td><td> question mark</td><tr><td> <tt>\f</tt> </td><td> formfeed</td><td> <tt>\'</tt></td><td> single quote</td><tr><td> <tt>\n</tt> </td><td> newline</td><td> <tt>\"</tt> </td><td> double quote</td><tr><td> <tt>\r</tt> </td><td> carriage return</td><td> <tt>\</tt><em>ooo</em> </td><td> octal number</td><tr><td> <tt>\t</tt> </td><td> horizontal tab</td><td> <tt>\x</tt><em>hh</em> </td><td> hexadecimal number </td><tr><td> <tt>\v</tt> </td><td> vertical tab</td></table><p>The character constant <tt>'\0'</tt> represents the character with value zero,the null character. <tt>'\0'</tt> is often written instead of <tt>0</tt> toemphasize the character nature of some expression, but the numeric value isjust 0.<p>A <em>constant expression</em> is an expression that involves only constants.Such expressions may be evaluated at during compilation rather than run-time,and accordingly may be used in any place that a constant can occur, as in<pre> #define MAXLINE 1000 char line[MAXLINE+1];</pre>or<pre> #define LEAP 1 /* in leap years */ int days[31+28+LEAP+31+30+31+30+31+31+30+31+30+31];</pre>A <em>string constant</em>, or <em>string literal</em>, is a sequence of zeroor more characters surrounded by double quotes, as in<pre> "I am a string"</pre>or<pre> "" /* the empty string */</pre>The quotes are not part of the string, but serve only to delimit it. The sameescape sequences used in character constants apply in strings; <tt>\"</tt>represents the double-quote character. String constants can be concatenatedat compile time:<pre> "hello, " "world"</pre>is equivalent to<pre> "hello, world"</pre>This is useful for splitting up long strings across several source lines.<p>Technically, a string constant is an array of characters. The internalrepresentation of a string has a null character <tt>'\0'</tt> at the end, sothe physical storage required is one more than the number of characterswritten between the quotes. This representation means that there is no limitto how long a string can be, but programs must scan a string completely todetermine its length. The standard library function <tt>strlen(s)</tt> returnsthe length of its character string argument <tt>s</tt>, excluding the terminal<tt>'\0'</tt>. Here is our version:<pre> /* strlen: return length of s */ int strlen(char s[]) { int i; while (s[i] != '\0') ++i; return i; }</pre><tt>strlen</tt> and other string functions are declared in the standard header<tt><string.h></tt>.<p>Be careful to distinguish between a character constant and a string thatcontains a single character: <tt>'x'</tt> is not the same as <tt>"x"</tt>. Theformer is an integer, used to produce the numeric value of the letter <em>x</em> inthe machine's character set. The latter is an array of characters thatcontains one character (the letter <em>x</em>) and a <tt>'\0'</tt>.<p>There is one other kind of constant, the <em>enumeration constant</em>. Anenumeration is a list of constant integer values, as in<pre> enum boolean { NO, YES };</pre>The first name in an <tt>enum</tt> has value 0, the next 1, and so on, unlessexplicit values are specified. If not all values are specified, unspecifiedvalues continue the progression from the last specified value, as the secondof these examples:<pre> enum escapes { BELL = '\a', BACKSPACE = '\b', TAB = '\t', NEWLINE = '\n', VTAB = '\v', RETURN = '\r' }; enum months { JAN = 1, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC }; /* FEB = 2, MAR = 3, etc. */</pre>Names in different enumerations must be distinct. Values need not be distinctin the same enumeration.<p>Enumerations provide a convenient way to associate constant values withnames, an alternative to <tt>#define</tt> with the advantage that the valuescan be generated for you. Although variables of <tt>enum</tt> types may bedeclared, compilers need not check that what you store in such a variable isa valid value for the enumeration. Nevertheless, enumeration variables offerthe chance of checking and so are often better than <tt>#define</tt>s. Inaddition, a debugger may be able to print values of enumeration variables intheir symbolic form.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -