Chapter 4

I l @ ve RuBoard

Chapter 4

  1. The program bombs . The first scanf() statement reads just your first name, leaving your last name untouched but still stored in the input "buffer." (This buffer is just a temporary storage area used to store the input.) When the next scanf() statement comes along looking for your weight, it picks up where the last reading attempt ended, and tries to read your last name as your weight. This frustrates scanf() . On the other hand, if you respond to the name request with something like Lasha 144 , it uses 144 as your weight, even though you typed it before your weight was requested .

    1. He sold the painting for $234.50.

    2. Hi!

      (Note: The first character is a character constant, the second is a decimal integer converted to a character, and the third is an ASCII representation of a character constant.)

    3.  His Hamlet was funny without being vulgar. has 42 characters. 
    4. Is 1.20e+003 the same as 1201.00?

  2. Use \" , as in the following:

     printf("\"%s\"\nhas %d characters.\n", Q, strlen(Q)); 
  3. Here is a corrected version:

     #include <stdio.h>   /* don't forget this    */ #define B "booboo"   /* add #, quotes        */ #define X 10         /* add #                */ int main(void)       /* instead of main(int) */ {    int age;    int xp;          /* declare all variables */    char name[40];   /* make into an array    */    printf("Please enter your first name.\n"); /* \n for readability */    scanf("%s", name);    printf("All right, %s, what's your age?\n", name); /* %s for ->string */    scanf("%d", &age);       /* %d, not %f, &age, not age */    xp = age + X;    printf("That's a %s! You must be at least %d.\n", B, xp);    return 0;                /* not rerun                 */ } 
  4. Recall the %% construction for printing % .

     printf("This copy of \"%s\" sells for $%0.2f.\n", BOOK, cost); printf("That is %0.0f%% of list.\n", percent); 
    1. %d

    2. %4X

    3. %10.3f

    4. %12.2e

    5. %-30s

    1. %15lu

    2. %#4x

    3. %-12.2E

    4. %+10.3f

    5. %8.8s

    1. %6.4d

    2. %*o

    3. %2c

    4. %+0.2f

    5. %-7.5s

    1.  int dalmations;    scanf("%d", &dalmations); 
    2.  float kgs, share;    scanf("%f%f", &kgs, &share); 

      Note: For input, e , f , and g can be used interchangeably. Also, for all but %c , it makes no difference if you leave spaces between the conversion specifiers.

    3.  char name[20];    scanf("%s", name); 
    4.  char action[20];    int value;    scanf("%s %d", action, &value); 
    5.  int value;    scanf("%*s %d", &value); 
  5. Whitespace consists of spaces, tabs, and newlines; C uses whitespace to separate tokens from one another; scanf() uses whitespace to separate consecutive input items from each other.

  6. The substitutions would take place. Unfortunately, the preprocessor cannot discriminate between those parentheses that should be replaced with braces and those that should not. Therefore,

     #define ( { #define ) } int main(void) (     printf("Hello, O Great One!\n"); ) <AP>         becomes int main{void} {     printf{"Hello, O Great One!\n"}; } 
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