C-1.2 printf Formats
ข้อมูลเดียวกันแต่แสดงต่างกันได้ — นี่คือแนวคิดพื้นฐานที่สุดของ “stored vs displayed” และ C คือที่ที่มันเกิดขึ้นครั้งแรก
Format Specifiers คืออะไร?
Section titled “Format Specifiers คืออะไร?”ใน C, printf() ไม่ได้รู้อัตโนมัติว่าคุณอยากแสดงข้อมูลแบบไหน คุณต้อง บอกมัน ด้วย format specifier:
| Specifier | ใช้กับ | แสดงผลเป็น |
|---|---|---|
%d | int | ตัวเลขจำนวนเต็ม (decimal) |
%f | float, double | ทศนิยม |
%c | char | ตัวอักษร 1 ตัว |
%s | char[] / char* | ข้อความ (string) |
%zu | size_t | ขนาด (จาก sizeof) |
%x | int | เลขฐาน 16 (hexadecimal) |
%o | int | เลขฐาน 8 (octal) |
ข้อมูลเดียวกัน — 4 หน้าตา
Section titled “ข้อมูลเดียวกัน — 4 หน้าตา”นี่คือ insight ที่สำคัญที่สุดของบทเรียนนี้:
#include <stdio.h>
int main() { char ch = 65; // เก็บเลข 65 ลงใน char
printf("As integer (%%d): %d\n", ch); // 65 printf("As character (%%c): %c\n", ch); // A printf("As hex (%%x): %x\n", ch); // 41 printf("As octal (%%o): %o\n", ch); // 101
printf("\n--- ข้อมูลเดียวกัน 4 แบบ ---\n"); printf("Memory: เก็บ bits เดียวกันทุกบรรทัด\n"); printf("ต่างกันแค่: วิธีที่เรา *อ่าน* มัน\n");
return 0;}ผลลัพธ์:
As integer (%d): 65As character (%c): AAs hex (%x): 41As octal (%o): 101สิ่งสำคัญ: ตัวแปร ch เก็บ bits เดียวกันทุกกรณี — 01000001 — ต่างกันแค่วิธีที่ printf แสดงผล มัน
นี่คือแนวคิด stored vs displayed ที่ทุกเครื่องมืออื่นสืบทอดมา
Sheets มีแนวคิดเดียวกันแต่ซ่อนไว้ใน Format > Number:
เซลล์เก็บ 0.75 แต่คุณเลือกแสดงเป็น:
0.75(Number)75%(Percent)3/4(Fraction)18:00(Time — 75% ของวัน)
ข้อมูลเดียวกัน หน้าตาต่างกัน — เหมือน printf ทุกประการ
ch = 65print(f"integer: {ch}") # 65print(f"char: {chr(ch)}") # Aprint(f"hex: {hex(ch)}") # 0x41Python ใช้ f-string หรือ format() แทน printf — แต่แนวคิดเดียวกัน
SELECT 65, CHR(65), TO_HEX(65);-- ผลลัพธ์: 65, 'A', '41'SQL ใช้ functions แทน format specifiers แต่ผลเหมือนกัน — แปลงวิธีแสดงผล
printf กับ Float Formatting
Section titled “printf กับ Float Formatting”#include <stdio.h>
int main() { double price = 1234.5;
printf("Default: %f\n", price); // 1234.500000 printf("2 decimals: %.2f\n", price); // 1234.50 printf("No decimals: %.0f\n", price); // 1235 (rounded!) printf("Width 10: %10.2f\n", price); // ' 1234.50' printf("Left align: %-10.2f|\n", price); // '1234.50 |'
// Scientific notation printf("Scientific: %e\n", price); // 1.234500e+03
return 0;}ผลลัพธ์:
Default: 1234.5000002 decimals: 1234.50No decimals: 1235Width 10: 1234.50Left align: 1234.50 |Scientific: 1.234500e+03สังเกต: %.0f ได้ 1235 ไม่ใช่ 1234 — printf ปัดเศษ ให้อัตโนมัติ
นี่คือต้นกำเนิดของ:
- Sheets: Format > Number > ทศนิยม 2 ตำแหน่ง
- Python:
f"{price:.2f}" - SQL:
ROUND(price, 2)
ทุกเครื่องมือสืบทอดแนวคิดนี้มาจาก printf
Sheets ใช้ custom number format เช่น #,##0.00 ซึ่งเทียบเท่า %10.2f ใน C
ข้อมูลในเซลล์ไม่เปลี่ยน — เปลี่ยนแค่การแสดงผล
price = 1234.5print(f"{price:.2f}") # 1234.50 — เหมือน printf("%.2f")SELECT ROUND(1234.5, 2); -- 1234.50SELECT TO_CHAR(1234.5, '9999.99'); -- PostgreSQL: ' 1234.50'ระวัง: Wrong Format = Wrong Output
Section titled “ระวัง: Wrong Format = Wrong Output”#include <stdio.h>
int main() { int num = 42; float pi = 3.14f;
// ถูก printf("int with %%d: %d\n", num); // 42 printf("float with %%f: %f\n", pi); // 3.140000
// ผิด! — ใช้ format ไม่ตรงกับ type printf("int with %%f: %f\n", num); // ขยะ! (undefined behavior) printf("float with %%d: %d\n", pi); // ขยะ!
return 0;}C ไม่ตรวจให้คุณ — คุณต้องจับคู่ format กับ type เอง ภาษาอื่นๆ เรียนรู้จากจุดอ่อนนี้ แล้วทำให้ type checking เป็นอัตโนมัติ
Sheets ไม่มีปัญหานี้เพราะมันจัดการ types ให้อัตโนมัติ — แต่นั่นหมายความว่าคุณไม่มีทาง รู้ ว่า type ตรงหรือเปล่า จนกว่าจะ error
# Python จะ error ถ้า type ไม่ตรงprint(f"{42:.2f}") # "42.00" — OK, Python แปลง int→float ให้SELECT CAST('hello' AS INTEGER); -- ERROR! ไม่สามารถแปลงได้