Lab: Print All Sizes
คุณจะเขียนโปรแกรม C ตัวเดียวที่รวม sizeof ของทุก type ไว้ด้วยกัน พร้อม map กับ SQL type — เป็น “reference card” ที่คุณสร้างเอง
เวลาที่ใช้: ~30 นาที
เครื่องมือ: onlinegdb.com (เลือก Language: C)
คำสั่ง
Section titled “คำสั่ง”-
สร้างโปรแกรม C ที่แสดง sizeof ของทุก type
เขียนโปรแกรมที่ print ตารางแบบนี้:
C Type Bytes SQL Equivalent──────────────── ───── ──────────────────────char 1 CHAR(1)short ? SMALLINTint ? INTEGERlong ? BIGINTfloat ? REALdouble ? DOUBLE PRECISIONchar[5] ? CHAR(5)char[100] ? CHAR(100)ใช้
sizeof()เพื่อหาค่า?ทั้งหมด -
เพิ่มส่วน string comparison
เพิ่ม code ที่แสดง:
sizeof("HELLO")vsstrlen("HELLO")- อธิบายว่าทำไมต่างกัน (ด้วย printf)
-
เพิ่มส่วน type interpretation
เพิ่ม code ที่เก็บเลข
66ลงในcharแล้วแสดงเป็น:- ตัวเลข (%d)
- ตัวอักษร (%c)
- เลขฐาน 16 (%x)
-
ตรวจสอบผลลัพธ์
ถามตัวเอง:
- ทำไม
char[5]ถึง = 5 bytes? (ไม่ใช่ 4?) - ทำไม
sizeof("HELLO")ถึง = 6? (ไม่ใช่ 5?) floatและintมี bytes เท่ากัน (4) — แต่เก็บข้อมูลต่างกันอย่างไร?
- ทำไม
-
Bonus: เปรียบเทียบกับ Python
เปิด Python แล้วรัน:
import sysfor val in [True, 42, 3.14, 'A', 'HELLO']:print(f"{str(val):>8} → {sys.getsizeof(val)} bytes")เปรียบเทียบกับผลจาก C — ทำไม Python ใช้ memory มากกว่า?
ดูเฉลย
#include <stdio.h>#include <string.h>
int main() { // ====== Part 1: sizeof ทุก type ====== printf("=== C Type Size Reference ===\n\n"); printf("%-16s %5s %-22s\n", "C Type", "Bytes", "SQL Equivalent"); printf("%-16s %5s %-22s\n", "----------------", "-----", "----------------------");
printf("%-16s %5zu %-22s\n", "char", sizeof(char), "CHAR(1)"); printf("%-16s %5zu %-22s\n", "short", sizeof(short), "SMALLINT"); printf("%-16s %5zu %-22s\n", "int", sizeof(int), "INTEGER"); printf("%-16s %5zu %-22s\n", "long", sizeof(long), "BIGINT"); printf("%-16s %5zu %-22s\n", "float", sizeof(float), "REAL"); printf("%-16s %5zu %-22s\n", "double", sizeof(double), "DOUBLE PRECISION");
char five[5]; char hundred[100]; printf("%-16s %5zu %-22s\n", "char[5]", sizeof(five), "CHAR(5)"); printf("%-16s %5zu %-22s\n", "char[100]", sizeof(hundred), "CHAR(100)");
// ====== Part 2: sizeof vs strlen ====== printf("\n=== sizeof vs strlen ===\n\n"); char hello[] = "HELLO"; printf("sizeof(\"HELLO\") = %zu (includes \\0 null terminator)\n", sizeof(hello)); printf("strlen(\"HELLO\") = %zu (counts only visible characters)\n", strlen(hello)); printf("Difference: %zu byte for the null terminator \\0\n", sizeof(hello) - strlen(hello));
// ====== Part 3: Same value, different representations ====== printf("\n=== One Value, Three Views ===\n\n"); char ch = 66; printf("Stored value: 66\n"); printf("As integer: %d\n", ch); // 66 printf("As character: %c\n", ch); // B printf("As hex: %x\n", ch); // 42
printf("\nAll three lines read the SAME bits in memory.\n"); printf("Only the DISPLAY FORMAT changes.\n");
return 0;}ผลลัพธ์ที่คาดหวัง:
=== C Type Size Reference ===
C Type Bytes SQL Equivalent---------------- ----- ----------------------char 1 CHAR(1)short 2 SMALLINTint 4 INTEGERlong 8 BIGINTfloat 4 REALdouble 8 DOUBLE PRECISIONchar[5] 5 CHAR(5)char[100] 100 CHAR(100)
=== sizeof vs strlen ===
sizeof("HELLO") = 6 (includes \0 null terminator)strlen("HELLO") = 5 (counts only visible characters)Difference: 1 byte for the null terminator \0
=== One Value, Three Views ===
Stored value: 66As integer: 66As character: BAs hex: 42
All three lines read the SAME bits in memory.Only the DISPLAY FORMAT changes.คำตอบคำถาม:
- ทำไม
char[5]= 5 bytes? — เพราะchar= 1 byte x 5 ช่อง = 5 bytes ตรงๆ - ทำไม
sizeof("HELLO")= 6? — เพราะ “HELLO” มี 5 ตัวอักษร + 1 null terminator (\0) floatกับintต่างกันอย่างไร? — ทั้งคู่ 4 bytes แต่ ตีความ bits ต่างกันintเก็บเลขจำนวนเต็มตรงๆfloatแบ่ง bits เป็น sign, exponent, mantissa (IEEE 754)
Bonus — ทำไม Python ใช้ memory มากกว่า?
Python ทุกค่าเป็น object ที่มี: reference count, type pointer, value — overhead อย่างน้อย 16-28 bytes ต่อค่า ในขณะที่ C เก็บแค่ value ตรงๆ