Skip to content

Commit 36f69b0

Browse files
committed
chapter 01 finished (almost)
1 parent fae2fe6 commit 36f69b0

File tree

4 files changed

+69
-4
lines changed

4 files changed

+69
-4
lines changed

week01/Lecture01.pptx

11 Bytes
Binary file not shown.

week01/README.md

+58-3
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ Compilation is briefly introduced in the previous part. There are several steps
182182
183183
Preprocessing directives begin with a `#` character, and each directive occupies one line. The most commonly used preprocessing directives are `include`, `define`, `undef`, `if`, `ifdef`, `ifndef`, `else`, `elif`, `endif`, `line`, `error` and `pragma`. `include` is just introduced, and it appears in almost all source files.
184184
185-
`define` is another popular directive, and can be used to define some constants. In the following source code we define `PI` as `3.14` using `#define PI 3.14`.
185+
`define` is another popular directive, and can be used to define some macros. In the following source code we define `PI` as `3.14` by using `#define PI 3.14`.
186186
187187
```C++
188188
#define PI 3.14
@@ -192,7 +192,7 @@ double len(double r)
192192
}
193193
```
194194

195-
After preprocessing, the source code will be as the following. `PI` has been replaced with `3.14`. Then it will be sent to a compile.
195+
After preprocessing, the source code will be as the following. `PI` has been replaced with `3.14`. Then it will be sent to a compiler. Macro `PI` is not a variable. The preprocessing for macros is like text replacement.
196196

197197
```C++
198198
double len(double r)
@@ -201,10 +201,65 @@ double len(double r)
201201
}
202202
```
203203
204+
Since macros behaviors like text replacement, sometimes it is dangerous and cause bugs. If we define `PI` as `2.14+1.0`, the statement is grammatically correct.
205+
206+
```C++
207+
#define PI 2.14+1.0
208+
double len(double r)
209+
{
210+
return 2.0 * PI * r;
211+
}
212+
```
213+
214+
But after preprocessing the return value of the function will be `4.28+r`, nor `2.0*3.14*r`. It may be not what you expected. Anyway the source code will be compiled successfully, and the compiler will not report any warning and error. You should be very careful when you use macros.
215+
216+
```C++
217+
double len(double r)
218+
{
219+
return 2.0 * 2.14+1.0 * r; //= 4.28 + r
220+
}
221+
```
222+
223+
We can even define a macro which works like a function. Sometimes macros can achieve better efficiency than functions since macros have no overhead of function callings.
224+
225+
```C+++
226+
#define MAX(a,b) (((a)>(b))?(a):(b))
227+
228+
int main()
229+
{
230+
//...
231+
float a = 2.0f;
232+
float b = 3.0f;
233+
float m;
234+
m = MAX(a, b);
235+
//...
236+
}
237+
```
238+
204239
## Command Line Arguments
205240

241+
We can pass some values to our programs through the command line. The command we use previously `g++ hello.cpp -o hello` send three command line arguments `hello.cpp`, `-o` and `hello` to the compiler `g++`.
206242

243+
The command line arguments can be handled using `main()` arguments. The function argument `argc` refers to the number of the command arguments, and `argv[]` is a pointer array. Each element in `argv[]` points to a `char` array string.
207244

208-
## Simple Input and Output
245+
```C++
246+
//argument.cpp
247+
#include <iostream>
209248

249+
using namespace std;
250+
int main(int argc, char * argv[])
251+
{
252+
for (int i = 0; i < argc; i++)
253+
cout << i << ": " << argv[i] << endl;
254+
}
255+
```
210256
257+
When the program from the above code is executed with some arguments, it will print the arguments one by one with their indices.
258+
259+
```bash
260+
$ ./argument hello.cpp -o hello
261+
0: ./argument
262+
1: hello.cpp
263+
2: -o
264+
3: hello
265+
```

week01/examples/argument.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include <iostream>
22

33
using namespace std;
4-
int main(int argc, char ** argv)
4+
int main(int argc, char * argv[])
55
{
66
for (int i = 0; i < argc; i++)
77
cout << i << ": " << argv[i] << endl;

week01/examples/macro.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#include <iostream>
2+
#define PI 2.14+1.0
3+
4+
using namespace std;
5+
6+
int main()
7+
{
8+
double len = 2 * PI * 3;
9+
cout << "len = " << len << endl; //it will output 7.28, nor 18.84
10+
}

0 commit comments

Comments
 (0)