summaryrefslogtreecommitdiff
path: root/03-04.md
blob: dfd1de5b7d2049aece0302d12bb33fc3213f3c90 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
[-> Notes 02/26](02-26.md)

---

# Large Programs

- When programs are long
	- Need to split the file into several files
	- Need to compile them together
	- Example:
		- We have files: main.c, list.c, file.c
		- To compile: `gcc -o proj main.c list.c file.c`

- Sharing global variables
	- Define types in a `.h` file
	- Declare global variables (do not initialize!)
		- extern in a .h file
	- Declare and initialize global variables
		- in one of the .c file (e.g., the main one)
	- Include the .h file in all the .c files which use any of the global variables: `#include "proj.h"`

- Example:
	- in `link.h`, declare (do not initialize!) global variables
		- `extern NODE *head;`
	- In main.c, include link.h, and declare and initialize global variables

```
#include "link.h"
NODE *head = (NODE *)NULL;
```

- In all the other .c files, just include link.h
	- `#include "link.h"`

- Use `makefile` to automate the compilation process
- Create a "makefile" file with instructions
- Command `make` will parse the makefile file and execute the appropraite command
	- `<prompt> make proj1`

- Example 1:

```
proj1: main1.c list1.c io1.c
	gcc -o proj1 main1.c list1.c io1.c
	./proj1 filename
```

- Example 2:

```
proj2: main2.c list2.c thr2.c
	gcc -o proj2 main2.c list2.c thr2.c -lpthread
	./proj2 filename1 filename2
```

- The tab is important! (its the syntax)

## Development Tools

- Unix/Linux
	- Environment and commands
	- Tools - learn to use these!!
		- vi
		- grep
		- gdb
		- makefile
		- shell script

- Libraries
	- Math
	- String
	- Memory
	- Threads

---

# Special Operators

## Condtional Operator

- The conditional operator `?:` takes three operands
	- c ? r1 : r2
	- The value of the expression using the conditional operator is the value of either its second or third operand, depending on the value of the first operand
	- Shorthand for:

```
if(c){
	//result value is r1
}
else{
	//result value is r2
}
```

- Examples
	- In assignment: `x = (a < b) ? a : b;`
		- x will be assigned the smallest value between a and b

- Very useful for macros
- Examples:
	- `#define MAX(a,b) (((a) > (b)) ? (a) : (b))`
		- returns the max between the paramaters assigned to a and b
	- `define ISLETTER(c) (((c) >= 'A' && (c) <= 'z') ? 1 : 0)`
		- returns 1 if the value of assigned to c is a letter and returns 0 if not

## Sequential Evaluation

- The comma operator
	- Evaluates its two operands in sequence, yeilding the value of the second operand as the value of the expression
	- The value of the first is discarded

- Example:
	- In assignments: `x = (i += 2, a[i]);` -> `i += 2; x = a[i];`
		- Parenthesis are important because precedence 

## Bitwise Operators

- Positive integers are represented in the computer by standard binary numbers
	- Examples:
		
```
short n = 13;
//in memory - 0000 0000 0000 1101
//2^0 + 2^2 + 2^3 = 13

char c = 5;
//in memory - 0000 0101
//2^0 + 2^2 = 5
```

- Bitwise operators
	- take operands of any integer type
		- char, short, int, long
	- but treat an operand as a collection of bits rather than a single number

### Bitwise Negation

- Bitwise negation
	- Operand: `~`
		- Application of `~` to an integer produces a value in which each bit of the operand has been replaced by its negation
			- 0 -> 1
			- 1 -> 0
		- Example:
			- ` n = 0000 0000 0000 1101`
			- `~n = 1111 1111 1111 0010`

### Bitwise Shift

- Shift operators
	- shift left: `<<`
	- shift right: `>>`
	- Take two integers operands
		- The value on the left is the number to be shifted
			- Viewed as a collection of bits that can move
			- To avoid implementation problems, avoid negative numbers when shifting right
		- The value on the right is a nonnegative number telling how far to move the bits

- Operand
	- `<<` shifts bits left
	- `>>` shifts bits right
- The bits that "fall off the end" are lost
- The "emptied" positions are filled with zeros

- Examples:
	
```
int n; //    n is 0000 0000 0000 1101
n << 1 //now n is 0000 0000 0001 1010
n << 4 //now n is 0000 0000 1101 0000
n >> 3 //now n is 0000 0000 0000 0001
n << 4 //now n is 0000 0000 1101 0000
```