1、A First Book of ANSI CFourth EditionChapter 14Additional CapabilitiesA First Book of ANSI C,Fourth Edition2Objectives Additional Features Bit Operations Macros Command-Line Arguments Common Programming and Compiler ErrorsA First Book of ANSI C,Fourth Edition3Additional Features The typedef declarati
2、on statement Conditional preprocessor directives Enumerated constants Conditional expressions The goto statementA First Book of ANSI C,Fourth Edition4The typedef Declaration Statement typedef permits constructing alternate names for an existing C data type name typedef double REAL;makes the REAL an
3、alias for double REAL val;is equivalent to double val;typedef does not create a new data type The equivalence produced by typedef can frequently be produced equally well by#define typedef processed by the compiler#define processed by the preprocessorA First Book of ANSI C,Fourth Edition5The typedef
4、Declaration Statement(continued)Another example:typedef int ARRAY100;ARRAY first,second;Equivalent to the two definitions int first100;and int second100;As another example:typedef struct char name20;int idNum;empRecord;empRecord employee75;A First Book of ANSI C,Fourth Edition6Conditional Preprocess
5、or Directives#ifndef and#ifdef permit conditional compilation in that the statements immediately following these directives,up to an#else or#endif,are compiled only if the condition is true,whereas the statements following the#else are compiled only if the condition is false For example,#ifndef cond
6、ition compile the statements placed here#else compile the statements placed here#endifA First Book of ANSI C,Fourth Edition7Conditional Preprocessor Directives(continued)The#else directive is optional#ifndef is the most frequently used conditional preprocessor directive Its most common usage is in t
7、he form#ifndef header-file#include#endif For example:#ifndef stdio.h#include#endifA First Book of ANSI C,Fourth Edition8Enumerated Constants Set of related integer values can be equated to an equivalent set of constants using an enumerated list enum Mon,Tue,Wed,Thr,Fri,Sat,Sun;By default,the first e
8、numerated name in an enumerated list has a value of 0 The enumeration above is equivalent to:#define Mon 0#define Tue 1#define Wed 2 A First Book of ANSI C,Fourth Edition9Enumerated Constants(continued)To specify the value of the first enumerated name:enum Mon=1,Tue,Wed,Thr,Fri,Sat,Sun;Any integer c
9、onstant can be equated to enumerated names;they need not be in sequence,and an optional enumerated list name can be used enum escsequences BELL=a,BACKSPACE=b,NEWLINE=n,RETURN=r,TAB=t;Enumerated constants can be any valid user-created identifier,but each name must be unique OK for two constants to be
10、 equated to the same integer valueA First Book of ANSI C,Fourth Edition10Conditional Expressions A conditional expression uses the conditional operator,?:and provides an alternate way of expressing a simple if-else statement expression1?expression2:expression3 For example,the if-else statementif(hou
11、rs 40)rate=0.045;else rate=0.02;Can be replaced withrate=(hours 40)?0.045:0.02;?:is unique in C in that it is a ternary operatorA First Book of ANSI C,Fourth Edition11Conditional Expressions(continued)Conditional expressions are only useful in replacing if-else statements when the expressions in the
12、 equivalent if-else statement are not long or complicated For example,the statement maxVal=a b?a:b;is a one-line statement that assigns the maximum value of the variables a and b to maxVal A longer,equivalent form of this statement isif(a b)maxVal=a;else maxVal=b;A First Book of ANSI C,Fourth Editio
13、n12The goto Statement goto provides an unconditional transfer of control to some other statement in a program goto label;label is any unique name chosen according to the rules for creating variable names For example,if(denom=0.0)goto err;else result=num/denom;.err:printf(Error:Attempted Division by
14、Zero);A First Book of ANSI C,Fourth Edition13The goto Statement(continued)Generally,it is much easier to call an error routine for unusual conditions or use a break statement if this is necessary,rather than use a goto Theoretically,a goto is never required because Cs normal structures provide suffi
15、cient flexibility to handle all possible flow control requirements gotos tend to complicate programs Using even one goto statement in a program is almost always a sign of bad programming structureA First Book of ANSI C,Fourth Edition14Bit OperationsA First Book of ANSI C,Fourth Edition15The AND Oper
16、ator&causes a bit-by-bit AND comparison between its two operands AND operations are extremely useful in masking,or eliminating,selected bits from an operand For example,The 0s in op2 effectively mask the respective bits in op1,while the ones in op2 filter the respective bits in op1 through with no c
17、hange in their values In this example,op2 is called a maskA First Book of ANSI C,Fourth Edition16The AND Operator(continued)Program 14.1 produces the following output:325 ANDed with 263 is 221A First Book of ANSI C,Fourth Edition17The Inclusive OR Operator The result of an inclusive OR(|)bit compari
18、son is 1 if either bit is a 1;otherwise the result is a 0 Inclusive OR operations are extremely useful in forcing selected bits to take on a 1 value or for passing through other bit values unchanged For example,ORing with a 0 has the same effect as ANDing with a 1A First Book of ANSI C,Fourth Editio
19、n18The Inclusive OR Operator(continued)Program 14.2 produces the following output:325 ORed with 263 is 367A First Book of ANSI C,Fourth Edition19The Exclusive OR Operator The result of an exclusive OR()comparison is 1 if one and only one of the bits being compared is a 1;otherwise the result is 0 An
20、 exclusive OR operation can be used to create the opposite value,or complement,of any individual bit in a variable For example,A First Book of ANSI C,Fourth Edition20The Complement Operator is the unary complement operator If op1 contains the binary number 11001010,op1 replaces this number with 0011
21、0101 is used to force any bit in an operand to 0,independent of the number of bits used to store it op1&=07;sets the last 3 bits of op1 to 0 This statement is equivalent to the following:op1=op1&0177770;/*if an int has 16 bits*/op1=op1&027777777770;/*if it has 32 bits*/This makes the program portabl
22、e between machines using different integer storage sizesA First Book of ANSI C,Fourth Edition21Different-Sized Data Items When&,|,and are used with operands of different sizes,the shorter operand is increased in bit size to match the size of the larger operand When extending signed numbers,the origi
23、nal leftmost bit is reproduced in the additional bits that are added to the numberA First Book of ANSI C,Fourth Edition22Different-Sized Data Items(continued)A First Book of ANSI C,Fourth Edition23Different-Sized Data Items(continued)A First Book of ANSI C,Fourth Edition24The Shift Operators The lef
24、t shift operator,causes the bits in an operand to be shifted to the left by a given amount For example,p1=op1,causes the bits in an operand to be shifted to the right by a given amount For example,op1=op1 3;A First Book of ANSI C,Fourth Edition27The Shift Operators(continued)A First Book of ANSI C,F
25、ourth Edition28The Shift Operators(continued)A First Book of ANSI C,Fourth Edition29The Shift Operators(continued)The type of fill shown in Figures 14.7b and 14.7c,where the sign bit is reproduced in vacated bit positions,is known as an arithmetic right shift In an arithmetic right shift,each single
26、 shift to the right corresponds to a division by 2 Some computers automatically fill the vacated bits with 0s;this type of shift is known as a logical shift For positive signed numbers,where the leftmost bit is 0,both arithmetic and logical right shifts produce the same resultA First Book of ANSI C,
27、Fourth Edition30Macros In its simplest form,the#define preprocessor is used to equate constants and operators to symbolic names#define SALESTAX 0.05 The substitutions are made by the C preprocessor just prior to program compilation C places no restrictions on the equivalences that can be established
28、 with the#define statement When the equivalence consists of more than a single value,operator,or variable,the symbolic name is referred to as a macroA First Book of ANSI C,Fourth Edition31Macros(continued)For example,the equivalence established by the statement#define FORMAT The answer is%fnenables
29、us to write the statement printf(FORMAT,15.2);The compiler always receives the expanded version after the text has been inserted in place of the symbolic name by the preprocessorA First Book of ANSI C,Fourth Edition32Macros(continued)Equivalences can use arguments#define SQUARE(x)x*x y=SQUARE(num);i
30、s expanded to y=num*num;Advantage:since the data type of the argument is not specified,the macro can be used with any data type argument Be careful:val=SQUARE(num1+num2);is expanded to val=num1+num2*num1+num2;Solution:use#define SQUARE(x)(x)*(x)A First Book of ANSI C,Fourth Edition33Macros(continued
31、)Macros are extremely useful when the calculations or expressions they contain are relatively simple A macro definition can be continued on a new line by using a The advantage of using a macro instead of a function is an increase in execution speed No execution time loss due to the call and return p
32、rocedures required by a function Disadvantage:the increase in required program memory space when a macro is used repeatedly Each time a macro is used,the complete macro text is reproduced and stored as part of the program A function is stored in memory only onceA First Book of ANSI C,Fourth Edition3
33、4Command-Line ArgumentsA First Book of ANSI C,Fourth Edition35Command-Line Arguments(continued)You can use command-line arguments to pass arguments to a main()function C:pgm14.3 three blind mice To standardize arguments passing to main(),only two items are allowed:a number and an array The number is
34、 an integer variable,which must be named argc(short for argument counter)The array is a one-dimensional list,which must be named argv(short for argument values)A First Book of ANSI C,Fourth Edition36Command-Line Arguments(continued)A First Book of ANSI C,Fourth Edition37Command-Line Arguments(contin
35、ued)A First Book of ANSI C,Fourth Edition38Command-Line Arguments(continued)A First Book of ANSI C,Fourth Edition39Command-Line Arguments(continued)If the executable version of Program 14.3 is named pgm14.3.exe,a sample output for the command line pgm14.3 three blind mice is:The number of items on t
36、he command line is 4The address stored in argv0 is 3280036The character pointed to is pThe address stored in argv1 is 3280044The character pointed to is tThe address stored in argv2 is 3280050The character pointed to is bThe address stored in argv3 is 3280056The character pointed to is mA First Book
37、 of ANSI C,Fourth Edition40Command-Line Arguments(continued)A First Book of ANSI C,Fourth Edition41Command-Line Arguments(continued)A First Book of ANSI C,Fourth Edition42Common Programming Errors Forgetting that enumerated constants are a set of symbolic constants that equate to integers Using the
38、relational operators,and and Forgetting that each argument passed to main()is passed as a string valueA First Book of ANSI C,Fourth Edition43Common Compiler ErrorsA First Book of ANSI C,Fourth Edition44Summary A typedef statement creates synonym names,which are known as aliases,for any C data type n
39、ame A conditional expression provides an alternate way of expressing a simple if-else statement.C also provides a goto statement Individual bits of character and integer variables and constants can be manipulated using Cs bit operators The AND and inclusive OR operators are useful in creating masksA
40、 First Book of ANSI C,Fourth Edition45Summary(continued)When AND and OR are used with operands of different sizes,the shorter operand is always increased in bit size to match the size of the larger The shift operators produce different results depending on whether the operand is a signed or an unsigned value Using the#define command,complete expressions can be equated to symbolic names Arguments passed to main()are termed command-line argumentsA First Book of ANSI C,Fourth Edition46