Why C Matters — ทุกเครื่องมือสร้างจาก C
แนวคิดหลัก
Section titled “แนวคิดหลัก”คุณไม่ต้องเขียน C ทุกวัน — แต่การเข้าใจ C ทำให้คุณ เข้าใจทุกเครื่องมืออื่น ในระดับที่ลึกกว่าคนที่ไม่เคยเรียน
C เป็น X-ray ที่เปิดเผยว่าเครื่องมือแต่ละตัวซ่อนอะไรไว้
Sheets ซ่อนทุกอย่าง
Section titled “Sheets ซ่อนทุกอย่าง”A1 = 42สิ่งที่ Sheets ซ่อน:
- Type: มันตัดสินใจเองว่า 42 เป็นตัวเลข
- Size: ไม่รู้ว่าใช้กี่ bytes
- Memory: ไม่รู้ว่าเก็บที่ไหน
- Encoding: ไม่รู้ว่าเก็บเป็น integer หรือ floating point
สำหรับงาน data ธรรมดา — นี่คือข้อดี ไม่ต้องยุ่งกับรายละเอียด แต่เมื่อข้อมูลผิดพลาด คุณจะไม่รู้ว่าผิดที่ไหน
C แสดงทุกอย่าง
Section titled “C แสดงทุกอย่าง”#include <stdio.h>#include <string.h>
int main() { // === ทุก SQL type มี C type อยู่ข้างใน ===
// SQL INT → C int int age = 25; printf("INT: %d (%zu bytes)\n", age, sizeof(age));
// SQL BIGINT → C long long population = 70000000L; printf("BIGINT: %ld (%zu bytes)\n", population, sizeof(population));
// SQL FLOAT → C float float price = 29.99f; printf("FLOAT: %.2f (%zu bytes)\n", price, sizeof(price));
// SQL DOUBLE → C double double pi = 3.14159265358979; printf("DOUBLE: %.14f (%zu bytes)\n", pi, sizeof(pi));
// SQL CHAR(5) → C char[6] char code[6] = "TH"; printf("CHAR(5): \"%s\" (sizeof=%zu, strlen=%zu)\n", code, sizeof(code), strlen(code));
// SQL BOOLEAN → C int (0 or 1) int is_active = 1; printf("BOOLEAN: %d (%zu bytes)\n", is_active, sizeof(is_active));
// === สิ่งที่ C เปิดเผย === printf("\n=== X-ray View ===\n");
// 1. ตัวเลข 65 กับตัวอักษร 'A' คือ byte เดียวกัน char a = 65; printf("65 as char: '%c'\n", a); printf("'A' as int: %d\n", 'A');
// 2. String มี byte ซ่อนอยู่ printf("\"HI\" ใช้ %zu bytes (ไม่ใช่ 2)\n", sizeof("HI"));
// 3. ทุกตัวแปรมี address printf("address of age: %p\n", (void*)&age);
return 0;}Python ซ่อน bytes
Section titled “Python ซ่อน bytes”# Python ซ่อนทุกอย่างเรื่อง memoryx = 42 # ไม่ต้องระบุ typename = "Hello" # ไม่ต้องคิดเรื่อง \0
# แต่ถ้ารู้ C จะเข้าใจ Python ลึกขึ้น:
# 1. ทำไม Python int ไม่ overflow?# → เพราะ Python int ไม่ใช่ C int — มันเป็น arbitrary-precisionbig = 2**100print(big) # ทำได้! C int ทำไม่ได้
# 2. ทำไม string เป็น immutable?# → เพราะถ้า mutable จะมีปัญหา buffer overflow เหมือน Cs = "Hello"# s[0] = 'h' # Error! ทำไม่ได้
# 3. ทำไม list กับ tuple ต่างกัน?# → list = dynamic array (malloc/realloc ใน C)# → tuple = fixed array (ขนาดตายตัวเหมือน C array)SQL = C types + Business rules
Section titled “SQL = C types + Business rules”-- ทุก column ใน SQL มี C type อยู่ข้างในCREATE TABLE employees ( id INT PRIMARY KEY, -- C int + uniqueness rule name VARCHAR(100), -- C char* + length limit salary DOUBLE, -- C double + precision dept_code CHAR(3), -- C char[4] + fixed size is_active BOOLEAN, -- C int (0/1) + constraint created_at TIMESTAMP -- C long (unix epoch seconds));
-- SQL เพิ่มสิ่งที่ C ไม่มี:-- ✓ NOT NULL (C ไม่มี concept "ไม่มีค่า")-- ✓ UNIQUE (C ไม่มี uniqueness check)-- ✓ FOREIGN KEY (C มี pointer แต่ไม่มี referential integrity)-- ✓ CHECK constraints (C ไม่มี built-in validation)ระดับการซ่อนข้อมูล
Section titled “ระดับการซ่อนข้อมูล”Sheets ██████████████████████ ซ่อนมากที่สุดPython ████████████████ ซ่อนปานกลางSQL ██████████ ซ่อนบ้าง (type ต้องระบุ)C ██ ซ่อนน้อยที่สุด (เห็นเกือบทุกอย่าง)Assembly █ ไม่ซ่อนเลย (แต่ไม่ต้องเรียน)สรุป: C เป็น X-ray
Section titled “สรุป: C เป็น X-ray”| สิ่งที่ C สอน | ทำให้เข้าใจอะไร |
|---|---|
sizeof(int) = 4 | ทำไม INT ใน SQL เก็บค่าได้แค่ ±2 พันล้าน |
\0 null terminator | ทำไม VARCHAR ต้องมี — เพราะ string ไม่รู้ความยาวตัวเอง |
pointer *p | ทำไม Python ใช้ references, SQL ใช้ foreign keys |
char = 65 = 'A' | ทำไม ASCII() และ CHAR() ทำงานแบบนั้นใน SQL |
| buffer overflow | ทำไม Python ถึงเลือกทำ string เป็น immutable |