Skip to content

Latest commit

 

History

History
252 lines (180 loc) · 17.4 KB

c.md

File metadata and controls

252 lines (180 loc) · 17.4 KB

C

  • C (programming language) - Wikipedia #ril

  • Learn to code using the C programming language on your Mac - Macworld UK (2016-05-26)

    • The C programming language has been around since the 1970s, but it has NEVER GONE OUT OF STYLE, and learning C is one of the best computer skills you can acquire. Mac OS X comes with C built into it, and Apple has used C while making every aspect of OS X and iOS.

    • Because C is such a popular language, it even forms the basis of many other programming languages, including two big names: C++ and Objective-C. And even though Apple is migrating from Objective-C to Swift, guess what languages Apple used to build Swift? Throughout the Swift code, you'll find C and C++ files.

      雖然 Apple 正把 Objective-C 往 Swift 遷移,但 Swift 本身就是 C 跟 C++ 寫的。

    • You're almost SURE NEVER TO USE C PROFESSIONALLY, but learning it is a rite of passage for programmers. The C programming language has influenced many other languages. Learning C enables you to UNDERSTAND WHAT'S GOING INSIDE A COMPUTER IN GENERAL.

    • C is also a curious creature, in that it's a HIGH-LEVEL language (these are the easy ones to understand), but with LOW-LEVEL elements (so-called because they are "close to the metal").

      The C syntax is similar to many modern programming languages. However, you have to learn how to allocate memory, free up memory to prevent leaks and use memory addresses and pointers (memory blocks that point to other memory blocks). All of this memory management will most likely drive you to tears at some point, but it's superb for understanding what a programming language is doing.

      This complexity is why C is used in courses such as Harvard's CS50: Introduction to Computer Science. More modern languages, like Python, take care of memory for you; which is better for day-to-day use, but not as good educationally.

      C 屬於高階語言 (high-level language),但有許多低階的操作,尤其是要自己做記憶體管理,許多語言像 Python 已經幫你把記憶體的管理處理掉,使得 C 更適合拿來做為教材,讓你瞭解電腦內部的運作;Harvard's CS50 就是用 C 做為教材。

  • C Programming/Why learn C? - Wikibooks, open books for an open world #ril

    • C 最常被拿來寫 OS,最早是 Unix,然後是 Windows, Mac OS X, GNU/Linux。另外,許多 high-level languages 本身也都是用 C 寫的 (Perl, PHP, Python...)
    • Assembly language 強調的是 speed 跟 maximum control (但每個 device/architecture 之間的 assembly language 並不相容),而 C 則在上面疊加了 portability 但又能維持住 performance。
    • 有 "what you see is all you get" 一說,因為 C statement 直接跟少量的 assembly statements 對應。
    • OS 用 C 寫,很自然地 high-level libraries (OpenGL, GTK 等) 大部份也會用 C 寫,之後特別要求效能的應用 (game, media players 等) 也會用 C 來寫 ... (pattern)
  • C Programming/History - Wikibooks, open books for an open world #ril

    • UNIX 的 portability 讓 UNIX 跟 C 變得流行 (因為 UNIX 是用 C 寫的)
    • 只要為系統特有的部份寫一段程式,再搭配該系統的 C compiler,就可以讓程式執行在不同的系統上。
  • Chapter 1: Getting Started - Let Us C, 5th Edition (2004.) #ril

    • 每個語言都有 4 個面向 -- 如何儲存資料、如何操作資料、如何處理 input/output、如何控制程式的執行順序。
    • C 在 1972 由 Dennis Ritchie 在 AT&T Bell 實驗室發展出來,在 70 年代後期 C 開始取代其他常見的語言 (例如 PL/I、ALGOL 等),沒有任何推廣,所以 Ritchie 自己也很意外;可能是它 reliable、simple 及 easy to use 的特性。
  • [1. 千里之行始於足下 - 歐萊禮 - 深入淺出 C](博客來-深入淺出 C) (2012-11) p2. 被應用在重視速度、空間和可攜性 (portability) 的地方,多數的作業系統跟程式語言都用 C 寫,大多數的遊戲更是。

  • 旗標 - C 語言教學手冊 (第四版) (2007-04) p1-4 C 語言具有低階語言的優點 (對硬體的控制能力佳),同時兼顧了高階語言的特色 (易於撰寫、除錯),有人稱之為 "中階語言",此外 C 可以很容易與組合語言連結。

為什麼要學 ??

  • 由於 OS 底層是 C 的關係,其他語言也是在外層包裝,學習 C 有助於瞭解底層的高階包裝。
  • Chapter 1: Getting Started - Let Us C, 5th Edition (2004.)
    • 常聽到 "C 已經被 C++、C# 和 Java 接替,為什麼還要學 C",除了先用 C 打好基礎、OS/kernel/driver 用 C 寫的事實、適合資源受限的 microprocessor,作者大部份的理由都是 "C 的效能無人能及",但相對 C++ 也是嗎?

