主要分為四大類:
main()
,調用函數時執行 push()
,函數 return
時執行 pop()
。malloc
, calloc
動態分配記憶體空間時,這些記憶體位置都屬於 Heap 內存。
如果使用
malloc()
時返回了null
,代表這一塊空間快要用盡了。
static
關鍵字聲明的變數會被放到該空間。
補充: static 的用途
使用標頭檔
定義方法:
/* File add.h */
#ifdef ADD_H
#define ADD_H
int add(int, int);
#endif
同名.c
實做方法:
/* File add.c */
#include "add.h"
int add(int a, int b)
{
return a + b;
}
引用標頭檔直接使用函式:
/* File triple.c */
#include "add.h";
int triple(int x){
return add(x, add(x, x));
}
typedef struct person_t person_t;
struct person_t {
char *name;
unsigned age;
};
int main(void){
person_t p ={ "Ian", 37 };
}
上面範例中的初始化結構方式寫死該結構的屬性的位置,若屬性有更動需一併更動相關程式碼,在軟工觀點上不佳。可以改用以下的方法來初始化結構:
#include <assert.h>
struct person_t p = {
.name = "Michael",
.age = 37
};
assert(pt.x == 3);// 存取結構內屬性
return 0;
typedef struct person_t person_t;
第一個 person_t
為結構名稱,第二個 person_t
為自定義型別名稱:
person_t p ={ "Ian", 37 };
考前必讀 1. I/O 處理 2. STL Container 3. STL Algorithm sort lower_bound upper_bound 4. String 處理
Mar 21, 2023主要參考文獻 Microsoft Docs AIdrifter CS 浮生筆錄 Google C++ Style Guide 繁體中文版 chenlen.com cplusplus.com 山姆大叔談 C++:從歷史談起,再給個定義—Modern C++ 解惑系列文 比較安全的 C++ 虛擬函式寫法:C++11 override 與 final 使用 VSCode 開發 C++
Mar 20, 2023查閱 Linux manual page 時會發現: 在 stdio.h 中有密密麻麻的函式/變數定義。有趣的是,筆者發現許多 C 語言的入門教材都沒有將標準輸入輸出交代仔細。讓大多數的人讀完文章還是對 stdio.h 內函式的設計感到一頭霧水,因此,筆者希望透過撰寫文章為大家解惑,也順便複習一下生疏的 C 語言技能。 先備知識 在學習 stdio library 之前,需要先了解 Unix 作業系統的檔案觀念,在掌握檔案觀念後,可以幫助我們更快的理解 stdio 的設計巧思。 File descriptor stdio.h 中的函式幾乎都跟 File 的操作脫不了關係。 在 Unix 或是 Unix-like 中,作業系統抽象了一層資料結構用來存取 File, input / output 資源,該資料結構稱為 File descriptor 。並且, File descriptor 是屬於 POSIX API 的一部份,在符合 POSIX 的作業系統上,每一個 Process (除了守護程序)都應該具備至少三個 File descriptor ,分別是:
Sep 21, 2021or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up