5
Variable types Local variable Local variables are declared within the body of a function, and can only be used within that function. Static variable Another class of local variable is the static type. It is specified by the keyword static in the variable declaration. The most striking difference from a non-static local variable is, a static variable is not destroyed on exit from the function. Global variable A global variable declaration looks normal, but is located outside any of the program's functions. So it is accessible to all functions.

15
The Answer j / i = 1; j % i = 5; k / i = 1.5; k % i It is illegal. Note: For %, the operands can only be integers.

16
Logical Operations What is “true” and “false” in C In C, there is no specific data type to represent “true” and “false”. C uses value “0” to represent “false”, and uses non-zero value to stand for “true”. Logical Operators A && B=>A and B A || B=> A or B A == B=>Is A equal to B? A != B=> Is A not equal to B?

17
A > B=>Is A greater than B? A >= B => Is A greater than or equal to B? A Is A less than B? A Is A less than or equal to B? Don’t be confused && and || have different meanings from & and |. & and | are bitwise operators.

29
Strings Strings are defined as arrays of characters. The only difference from a character array is, a symbol “\0” is used to indicate the end of a string. For example, suppose we have a character array, char name[8], and we store into it a string “Dave”. Note: the length of this string 4, but it occupies 5 bytes. Dave\0

30
Functions Functions are easy to use; they allow complicated programs to be broken into small blocks, each of which is easier to write, read, and maintain. This is called modulation. How does a function look like? returntype function_name(parameters…) { local variables declaration; function code; return result; }

32
Pointers What is a pointer? A pointer is a variable which contains the address in memory of another variable. In C we have a specific type for pointers.

33
Declaring a pointer variable int * pointer; char * name; How to obtain the address of a variable? int x = 0x2233; pointer = &x; where & is called address of operator. How to get the value of the variable indicated by the pointer? int y = *pointer;

34
x52000x5203 0x5200 pointer What happens in the memory? Suppose the address of variable x is 0x5200 in the above example, so the value of the variable pointer is 0x5200. X

35
swap the value of two variables

36
Why is the left one not working? swap main x, y a, b call swap(a, b) in main x, y, a, b are all local variables

42
The output The argc is 2 The 0th element in argv is./hello The 1th element in argv is 10 The trick is the system always passes the name of the executable file as the first argument to the main() function. How to use your argument? Be careful. Your arguments to main() are always in string format. Taking the above program for example, the argv[1] is string “10”, not a number. You must convert it into a number before you can use it.

43
Data Structure A data structure is a collection of one or more variables, possibly of different types. An example of student record struct stud_record{ char name[50]; int id; int age; int major; …… };

45
Memory Allocation Stack memory allocation Non-static local variable is an example of stack memory allocation. Such memory allocations are placed in a system memory area called the stack. Static memory allocation Static local variable and global variable require static memory allocation. Static memory allocation happens before the program starts, and persists through the entire life time of the program.

46
Dynamic memory allocation It allows the program determine how much memory it needs at run time, and allocate exactly the right amount of storage. The region of memory where dynamic allocation and deallocation of memory can take place is called the heap. Note: the program has the responsibility to free the dynamic memory it allocated.