C Primer Plus (5th Edition)

I l @ ve RuBoard

Chapter 2

  1. They are called functions.

  2. A syntax error is a violation of the rules governing how sentences or programs are put together. Here's an example in English: "Me speak English good." Here's an example in C:

    printf"Where are the parentheses?";.

  3. A semantic error is one of meaning. Here's an example in English: "This sentence is excellent Italian." Here's a C example:

    thrice_n = 3 + n;

  4. Line 1: Begin the line with a # ; spell the file stdio.h ; place the filename within angle brackets.

    Line 2: Use () , not {} ; end comment with */ , not /* .

    Line 3: Use { , not ( .

    Line 4: Complete the statement with a semicolon.

    Line 5: Mr. I.B.M. got this one (the blank line) right!

    Line 6: Use = , not := for assignment. (Apparently, Mr. I.B.M. knows a little Pascal.) Use 52, not 56, weeks per year.

    Line 7: Should be

    printf("There are %d weeks in a year.\n", s);

    Line 9: There isn't a line 9, but there should be, and it should consist of the closing brace , } .

    Here's how the code looks after these changes:

    #include <stdio.h> int main(void) /* this prints the number of weeks in a year */ { int s; s = 52; printf("There are %d weeks in a year.\n", s); return 0; }

    1. Baa Baa Black Sheep.Have you any wool?

      (Note that there is no space after the period. You could have had a space by using " Have instead of "Have .)

    2. Begone! O creature of lard!

      (Note that the cursor is left at the end of the second line.)

    3. What? No/nBonzo?

      (Note that the slash ( / ) does not have the same effect as the backslash \ ; it simply prints as a slash.)

    4. 2 + 2 = 4

      (Note how each %d is replaced by the corresponding variable value from the list. Note, too, that + means addition and that calculation can be done inside a printf() statement.)

  5. int and char ( main is a function name , and = is an operator).

  6. printf("There were %d words and %d lines.\n", words, lines);

  7. After line 7, a is 5 and b is 2. After line 8, both a and b are 5. After line 9, both a and b are still 5. Note that a can't be 2 because by the time you say a = b; , b has already been changed to 5.

I l @ ve RuBoard

Категории