1. 程式人生 > 其它 >《C和指標》學習筆記[第二章 基本概念]

《C和指標》學習筆記[第二章 基本概念]

2.7問題

1.無法通過編譯,適配最後第一個*/,後面那個無法適配

2.優點的話,管理比較方便,各個函式衝突少。缺點,任何修改需要重新編譯整個原始檔,費時間。

3.在每個?前面新增\轉移字元

4. \40=空格 \100=@

\x100報錯,hex escape sequence out of range

\0123 分別為\012 與3

\x0123 報錯 \x後面的數字,mac的gcc編譯器會全部適配,不是隻有3個字元

5. 報錯,因為註釋為一個空格,兩個變數初始化中間少了逗號

6.沒啥錯誤,雖然更關鍵字看過去有點像,但並不一致,關鍵字都是小寫的,stop不是關鍵字

7.錯,還是排班好比較好,因為是給人看的

8.兩個對少了while的右括號[看答案的],第二個好,因為第二個有縮排

9.編譯gcc -c 連結 gcc

10最後新增 -lparse

11 list.c改了 gcc -c list.c 

list.h改了 gcc -c list.c table.c main.c

table.h改了 gcc -c table.c main.c

2.8程式設計練習

main.c

#include <stdio.h>
#include <stdlib.h>

#include "increment.h"
#include "negate.h"

#define NUM 3
int
main(void) { int num_array[NUM] = {10, 0, -10}; for (int i=0; i < NUM; i++) { printf("increment num = %d, negate num = %d\n", increment(num_array[i]), negate(num_array[i])); } return EXIT_SUCCESS; }

increment.h

#ifndef increment_h
#define increment_h

#include <stdio.h>

int
increment(int num); #endif /* increment_h */

increment.c

#include "increment.h"

int
increment(int num)
{
    return num + 1;
}

negate.h

#ifndef negate_h
#define negate_h

#include <stdio.h>

int negate(int num);
#endif /* negate_h */

negate.c

#include "negate.h"

int
negate(int num)
{
    return -num;
}

2.[抄了答案]邏輯簡單

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "t_c.h"

int main(void)
{
    
    int ch;
    int braces = 0;
    
    while ((ch = getchar() ) != EOF) {
        if (ch == '{') {
            braces++;
        }
        else if ( ch == '}' ){
            if ( braces == 0 ) {
                printf("Extra closing brace!\n");
            }
            else{
                braces--;
            }
        }
    }
    
    if (braces != 0 ) {
        printf("%d unmatched opening brace(s)!\n", braces);
    }
    
    return EXIT_SUCCESS;
}