# MVMC 基礎程式練習
1. 請解釋 `i++` 與 `++i` 的不同。 (實驗一相關)
```c=
#include <stdio.h>
int main() {
int A = 0;
int B = 0;
int C = A++;
// int C = A;
// A = A + 1;
int D = ++B;
// B = B + 1;
// D = B;
printf("%d %d %d %d\n", A, B, C, D);
return 0;
}
```
2. [reverse-integer](https://leetcode.com/problems/reverse-integer/) (實驗二相關)
3. 試宣告以下
- 一個整型數型態(An integer)
``` =c
int q;
```
- 一個指向整數型態的指標(A pointer to an integer)
```=c
int *q;
```
- 一個指向指標的指標,它指向的指標是指向一個整型數(A pointer to a pointer to an integer)
```=c
int **q;
```
- 一個有10個整數的陣列(An array of 10 integers)
```=c
int q[10];
```
- 一個有10個指標的陣列,該指標指向一個整數(An array of 10 pointers to integers)
```=c
int *q[10];
```
- 一個指向有10個整數型態陣列的指標(A pointer to an array of 10 integers)
```=c
int (*q)[10];
```
- 一個指向函數的指標,該函數有一個整數型態參數並返回一個整數(A pointer to a function that takes an integer as an argument and returns an integer)
```=c
int (*q)(int );
int squre (int a){
return a * a;
}
q = &squre;
squre(10);
q(10);
```
- 一個有10個指標的陣列,該指標指向一個函數,該函數有一個整數型態參數並返回一個整型(An array of ten pointers to functions that take an integer argument and return an integer)
```=c
int (*q[10])(int);
```
:::info
[第11題](https://hackmd.io/@Rance/SkSJL_5gX?type=view)
:::
4. [reverse-string](https://leetcode.com/problems/reverse-string/)
5. bitwise swap
可用運算符號有: __&__ (and), __|__ (or), __^__ (xor), __>>__ (right shift), __<<__ (left shift), __~__ (arithmetic not), __!__ (logical not)
```=c
void swap (int *x, int *y){
*x ^= *y;
*y ^= *x;
*x ^= *y;
x ^= y
x = x ^ y
//x += y
//x = x + y
// x ^= y
// x = x ^ y
// y ^= x
// y = y ^ x = y ^ (x ^ y) = x
// x ^= y
// x = (x ^ y) ^ x = y
}
```
6. macro trap
判斷以下程式碼是否能正常執行
若不能 解釋原因
```c=
#define max(x,y) x > y ? x : y
max(a, max(b, c));
max(a, a+1)
//max(a, b > c ? b : c)
// ->
// a > b > c ? b : c ? a : b > c ? b : c
//
// imporve
// #define max(x,y) (x) > (y) ? (x) : (y)
// (a) > ((b) > (c) ? (b) : (c) ) ? (a) : ((b) > (c) ? (b) : (c) )
```
7. 比較下列寫法與第6題之差異
```c=
int max(int x, int y){
if (x > y){
return x;
}else{
return y;
}
}
max(a, max(b, c));
```
-------------------------------------------
## 工具
- [Linux上安裝cdecl](https://hackmd.io/@sysprog/c-pointer#英文很重要)
- [cdecl online](https://cdecl.org/?q=char+%28*%28*%28*+const+x%5B3%5D%29%28%29%29%5B5%5D%29%28int%29)