Appendix B. C Topics: Revision roundup

image with no caption

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.

Basics

Chapter 1

Simple statements are commands.

Chapter 1

if statements run code if something is true.

Chapter 1

You can combine conditions together with && and ||.

Chapter 1

#include includes external code for things like input and output.

Chapter 1

Block statements are surrounded by { and }.

Chapter 1

switch statements efficiently check for multiple values of a variable.

Chapter 1

Every program needs a main function.

Chapter 1

Your source files should have a name ending in .c.

Chapter 1

You need to compile your C program before you run it.

Chapter 1

You can use the && operator on the command line to only run your program if it compiles.

Chapter 1

count++ means add 1 to count.

Chapter 1

while repeats code as long as a condition is true.

Chapter 1

for loops are a more compact way of writing loops.

Chapter 1

gcc is the most popular C compiler.

Chapter 1

-o specifies the output file.

Chapter 1

count-- means subtract 1 from count.

Chapter 1

do-while loops run code at least once.

Pointers and memory

Chapter 2

scanf(“%i”, &x) will allow a user to enter a number x directly.

Chapter 2

Initialize a new array with a string, and it will copy it.

Chapter 2

&x returns the address of x.

Chapter 2

Read the contents of an address a with *a.

Chapter 2

Local variables are stored on the stack.

Chapter 2

A char pointer variable x is declared as char *x.

Chapter 2

&x is called a pointer to x.

Chapter 2

Array variables can be used as pointers.

Chapter 2

fgets(buf, size, stdin) is a simpler way to enter text.

Strings

Chapter 2

Literal strings are stored in read-only memory.

Chapter 2.5

An array of strings is an array of arrays.

Chapter 2.5

strstr(a, b) will return the address of string b in string a.

Chapter 2.5

strcat() concatenates two strings together.

Chapter 2.5

strcpy() copies one string to another.

Chapter 2.5

The string.h header contains useful string functions.

Chapter 2.5

You create an array of arrays using char strings [...][...].

Chapter 2.5

strcmp() compares two strings.

Chapter 2.5

strchr() finds the location of a character inside a string.

Chapter 2.5

strlen() finds the length of a string.

Data streams

Chapter 3

C functions like printf() and scanf() use the Standard Output and Standard Input to communicate.

Chapter 3

The Standard Input reads from the keyboard by default.

Chapter 3

The Standard Error is a separate output intended for error messages.

Chapter 3

You can create custom data streams with fopen(“filename”, mode).

Chapter 3

The Standard Output goes to the display by default.

Chapter 3

You can change where the Standard Input, Output, and Error are connected to using redirection.

Chapter 3

You can print to the Standard Error using fprintf(stderr,...).

Chapter 3

The mode can be “w” to write, “r” to read, or “a” to append.

Chapter 3

Command-line arguments are passed to main() as an array of string pointers.

Chapter 3

The getopt() function makes it easier to read command-line options.

Data types

Chapter 4

chars are numbers.

Chapter 4

Use shorts for small whole numbers.

Chapter 4

ints are different sizes on different machines.

Chapter 4

Use doubles for really precise floating points.

Chapter 4

Use longs for really big whole numbers.

Chapter 4

Use ints for most whole numbers.

Chapter 4

Use floats for most floating points.

Multiple files

Chapter 4

Split function declarations from definitions.

Chapter 4

#include <> for library headers.

Chapter 4

Save object code into files to speed up your builds.

Chapter 4

Put declarations in a header file.

Chapter 4

#include “” for local headers.

Chapter 4

Use make to manage your builds.

Structs

Chapter 5

A struct combines data types together.

Chapter 5

You can intialize structs with {array, like, notation}.

Chapter 5

typedef lets you create an alias for a data type.

Chapter 5

You can read struct fields with dot notation.

Chapter 5

-> notation lets you easily update fields using a struct pointer.

Chapter 5

Designated initializers let you set struct and union fields by name.

Unions and bitfields

Chapter 5

unions can hold different data types in one location.

Chapter 5

Bitfields give you control over the exact bits stored in a struct.

