owned this note
owned this note
Published
Linked with GitHub
---
title: The C Programming Language
tags: 读书笔记
---
# The C Programming Language
- Types, Operators and Expression
- Control Flow
- Functions and Program Structures
- Pointers and Arrays
- Structures
- I/O
- Unix Interface
Pointers and Structures are critical part of C.
[My exercise solutions](https://github.com/wangzhe3224/Solutions_The_C_Programming_Language)
## 2. Types, Operators and Expressions
Use `strcpy` to assig string.
## 3. Control Flow
`switch, for, while, do-while, goto, break, continue`
## 4. Functions and Program Structure
`char` is `int` type but only got 8bit, which is smaller than `int`.
C does not allow to define a function inside another function.
Split functions into different source code file.
There is no `;` in `#define`
Use header files to store definitions.
`static` will limit the global variables inside the source code file scope. This means that internal static variables provide private, permanent storage within a single function.
`register` tells the compiler that the variable will be used heavily.
COnditional Inclusion is used in the pre-processor to have dynamic definition.
## 5. Pointer
pointers are variables.
`*ptr` is value the it points to, `&value` is the pointer (location).
Unary operators, such as `++, *, &`, associate right to left.
By default C passes arguments to function by value.
The array name is like a pointer, but array name is not a variable! See example:
```
pa = &a[0];
pa = a; // the name of the array is the pointer to the first element'
// a[i] => *(a+i)
// a++ is not legal, while pa++ is legal.
// char s[] and char *s is equivalent.
```
```c
/* month_name: return name of n-th month */
char *month_name(int n)
{
static char *name[] = {
"Illegal month",
"January", "February", "March",
"April", "May", "June",
"July", "August", "September",
"October", "November", "December"
};
return (n < 1 || n > 12) ? name[0] : name[n];
}
```
```c
int (*comp)(void *, void *);
/* comp is a pointer to a function that has two void * arguments and returns an int. */
```
Idiom way to push/pop for stack
```c
*p++ = val;
val = *--p;
while (*s++ == *t++);
if (!*s) return 0;
```
Pointers to Pointers
Pointers to Functions
`int (*comp) (void *, void *)`: comp is a pointer to a function accepts two args and return an int.
`int *comp ()`: comp is a function that returns a pointer to an int.
## 6. Structures
A structure is a collection of one or more variables, possibly different types, grouped together under a single name.
A `struct` declaration defines a type. For example, `struct point` type.
Access member of structure.
`p->x // if p is a pointer to a structure`
`p.x // if p is a structure`
The structure operator, `.` and `->`, together with `()` and `[]` are at the top of the precedence hierarchy and bind very tightly.
## Reference:
- [Exercise Solutions](https://github.com/thvdburgt/KnR-The-C-Programming-Language-Solutions)
- [Emacs Config](http://tuhdo.github.io/c-ide.html#orgheadline4)