McGill Comp 206 C Tutorial PDF
McGill Comp 206 C Tutorial PDF
McGill Comp 206 C Tutorial PDF
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
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
http://www.cplusplus.com/doc/tutorial/preprocessor/
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
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
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
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 1.Pointer is a kind of variable, it occupies memory in your computer 2.The pointer variables value is integer, this integer is actually a memory address
1.You 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);
3.We 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
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).
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).
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.
Thank You !