Chapter 5

enums let you create a set of symbols.

Data structures

Chapter 6

Dynamic data structures use recursive structs.

Chapter 6

A linked list is a dynamic data structure.

Chapter 6

A linked list is more extensible than an array.

Chapter 6

Recursive structs contain one or more links to similar data.

Chapter 6

Data can be inserted easily into a linked list.

Dynamic memory

Chapter 6

The stack is used for local variables.

Chapter 6

malloc() allocates memory on the heap.

Chapter 6

strdup() will create a copy of a string on the heap.

Chapter 6

valgrind can help you track down memory leaks.

Chapter 6

Unlike the stack, heap memory is not automatically released.

Chapter 6

free() releases memory on the heap.

Chapter 6

A memory leak is allocated memory you can no longer access.

Advanced functions

Chapter 7

Function pointers let you pass functions around as if they were data.

Chapter 7

The name of every function is a pointer to the function.

Chapter 7

Each sort function needs a pointer to a comparator function.

Chapter 7

Arrays of function pointers can help run different functions for different types of data.

Chapter 7

Function pointers are the only pointers that don’t need the * and & operators, but you can use them if you want to.

Chapter 7

qsort() will sort an array.

Chapter 7

Comparator functions decide how to order two pieces of data.

Chapter 7

Functions with a variable number of arguments are called “variadic.”

Chapter 7

stdarg.h lets you create variadic functions.

Static and dynamic libraries

Chapter 8

#include <> looks in standard directories such as /usr/include.

Chapter 8

-l<name> links to a file in standard directories such as /usr/lib.

Chapter 8

The ar command creates a library archive of object files.

Chapter 8

Library archives are statically linked.

Chapter 8

-L<name> adds a directory to the list of standard library directories.

Chapter 8

-I<name> adds a directory to the list of standard include directories.

Chapter 8

Library archives have names like libsomething.a.

Chapter 8

“gcc -shared” converts object files into dynamic libraries.

Chapter 8

Dynamic libraries are linked at runtime.

Chapter 8

Dynamic libraries have .so, .dylib, .dll, or .dll.a extensions.

Chapter 8

Dynamic libraries have different names on different operating systems.

Processes and communication

Chapter 9

system() will run a string like a console command.

Chapter 9

fork() + exec() creates a child process.

Chapter 10

Processes can communicate using pipes.

Chapter 10

exit() stops the program immediately.

Chapter 9

fork() duplicates the current process.

Chapter 9

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.

Chapter 10

pipe() creates a communication pipe.

Chapter 10

waitpid() waits for a process to finish.

Chapter 10

fileno() finds the descriptor.

Chapter 10

Signals are messages from the O/S.

Chapter 10

A program can send signals to itself with raise().

Chapter 10

The kill command sends a signal.

Chapter 10

dup2() duplicates a data stream.

Chapter 10

sigaction() lets you handle signals.

Chapter 10

alarm() sends a SIGALRM after a few seconds.

Chapter 12

Simple processes do one thing at a time.

Sockets and networking

Chapter 11

telnet is a simple network client.

Chapter 11

Servers BLAB:

B = bind()

L = listen()

A = accept()

B = Begin talking.

Chapter 11

DNS = Domain name system.

Chapter 11

Create sockets with the socket() function.

Chapter 11

Use fork() to cope with several clients at once.

Chapter 11

getaddrinfo() finds addresses by domain.

Threads

Chapter 12

Threads allow a process to do more than one thing at the same time.

Chapter 12

POSIX threads (pthread) is a threading library.

Chapter 12

pthread_join() will wait for a thread to finish.

Chapter 12

If two threads read and update the same variable, your code will be unpredictable.

Chapter 12

pthread_mutex_lock() creates a mutex on code.

Chapter 12

Threads are “lightweight processes.”

Chapter 12

pthread_create() creates a thread to run a function.

Chapter 12

Threads share the same global variables.

Chapter 12

Mutexes are locks that protect shared data.

Chapter 12

pthread_mutex_unlock() releases the mutex.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset