Ever wished all those great C facts were in one place?
This is a roundup of all the C topics and principles we’ve covered in the book. Take a look at them, and see if you can remember them all. Each fact has the chapter it came from alongside it, so it’s easy for you to refer back if you need a reminder. You might even want to cut these pages out and tape them to your wall.
Simple statements are commands. |
if statements run code if something is true. |
You can combine conditions together with && and ||. |
#include includes external code for things like input and output. |
Block statements are surrounded by { and }. |
switch statements efficiently check for multiple values of a variable. |
Every program needs a main function. |
Your source files should have a name ending in .c. |
You need to compile your C program before you run it. |
You can use the && operator on the command line to only run your program if it compiles. |
count++ means add 1 to count. |
while repeats code as long as a condition is true. |
for loops are a more compact way of writing loops. |
gcc is the most popular C compiler. |
-o specifies the output file. |
count-- means subtract 1 from count. |
do-while loops run code at least once. |
scanf(“%i”, &x) will allow a user to enter a number x directly. |
Initialize a new array with a string, and it will copy it. |
&x returns the address of x. |
Read the contents of an address a with *a. |
Local variables are stored on the stack. |
A char pointer variable x is declared as char *x. |
&x is called a pointer to x. |
Array variables can be used as pointers. |
fgets(buf, size, stdin) is a simpler way to enter text. |
Literal strings are stored in read-only memory. |
An array of strings is an array of arrays. |
strstr(a, b) will return the address of string b in string a. |
strcat() concatenates two strings together. |
strcpy() copies one string to another. |
The string.h header contains useful string functions. |
You create an array of arrays using char strings [...][...]. |
strcmp() compares two strings. |
strchr() finds the location of a character inside a string. |
strlen() finds the length of a string. |
C functions like printf() and scanf() use the Standard Output and Standard Input to communicate. |
The Standard Input reads from the keyboard by default. |
The Standard Error is a separate output intended for error messages. |
You can create custom data streams with fopen(“filename”, mode). |
The Standard Output goes to the display by default. |
You can change where the Standard Input, Output, and Error are connected to using redirection. |
You can print to the Standard Error using fprintf(stderr,...). |
The mode can be “w” to write, “r” to read, or “a” to append. |
Command-line arguments are passed to main() as an array of string pointers. |
The getopt() function makes it easier to read command-line options. |
chars are numbers. |
Use shorts for small whole numbers. |
ints are different sizes on different machines. |
Use doubles for really precise floating points. |
Use longs for really big whole numbers. |
Use ints for most whole numbers. |
Use floats for most floating points. |
Split function declarations from definitions. |
#include <> for library headers. |
Save object code into files to speed up your builds. |
Put declarations in a header file. |
#include “” for local headers. |
Use make to manage your builds. |
A struct combines data types together. |
You can intialize structs with {array, like, notation}. |
typedef lets you create an alias for a data type. |
You can read struct fields with dot notation. |
-> notation lets you easily update fields using a struct pointer. |
Designated initializers let you set struct and union fields by name. |
unions can hold different data types in one location. |
Bitfields give you control over the exact bits stored in a struct. |
enums let you create a set of symbols. |
Dynamic data structures use recursive structs. |
A linked list is a dynamic data structure. |
A linked list is more extensible than an array. |
Recursive structs contain one or more links to similar data. |
Data can be inserted easily into a linked list. |
The stack is used for local variables. |
malloc() allocates memory on the heap. |
strdup() will create a copy of a string on the heap. |
valgrind can help you track down memory leaks. |
Unlike the stack, heap memory is not automatically released. |
free() releases memory on the heap. |
A memory leak is allocated memory you can no longer access. |
Function pointers let you pass functions around as if they were data. |
The name of every function is a pointer to the function. |
Each sort function needs a pointer to a comparator function. |
Arrays of function pointers can help run different functions for different types of data. |
Function pointers are the only pointers that don’t need the * and & operators, but you can use them if you want to. |
qsort() will sort an array. |
Comparator functions decide how to order two pieces of data. |
Functions with a variable number of arguments are called “variadic.” |
stdarg.h lets you create variadic functions. |
#include <> looks in standard directories such as /usr/include. |
-l<name> links to a file in standard directories such as /usr/lib. |
The ar command creates a library archive of object files. |
Library archives are statically linked. |
-L<name> adds a directory to the list of standard library directories. |
-I<name> adds a directory to the list of standard include directories. |
Library archives have names like libsomething.a. |
“gcc -shared” converts object files into dynamic libraries. |
Dynamic libraries are linked at runtime. |
Dynamic libraries have .so, .dylib, .dll, or .dll.a extensions. |
Dynamic libraries have different names on different operating systems. |
system() will run a string like a console command. |
fork() + exec() creates a child process. |
Processes can communicate using pipes. |
exit() stops the program immediately. |
fork() duplicates the current process. |
execl() = list of args. execle() = list of args + environment. execlp() = list of args + search on path. execv() = array of args. execve() = array of args + environment. execvp() = array of args + search on path. |
pipe() creates a communication pipe. |
waitpid() waits for a process to finish. |
fileno() finds the descriptor. |
Signals are messages from the O/S. |
A program can send signals to itself with raise(). |
The kill command sends a signal. |
dup2() duplicates a data stream. |
sigaction() lets you handle signals. |
alarm() sends a SIGALRM after a few seconds. |
Simple processes do one thing at a time. |
telnet is a simple network client. |
Servers BLAB: B = bind() L = listen() A = accept() B = Begin talking. |
DNS = Domain name system. |
Create sockets with the socket() function. |
Use fork() to cope with several clients at once. |
getaddrinfo() finds addresses by domain. |
Threads allow a process to do more than one thing at the same time. |
POSIX threads (pthread) is a threading library. |
pthread_join() will wait for a thread to finish. |
If two threads read and update the same variable, your code will be unpredictable. |
pthread_mutex_lock() creates a mutex on code. |
Threads are “lightweight processes.” |
pthread_create() creates a thread to run a function. |
Threads share the same global variables. |
Mutexes are locks that protect shared data. |
pthread_mutex_unlock() releases the mutex. |