/the-practice-of-programming-solutions

Provide high quality solutions to the exercises in "The Practice of Programming" by Kernighan and Pike, also referred to as "TPOP".

Primary LanguageC

The Practice of Programming Solutions

Provide high quality solutions to the exercises in "The Practice of Programming" by Kernighan and Pike, also referred to as "tpop".

Table of Contents

Chapter 1. Style

  • Exercise 1-2. Improve this function:
?    int smaller(char *s, char *t) {
?        if (strcmp(s, t) < 1)
?            return 1;
?        else
?            return 0;
?    }

solution

  • Exercise 1-4. Improve each of these fragments:
?    if ( !(c == 'y' || c == 'Y') )
?        return;

?    length = (length < BUFSIZE) ? length : BUFSIZE;

?    flag = flag ? 0 : 1;

?    quote = (*line == '"') ? 1: 0;

?    if (val & 1)
?        bit = 1;
?    else
?        bit = 0;

solution

  • Exercise 1-5. What is wrong with this excerpt?
?    int read(int *ip) {
?        scanf("%d", ip);
?        return *ip;
?    }

?    insert(&graph[vert], read(&val), read(&ch));

solution

  • Exercise 1-6. List all the different outputs this could produce with various orders of evaluation:
?    n = 1;
?    printf("%d, %d\n", n++, n++);

Try it on as many compilers as you can, to see what happens in practice. solution