重要知识点摘记_嗨翻C语言书评-查字典图书网
查字典图书网
当前位置: 查字典 > 图书网 > 编程 > 嗨翻C语言 > 重要知识点摘记
杰克小薛 嗨翻C语言 的书评 发表时间:2014-03-13 22:03:29

重要知识点摘记

=== Basics ===
> simple statements are commands
> block statements are surrounded by { and }
> if statements run code if something is true
> switch statements efficiently check for multiple values of a variable
> you can combine conditions together with $$ and ||
> every program needs a main founction
> #include include external code for things like input and output
> your source files should have a name ending in .c
> you need to compile your C program before you run it
> gcc is the most popular C compiler
> you can use the $$ operator on the command line to only run your program if it compiles
> -o specifies the output file
> count++ means add 1 to count
> count-- means subtract 1 from count
> while repeats code as long as a condition is true
> do-while loops run code at least once
> for loops are a more compact way of writing loops

=== Pointers and memory ===
> scanf("%i", $x) will allow a user to enter a number x directly
> a char pointer variable x is declared as char *x
> initialize a new array with a string, and it will copy it
> $x is called a pointer to x
> $x returns the address of x
> array variables can be used as pointers
> read the contents of an address a with *a
> fgets(buf, size, stdin) is a simpler way to enter text
> local variable are stored on the stack

=== Strings ===
> literal strings are stored in read-only memory
> the string.h header contains useful string functions
> an array of strings is an array of arrays
> you create an array of arrays using char strings[][]
> strstr(a, b) will return the address of string b in string a
> strcmp() compares two strings
> strcat() concatenates two string together
> strchr() finds the location of a character inside a string
> strcpy() copies one string to another
> strlen() finds the length of a string

=== Data streams ===
> C functions like printf() and scanf() use the Standard Output and Standard Input to communicate
> the Standard Output goes to the display by default
> the Standard Input reads from the keyboard by default
> you can change where the Standard Input, Output, and Error are connected to using redirection
> the Standard Error is a separate output intended for error messages
> you can print to the Standard Error using fprintf(stderr, ...)
> you can create custom data streams with fopen("filename", mode)
> 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

=== Data types ===
> chars are nubmers
> use longs for really big whole numbers
> use shorts for small whole numbers
> use ints for most whole numbers
> ints are different sizes on different machines
> use floats for most floating points
> use doubles for really precise floating points

=== Multiple files ===
> split function declarations from definitions
> put declarations in a header file
> #include <> for library headers
> #include "" for local headers
> save object code into files to speed up your builds
> use make to manage your builds

=== Compilation behind the scenes ===
1. Preprocessing: fix the source
2. Compilation: translate into assembly
3. Assembly: generate the object code
4. Linking: put it all together

=== Structs ===
> a struct combines data types together
> you can read struct fields with dot notation
> you can initialize structs with {array, like, notation}
> -> notation lets you easily update fields using a struct pointer
> typedef lets you create an alias for a data type
> designated initializers let you set struct and union fields by name

=== Unions and bitfields ===
> unions can hold different data types in one location
> enums let you create a set of symbols
> bitfields give you control over the exact bits stored in a struct

=== Data structures ===
> dynamic data structures use recursive structs
> recursive structs contain one or more links to similar data
> a linked list is a dynamic data structure
> data can be inserted easily into a linked list
> a linked list is more extensible than an array

=== Dynamic memory ===
> the stack is used for local variables
> unlike the stack, heap memory is not automatically released
> malloc() allocates memory on the heap
> free() releases memory on the heap
> strdup() will create a copy of a string on the heap
> a memory leak is allocated memory you can no longer access
> valgrind can help you track down memory leaks

=== Advanced functions ===
> function pointers let you pass functions around as if they were data
> function pointers are the only pointers that don't need the * and $ operators, but you can use them if you want to
> the name of every function is a pointer to the function
> qsort() will sort an array
> each sort function needs a pointer to a comparator function
> comparator functions decide how to order two pieces of data
> arrays of function pointers can help run different functions for different types of data
> functions with a variable number of arguments are called "variadic"
> stdarg.h lets you create variadic functions

=== Static and dynamic libraries ===
> #include <> looks in standard directories such as /usr/include
> -L<name> adds a directory to the list of standard library directories
> -1<name> links to a file in standard directories such as /usr/lib
> -I<name> adds a directory to the list of standard include directories
> the ar command creates a library archive of object files
> library archives have names like libsomething.a
> library archives are statically linked
> "gcc -shared" converts object files into dynamic libraries
> dynamic libraries are linked at runtime
> dynamic libraries have different names on different operting systems
> dynamic libraries have .so, .dylib, .dll, or .dll.a extensions

=== Processes and commumication ===
> system() will run a string like a console command
> fork() duplicates the current process
> fork() + exec() creates a child process
> execl() = list of args
  execle() = list of args + environment
  execlp() = list of args + search on path
  execv() = arrary of args
  execve() = arrary of args + environment
  execvp() = array of args + search on path
> processes can communicate using pipe
> pipe() creates a communication pipe
> exit() stops the program immediately
> waitpid() waits for a process to finish
> fileno() finds the descriptor
> dup2() duplicates a data stream
> signals are messages from the O/S
> sigaction() lets you handle signals
> a program can send signals to itself with raise()
> alarm() sends a SIGALRM after a few seconds
> the kill command sends a signal
> simple processes do one thing at a time

=== Sockets and networking ===
> telnet is a simple network client
> create sockets with the socket() function
> servers BLAB:
  B = bind()
  L = listen()
  A = accept()
  B = begin talking
> use fork() to cope with serval clients at once
> DNS = domain name system
> getaddrinfo() finds addresses by domain

=== Threads ===
> threads allow a process to do more than one thing at the same time
> threads are "lightweight processes"
> POSIX threads (pthread) is a threading library
> pthread_create() creates a thread to run a function
> pthread_join() will wait for a thread to finish
> threads share the same global variables
> if two threads read and update the same variable, your code will be upredictable
> mutexes are locks that protect shared data
> pthread_mutex_lock() creates a mutex on code
> pthread_mutex_unlock() releases the mutex

展开全文


推荐文章

猜你喜欢

附近的人在看

推荐阅读

拓展阅读