跟 C++ 的關係 ??

Hello, World!

$ vi hello.c
$ make hello
cc     hello.c   -o hello

$ ./hello
Hi, my name is Judy, and I'm 15 years old.

hello.c:

#include <stdio.h>

int main() {
    int age = 15;
    char name[] = "Judy";
    printf("Hi, my name is %s, and I'm %d years old.\n", name, age);

    return 0;
}

參考資料:

  • How to set up C in OS X - Learn to code using the C programming language on your Mac - Macworld UK (2016-05-26)

    • Like Python, it's easy to set up C in OS X. Mostly because it's already packaged in the system and you don't need to install anything.

    • There are multiple standards for C, and the two you'll come across most are C99 and C11. As a newcomer, you don't need to worry too much about these, and you'll almost certainly learn C99 then discover the new features in C11.

      只要知道 C11 (2011) 比 C99 (1999) 新就好。

    • C differs from other programming languages, like Python, in that you need to compile programs before you can run them. You'll typically do this in C using the command "make". C programs end with the ".c" extension, and you'll run make to build a second file, that is the compiled program. The compiled file is what you run.

      #include <stdio.h>
      #import
      
      int main() {
          printf("Hello, World!\n");
          return 0;
      }
      

      執行 make hello 即可完成編譯。

      $ make hello
      cc     hello.c   -o hello
      $ ./hello
      Hello, World!
      $ ls -l hello
      -rwxr-xr-x  1 jeremykao  wheel  8432 Apr 27 08:41 hello
      

      小小的 hello.c 編出來有 8432 bytes,是因為 stdio.h 整個包進去了嗎??

新手上路 {: #getting-started }

  • 大朋友可以從 C Programming Language, 2nd Edition 下手。
  • Head First C 好像很適合小朋友? 寫作風格很活潑;在那之前還可以先看過 Head First Programming。

參考資料:

  • long long 是什麼?

  • 預設是 signed?? 跟 short/long 一起使用時的順序??

    • c - Is the integer constant's default type signed or unsigned? - Stack Overflow http://stackoverflow.com/questions/11310456/ Integer constant 沒有 suffix 時要視為 signed int 或 unsigned int? ouah 提到 decimal/octal/hexadecimal 的規則不同,但概念上都是 "first type the value can fit in";就 decimal constant 而言,依序是 int, long, long long,而 hexadecimal 依序是 int, unsigned int, long, unsigned long, long long, unsigned long long (主要都是 unsigned) ... 不過這還是沒有回答問題。
    • language agnostic - Default int type: Signed or Unsigned? - Stack Overflow http://stackoverflow.com/questions/1555154/ #ril
  • C Programming Boot Camp http://gribblelab.org/cbootcamp/ #ril

  • Learn c in Y Minutes https://learnxinyminutes.com/docs/c/ 跳太快? 看了一些就開始卡了... #ril

  • C for Python Programmers http://www.toves.org/books/cpy/ #ril

  • C and C++ for Java Programmers - Cprogramming.com http://www.cprogramming.com/java/c-and-c++-for-java-programmers.html #ril

  • Which book is best for learning C programming by a beginner? - Quora 最常出現的是 The C Programming Language、Let Us C、Head First C 及 Programming in ANSI C。

    • Jack Sparrow: The C Programming Language、C Programming: A Modern Approach、C Programming in 12 Easy Lessons、C for Dummies、C for Dummies Vol. II
    • Geet Gobind Singh: Let Us C、Pointers in C、Test Your C Skills、The C Programming Language (前 3 本都是 Yashwant Kanetkar 的書)
    • Mohammed Raiyyan: Let Us C、
    • Subhomoy Haldar: ANSI C、Let Us C、Programming in C、The C reference Guide、Head First C、C: How to Program
    • Vinod Chuahan: Let Us C、Head First C
    • Kamal Rana: Let Us C、The C Programming Language
    • Ankit Bhasker: The C Programming Language、C: The Complete Reference、Programming in ANSI C、Let Us C、Head First C
    • Bhairab Changkakoty: Head First C
    • Shubham Yadav: Let Us C、The C Programming Language
    • Kumar Subham: Programming in ANSI C
    • Shrinath Bhosale: Head First C
    • SANUJ BHADRA: Let Us C、Programming in ANSI C

ANSI, C99, C11 ??

  • C 有許多標準,差別是什麼??
  • 跨平台 (portable) 是透過在不同平台上重新 compile。
  • [1. 千里之行始於足下 - 歐萊禮 - 深入淺出 C](博客來-深入淺出 C) (2012-11) p2. 有 3 種語言標準 (C standard),ANSI C 來自 80 年代後期,1999 的 C99 有許多修正,2011 年的 C11 添加了許多酷炫的功能;版本間的差異其實不太。

參考資料:

Debugging ??

C Standard Library

慣例

安裝設置 {: #setup }

macOS

參考資料

社群:

更多:

書籍:

相關:

  • macOS 內建 Clang 支援 C 相關的開發

手冊: