Chapter 3

I l @ ve RuBoard

Chapter 3

    1. int , possibly short or unsigned or unsigned short ; population is a whole number.

    2. float ; it's unlikely the average will be an exact integer. (You could use double but don't really need the extra precision.)

    3. char .

    4. int , possibly unsigned .

  1. One reason is that long may accommodate larger numbers than int on your system; another reason is that if you do need to handle larger values, you improve portability by using a type guaranteed to be at least 32 bits on all systems.

    1. char constant (but stored as type int )

    2. int constant

    3. double constant

    4. unsigned int constant, hexadecimal format

    5. double constant

  2. Line 1:  

    Should be #include <stdio.h> .

    Line 2:  

    Should have a pair of parentheses containing void following main ; that is, main(void) .

    Line 3:  

    Use { , not ( .

    Line 4:  

    Should be a comma, not a semicolon, between g and h .

    Line 5:  

    Fine.

    Line 6:  

    (blank) Fine.

    Line 7:  

    There should be at least one digit before the e . Either 1e21 or 1.0e21 is okay.

    Line 8:  

    Fine.

    Line 9:  

    Use } , not ) .

    Missing lines:  

    First, rate is never assigned a value. Second, the variable h is never used. Also, the program never informs you of the results of its calculation. Neither of these errors will stop the program from running (although you might be given a warning about the unused variable), but they do detract from its already limited usefulness . Also, there should be a return statement at the end.

    Here is one possible correct version:

     #include <stdio.h> int main(void) {   float g, h;   float tax, rate;   rate = 0.08;   g = 1.0e21;   tax = rate*g;   h = g + tax;   printf("You owe $%f plus $%f in taxes for a total of $%f.\n", g, ->tax, h);   return 0; } 
  3. Constant Type Specifier
    a. 12 int %d
    b. 0X3 unsigned int %X
    c. 'C' char (really int ) %c
    d. 2.34E07 double %e
    e. '\040' . char (really int ) %c
    f. 7.0 double %f
    g. 6L long %ld
    h. 6.0f float %f
  4. Constant Type Specifier
    a. 012 unsigned int %o
    b. 2.9e05L long double %Le
    c. 's' char (really int ) %c
    d. 100000 long %ld
    e. '\n' char (really int ) %c
    f. 20.0f float %f
    g. 0x44 unsigned int %x
  5.  printf("The odds against the %d were %ld to 1.\n", imate, shot); printf("A score of %f is not an %c grade.\n", log, grade); 
  6.  ch = '\r'; ch = 13; ch = '5' ch = '\xd' 
  7. Line 0: It's better form to have #include <stdio.h> .

    Line 1: Use /* and */ .

    Line 3: int cows, legs;

    Line 5: count?\n");

    Line 6: %d , not %c

    Line 6: &legs

    Line 8: %d , not %f

    Add a return statement.

    Here's one correct version:

     #include <stdio.h> int main(void) /* this program is perfect */ {  int cows, legs;  printf("How many cow legs did you count?\n");  scanf("%d", &legs);  cows = legs / 4;  printf("That implies there are %d cows.\n", cows);  return 0; } 
    1. newline character

    2. a slash character

    3. a double quotation mark

    4. a tab character

I l @ ve RuBoard


C++ Primer Plus
C Primer Plus (5th Edition)
ISBN: 0672326965
EAN: 2147483647
Year: 2000
Pages: 314
Authors: Stephen Prata

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net