Skip to content

Instantly share code, notes, and snippets.

@weakish
Created July 22, 2017 07:29
Show Gist options
  • Save weakish/9dfaa8be54997a82c84147e7077cc694 to your computer and use it in GitHub Desktop.
Save weakish/9dfaa8be54997a82c84147e7077cc694 to your computer and use it in GitHub Desktop.
#afterwords on #K&R #C #Programming 2ed.

K & R The C Programming Language 2ed.

Section 4.2

The function atof must be declared and defined consistently. If atof itself and the call to it in main have inconsistent types in the same source file, the error will be detected by the compiler. But if (as is more likely) atof were compiled separately, the mismatch would not be detected (p.63)

Actually both gcc and clang can detect the mismatch across files. (Tested on gcc 4.8.2 and clang 3.4.)

the arguments to scanf and sscanf must be pointers. By far the most common error is writing scanf("%d", n); instead of scanf("%d", &n); This error is not generally detected at compile time. (p. 130)

Nowadays both gcc and clang will warn against this.

fabs(x) absolute value of x (p.136)

Actually absolute value of float x, thus the name. C99 adds abs for int.

dirwalk

void dirwalk(char *dir, void (*fcn)(char *)) (p.147)
(*fcn)(name); (p.148)

Both (*fcn) can be replaced with fcn. This alternative syntax is added by C89 standard. So this code is probably from K&R's first edition and did not get updated.

sbrk returns -1 if there was no space, even though NULL could have been a better design. (p. 152)

Currently on Linux it returns (void *) -1.

License

0BSD

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment