最簡單的 c 程式 2022
Author: WhoAmI
Date: 20220726
Copyright: CC BY-NC-SA
若您有寶貴的意見,改進或建議歡迎 email 給本人, 大專以上教師若要教學使用可自行免費使用修改當教材給學生使用
WhoAmI e-mail: kccddb@gmail.com
如果您能力夠請 贊助 (可用信用卡):
If possible, please donate Taiwan Fund for Children and Families (TFCF) :
財團法人台灣兒童暨家庭扶助基金會
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
bugs 除不盡春風吹又生
千里之行始於足下
資料型態最好依照 man page, 如此日後才方便移植至其他系統!小故事: 以前老總看到一位於上市公司研發部 "五年" 經驗的求職者, 本來要請他來面談, 結果 我收到 他的email 來的作品(程式), 看過就婉謝了. Why?
還有 10 多年經驗的研發, c, c++ 綜合體~變形金剛???
另一正面的故事, 我朋友公司來了一位外文系的有心學, 我朋友就教他, 最後 他成為團隊裡最強的研發人員.
養成良好的程式習慣
本人擔任外面公司顧問多年, 深深覺得就算國立大學研究所資工相關科系畢業, 程式撰寫習慣欠佳(1. 有點像寫作業! 2. 不太會用 Makefile 3. 作業系統觀念淪為考試用 4. warning 當正常 )
業界大都要靠團隊合作, 習慣不好如何與別人配合, 日後新增修改如何處理? 有問題的程式碼沒註明
Garbage Collection(GC) 養成設計師的壞習慣 (尤其會 java 的), 以前一位 研究生 用java, 我說不能如此寫, 請他試試 長期執行是否會 掛! 後來他說 真的掛!!!
Critical Section lock un-lock 效能問題
Variable has no initial value 有些 剛開機 可以, 但過陣子又不行
memory leaks (這是嚴重的大問題, 時常與程式習慣與疏忽有關)
data type 錯誤( 32/64 bits 移植至 16bits)
volitile 不知其重要性 (尤其 單晶片, kernel driver, …)
interrupt 處理方式不正確, 造成 有時 沒動作
Thread safety, e.g., gethostbyname(), strtok(),… (See man page)
printf, cout 過分濫用, 不知其影響效能, c/c++ 混用
用 大 array 代替 malloc…真誇張, 不知他如何畢業的
練習看 man page
基本c 語言的資料可以參閱很不錯的文件(包含 簡單的 makefile): Tim Love, ANSI C for Programmers on UNIX Systems
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
OnlineGDB 可以練習 c, bash,…
善用:
#define
#ifdef
#ifndef
#define debug(x) x
Variadic Macros
gcc -Wall …
行有餘力 學 gdb
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
寫好網路程式除了網路基礎外, Object-oriented programming (OOP) 與 OS 的知識是必須的!
C 也可以寫的很OOP 簡單範例與簡易Makefile
basic.c:
為什麼要 exit ?
shell script 幫助檢查執行結果 正確與否
注意 檔案格式 要用 unix 格式
Makefile 也要用 unix 格式
dos2unix 可幫您轉換!!!
常用的工具程式
strace- trace system calls and signals
ldd - print shared object dependencies
top - display Linux processes
strip - discard symbols and other data from object files
pstree - display a tree of processes
基本常用的工具請自行參閱網路上的文件例如 (ls, cp, rm, gcc, vim, cd, tar, gunzip, gzip, bzip2, grep, more, find…)
這是一個 相當簡單的 c 程式, 但是有很多值得理解的問題
Input Queue

Output Queue 與多工輸入時共有的現象.



除了 output congestion 問題以外, 還有 不同 process 到 kernel 的同步 (synchronous) 問題! 這是一般讀者常忽略的問題!
例如 Google GO channels type 與 memory management
程式的設計考慮多CPU 的平行處理(例如 Google GO: Efficient parallel computation) *convolution 多CPU 例子(學過通訊的應該看得懂)
gcc 新的版本 可以用 multiline string (寫 CGI 程式很好用)
運用 gcc Variadic Macros, 兩者有點小不同, 您執行看看就知道了.
do {…}while(0)
SSH, telnet,…
MobaXterm
文字編輯:
Komodo Edit
沒有 Windows 就靠
vim
HW: ./basic happy day !!!
程式如何 輸出 happy day ?
HW:
c language: function pointer, callback
printf 設計方法:
stdarg, va_start, va_arg, va_end, va_copy - variable argument lists
過去教 大三網路程式設計最後一次作業(team work)–-可以進行檔案續傳的 client/server, 讓同學 體會 穩定不容易
- 可以網路連結斷線後續傳完成(要 wifi 可用, 測拔網路線, 蓋金屬蓋, 斷電, 不同網路頻寬,…)
- 1Gbytes 以上
- 具有 timeout 的機制, 特別是 send timeout
- 不同 CPU byte order 的問題, 例如 little-endian <–>big-endian
- TCP send N 次 recv 是N 次嗎? 一般來說不正確! 可能 M 次, N<M, N>M, N=M 都有可能

how to use void ** pointer correctly?
Abstract List
