C Tutorial
Nan Zhu 2013.2
Outline
Why learn C? & Experiences on learning a programming language? Structure of C programs How C Compiler works Practical C Knowledge
Special Types Flow Control Statements Memory Management
Outline
Why learn C? & How to learn a programming language? Structure of C programs How C Compiler works Practical C knowledge
Special Types Flow Control Statements Memory Management
Why C?
Powerful C
The backbone of Internet is build with C Linux/Unix, OS of most servers providing service, is written in C Facebook is build above ? Not PHP, not Javascript Facebook High Performance C/C++ Library: Folly; (latest news, Google released their high performance c++ library b-tree containers yesterday) Drivers of many hardware is written in C Most of compilers/interpreters, for PHP, Java, Javascript, are written in C/C++
Personal Ideas
Most of modern languages hide many programming details for programmers, bringing higher productivity(if youre really good at those languages), but less chances to build your programming skills
Learn a Programming Language
1. Basic Syntax
if else, while, for Follow the examples in textbook
2. Practice
C/C++, C#, Java, etc. (first language) Algorithms Introduction to Algorithms Algorithms in a Nutshell PHP, Perl, etc. Jump to Step 3
3. Project
Write real/complete programs Manage your image/pdf files in computer Projects from professors in SOCS You need a bible-level book
Outline
Why learn C? & How to learn a programming language? Structure of C programs How C Compiler works Practical C knowledge
Special Types Flow Control Statements Memory Management
Preprocessor Directives
Structure of C Programs
Global Declaration
Function Declaration Function Body
The Structure of C Program
Preprocessor Directives:
Start with a # Include header files (#include): the preprocessor (one of the components of compiler) will expand the text of included file to the directive caller (your program source file is an good example) #include myheader.h, myheader.h must be in the same directory with the caller, or you can specify the path of myheader.h, e.g. /home/user/proj/ myheader.h #include <stdio.h>, stdio.h must be in the system include path, specify I parameter in your compiling option #define: define some macros, which will be replaced during compilation #define MAX_INT 10000, you can then use MAX_INT in your program instead of using 10000 to refer to the maximum integer in your program Conditional Compilation: #ifndef #define #endif, usually used when you want to print more debug information Very useful in multi-file project or release&debug version project
[Link]
The Structure of C Program
Global Declaration
Compilation unit: one single .c file as well as all included header files Every variable declared out of an body area of function is considered as global variable int global_var = 0; Access the global variable declared in another compile unit Declare for another time, like int global_var = 0; again? No! you will receive an error like duplicate symbol extern int global_var = 0; is the right way to do; keyword extern will instruct the compiler to link the variable name with the actual address which must appear in somewhere else
The Structure of C Program
Function Declaration
return_type function_name(type1 parameter1, type2 parameter2, ) The program will start from int main(int argc, char **argv) in Linux, its a convenient to make the return type of main as int(in windows, it can be void), 0 the program can return without any error, 1(other values) errors happened argc number of command line options argv command line options e.g. ls l argc == 2, argv[0] is ls(the command name itself), argv[1] is -l
The Structure of C Program
Function body
The main component of C programs The body of the function is always wrapped by {} We call the statements encapsulated in {} as block
Effect Scope
File Scope: global variables, (extern is to extend the scope of that variable) Block Scope: the variables declared inside a block Function scope: the variable declared inside a function, or as function parameter
The Structure of C Program
Scope
A bad Example from many textbooks int x = 0; Bad Coding Style !!! if condition One of the principle to write good { code: int x = 0; x = 1; } printf(%d\n, x); self-explain: you can add comments in your code to explain what the code is doing, but the best code is that other programmers can understand your code from the name of your variables, clear logic, and good architecture design
Outline
Why learn C? & How to learn a programming language? Structure of C programs How C Compiler works Practical C Knowledge
Special Types Flow Control Statements Memory Management
How C Compiler works
How C Compiler works
Linker
Static Linking copying all library routines used in the program into the executable image. require more disk space and memory faster and more portable Dynamic linking placing the name of a sharable library in the executable image Actual linking with the library routines does not occur until the image is run multiple programs can share a single copy of the library.
How C Compiler works
Compilers
gcc The GNU compiler collections Includes front ends for C, C++, Objective-C, Fortran, Java, Ada and Go clang Started as a research project in UIUC([Link] a complete compiler based on LLVM Compiler Infrastructure High probability to replace gcc Latest version of FreeBSD has make it as the default compiler
How C Compiler Works
GCC command line options format
gcc COMPILER_OPTIONS source_file -o OUTPUT_FILE
Compile a single source file to executable (foo.c)
gcc foo.c o foo
Usual compiler options
-o outfile, generate the executable as outfile -c, generate object file instead of executable (useful when you are compiling a file without main function) -g, add debugging information to output file, and then you can use gdb and objdump to debug your program -Idir, add dir as the standard header files directory -Ldir, add dir as the standard libraries directory -Wall, print all warnings -O1 O2 O3, optimize your program while keeping the correctness More optimizations, more compile time
How C Compiler Works
Linux kernel consists of (tens) of thousands of c files
Compile them one by one?
GNU Make
a tool which controls the generation of executables and other nonsource files of a program from the programs source files Makefile Contains the rule telling Make how to execute a series of commands in order to build a target file from source files
How C Compiler Works
Write your own Makefile
What a simple rule looks like: target: dependencies <TAB>commands Target: can be an object file, like foo.o; an executable, like foo; or a label specified in the same Makefile Dependencies: before executing the commands to generate target, the rule about dependencies(specified in the same Makefile) will be executed first Commands: the commands to generate target E.g. gcc abc.c o abc
How C Compiler Works
Goal, in a project with 3 source files, main.c, module1.c, module2.c, generate executable proj First Version of Makefile
CC=gcc CC_FLAGS= -O2 c g OBJECTS=main.o module1.o module2.o proj: $(OBJECTS) $(CC) $(OBJECTS) o proj main.o: $(CC) $(CC_FLAGS) main.c o main.o module1.o: $(CC) $(CC_FLAGS) module1.c o module1.o module2.o: $(CC) $(CC_FLAGS) module2.c o module2.o
How C Compiler Works
Simpler Makefile
CC=gcc CC_FLAGS= -O2 c g OBJECTS=main.o module1.o module2.o proj: $(OBJECTS) $(CC) $(OBJECTS) $^ o $@ main.o:main.c $(CC) $(CC_FLAGS) $< module1.o:module1.c $(CC) $(CC_FLAGS) $< module2.o:module2.c $(CC) $(CC_FLAGS) $<
How C Compiler Works
Using Wildcards
Complicated to define a target for every module CC=gcc CC_FLAGS= -O2 c g OBJECTS=main.o module1.o module2.o proj: $(OBJECTS) $(CC) $(OBJECTS) $^ o $@ OBJECTS: %.o:%.c $(CC) $(CFLAGS) $<
Outline
Why learn C? & How to learn a programming language? Structure of C programs How C Compiler works Practical C Knowledge
Special Types Flow Control Statements Memory Management
Basic C Syntax
Declare Variables
type_name variable_name Examples int distance = 20; float power = 2.345f; double d_power = 123.45678; char c = A; char first_name[10]; int *p = NULL;
Basic C Syntax
Some special types
Array Declare and initialization int number[4] = {0}; int number[4] = {0, 1, 2, 3}; Set value number[2] = 1; String C stores string in an array of bytes char carray[4] = {a, b, c, d}; We can also access array via a pointer for facilitating operation point_array.c
+ 1 will be translated according to the type of pointer, its can be translated by the base_address(cur_age in this case) + 1 * sizeof(type) (int in this case, 4 bytes)
Basic C Syntax
Operate Array with Pointer
Basic C Syntax
Understand Pointer [Link] is a kind of variable, it occupies memory in your computer [Link] pointer variables value is integer, this integer is actually a memory address
[Link] can assign 0 to a integer variable or an integer pointer, but in the second case, 0 will be explained as the memory address 0 (NULL);
[Link] can use * to directly access the value stored in the memory address pointed by the pointer
Basic C Syntax
Some special types
Pointers pointer_example.c
Basic Control Structure
FOR Loops
Workflow The INITIALIZER is code that is run to setup the loop, in this case i = 0. Next the TEST boolean expression is checked, and if it's false (0) then CODE is skipped, doing nothing. The CODE runs, does whatever it does. After the CODE runs, the INCREMENTER part is run, usually incrementing something, like in i++. And it continues again with Step 2 until the TEST is false (0).
Basic Control Structure
While Loops
Workflow The INITIALIZER is code that is run to setup the loop, in this case i = 0. Next the TEST boolean expression is checked, and if it's false (0) then CODE is skipped, doing nothing. The CODE runs, does whatever it does. After the CODE runs, the INCREMENTER part is run, usually incrementing something, like in i++. And it continues again with Step 2 until the TEST is false (0).
Basic Control Structure
Condition Statement
Function Parameter Passing
Swap two integers int swap(int a, int b) { t = a; a = b; } b = t;
Why this function doesnt work? When you pass the values to function parameter, the compiler will make a copy of them, e.g. int m = 0; int n = 1; swap(m, n); Compiler will make a copy of m, n as m, n, so, what youre operating is m and n not m, n
Function Parameter Passing
void foo(int * p) { } p = (int *) malloc(sizeof(int) * 10);
Course of many bugs in C
int *a = NULL; foo(a); a == NULL or not? Why?
Dynamic Memory Management
C/C++ is always used in scenarios with high performance requirement (real time system, infrastructure level system)
C is different because it's using the real CPU's actual machinery to do its work Java, Python using bytecode, programs written in these languages are running within their own virtual machines(running environment) Involves two partitions of memory, stack & heap Next page
Dynamic Memory Management
Stack v.s. Heap
Heap Allocation: Dynamic, slow, malloc Access: pointer The heap will be pointed by a ptr, but pointers are not always pointing to a heap area Manage: manually Memory leak Stack Allocation: the maximum size is determined when your program is started, much faster than heap Access: variables declared within functions are always in stack Manage: automatic
Common interview questions for summer interns
Dynamic Memory Management
All variables declared within functions are stored in stack (except those assigned with malloc-like functions)
int * foo(int para_a, int para_b) { int *a = (int *) malloc(sizeof(int) * 10); int b = 0; } return a;
a is in heap; b in in stack;
Stack is a memory block assigned to each function, all the block will be destroyed except the return value.
Dynamic Memory Management
Whats the problem in following code snippets? int * getRandomArray(int array_length) { int ret[array_length]; srand(time(NULL)); for (int i = 0; i < array_length; i++) ret[i] = rand(); } return ret;
A very common bug
My TA Hour: Tuesday 2:00 3:00 p.m. 3070
Thank You !