You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: week01/README.md
+58-3
Original file line number
Diff line number
Diff line change
@@ -182,7 +182,7 @@ Compilation is briefly introduced in the previous part. There are several steps
182
182
183
183
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.
184
184
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`.
186
186
187
187
```C++
188
188
#define PI 3.14
@@ -192,7 +192,7 @@ double len(double r)
192
192
}
193
193
```
194
194
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.
196
196
197
197
```C++
198
198
doublelen(double r)
@@ -201,10 +201,65 @@ double len(double r)
201
201
}
202
202
```
203
203
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
+
doublelen(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
+
204
239
## Command Line Arguments
205
240
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++`.
206
242
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.
207
244
208
-
## Simple Input and Output
245
+
```C++
246
+
//argument.cpp
247
+
#include<iostream>
209
248
249
+
usingnamespacestd;
250
+
intmain(int argc, char * argv[])
251
+
{
252
+
for (int i = 0; i < argc; i++)
253
+
cout << i << ": " << argv[i] << endl;
254
+
}
255
+
```
210
256
257
+
When the program from the above code is executed with some arguments, it will print the arguments one by one with their indices.
0 commit comments