Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> การเขียนโปรแกรม C
การเขียนโปรแกรม C
  1. ฟังก์ชัน raise() ใน C/C++: ส่ง Signal ให้ตัวเอง

    ฟังก์ชัน raise() ใช้ส่ง Signal ให้ Process ตัวเอง (Self-signal) เพื่อทดสอบ Signal Handlers หรือ Trigger การจัดการ Event ต่างๆ อยู่ใน Header <signal.h> ไวยากรณ์ #include <signal.h> int raise(int sig); sig - Signal Number ที่จะส่ง (เช่น SIGINT, SIGTERM, SIGILL) คืนค่า: 0 = สำเร็จ, Non-z

  2. ฟังก์ชัน mbrlen() ใน C/C++: หาความยาว Multibyte Character

    ฟังก์ชัน mbrlen() ใช้หาความยาว (ในหน่วย Bytes) ของ Multibyte Character หนึ่งตัว จาก Multibyte String ใช้สำหรับการประมวลผล Unicode/UTF-8 และ Multibyte Encodings อื่นๆ อยู่ใน Header <wchar.h> ไวยากรณ์ #include <wchar.h> size_t mbrlen(const char *s, size_t n, mbstate_t *ps); s - Pointer

  3. วิธีเคลียร์ Console (จอภาพ) ใน C

    มีหลายวิธีในการเคลียร์หน้าจอ Console ใน C ขึ้นอยู่กับ Platform และ Compiler วิธีที่ 1: clrscr() (Turbo C / Borland / DOS) #include <conio.h> void main() { clrscr(); // เคลียร์หน้าจอ printf(Hello World); getch(); } ข้อจำกัด: เฉพาะ Turbo C, Borland C, DOS/Windows Console แบบเก่า ไ

  4. ฟังก์ชัน isgraph() ใน C: ตรวจสอบกราฟิกตัวอักษร

    ฟังก์ชัน isgraph() ตรวจสอบว่าตัวอักษรมี Graphical Representation (รูปแบบที่มองเห็นได้/พิมพ์ได้) หรือไม่ อยู่ใน Header <ctype.h> ไวยากรณ์ #include <ctype.h> int isgraph(int c); Parameter: int c - ตัวอักษรที่จะตรวจสอบ (ส่งเป็น unsigned char หรือ EOF) คืนค่า: Non-zero (True) ถ้ามี Graphi

  5. <climits> (limits.h) ใน C/C++: ขีดจำกัดชนิดข้อมูล

    Header <limits.h> (C) และ <climits> (C++) กำหนด Macro Constants สำหรับขีดจำกัดของชนิดข้อมูลพื้นฐาน (Fundamental Types) ช่วยให้เขียนโค้ดที่ Portable และไม่ขึ้นกับ Platform Macros สำคัญใน limits.h / climits Character Types Macroคำอธิบายค่าตัวอย่าง (Typical) CHAR_BITBits per Byte8 CHAR_M

  6. วิธีพิมพ์เลข 1 ถึง 100 โดยไม่ใช้ Loop ใน C

    มีหลายวิธีพิมพ์ตัวเลข 1-100 โดยไม่ใช้ Loop (for/while/do-while) ใช้ Recursion, Goto, หรือ Function Calls วิธีที่ 1: Recursion (ฟังก์ชันเรียกตัวเอง) #include <stdio.h> void printNumbers(int n) { if (n <= 100) { printf(%d , n); printNumbers(n + 1); // Recursive Call

  7. Wild Pointers (ตัวชี้ขี้เลว) ใน C/C++ คืออะไร

    Wild Pointer คือ Pointer ที่ชี้ไปที่หน่วยความจำที่ ไม่ได้จัดสรร (Unallocated) หรือ ถูกปล่อยแล้ว (Deallocated/Freed) ต่างจาก Pointer ปกติที่ชี้ที่อยู่ที่ถูกต้อง สาเหตุหลัก Uninitialized Pointer: ประกาศ Pointer แต่ไม่ได้กำหนดค่าเริ่มต้น Dangling Pointer: Pointer ชี้ไปที่ Memory ที่ free() / delete ไ

  8. สลับค่าตัวแปรสองตัวในบรรทัดเดียวใน C/C++

    สามารถสลับค่าตัวแปรสองตัวได้ในบรรทัดเดียว (One-liner) หลายวิธี วิธีที่ 1: Arithmetic Method a += b -= a = b - a; การทำงาน (a=28, b=8) 1. a = b - a → a = 8 - 28 = -20 2. b -= a → b = 8 - (-20) = 28 3. a += b → a = -20 + 28 = 8 Result: a=8, b=28 ✓ วิธีที่ 2: XOR Swap (Bitwise) (a ^=

  9. พิมพ์ "Hello World" ใน C/C++ โดยไม่ใช้ Header Files

    ปกติเราใช้ Header Files (stdio.h, iostream) เพื่อเข้าถึง Built-in Functions เช่น printf() แต่จริงๆ แล้วสามารถเรียกใช้ได้โดย Forward Declaration เอง วิธีการ: Forward Declaration ประกาศ Prototype ของ printf() เองในโค้ด โดยไม่ Include Header ตัวอย่างใน C // Forward Declaration ของ printf int printf(c

  10. ฟังก์ชัน strftime() ใน C/C++: จัดรูปแบบวันที่และเวลา

    ฟังก์ชัน strftime() ใช้จัดรูปแบบวันที่และเวลาเป็น String ตาม Locale ที่กำหนด อยู่ใน Header <time.h> ไวยากรณ์ #include <time.h> size_t strftime(char *str, size_t maxsize, const char *format, const struct tm *timeptr); str - Buffer รับผลลัพธ์ maxsize - ขนาด Buffer สูงสุด (ป้องกัน Overfl

  11. exit(), abort() และ assert() ใน C/C++: ความแตกต่างและการใช้งาน

    ทั้งสามฟังก์ชันใช้สำหรับจบโปรแกรมแต่มีพฤติกรรมและจุดประสงค์ต่างกัน exit() - Normal Termination จบโปรแกรมอย่างปกติ พร้อม Cleanup ครบถ้วน พฤติกรรม เรียก atexit() Handlers (LIFO) Flush I/O Buffers ปิด Open Streams/Files เรียก Destructors ของ Static/Thread-local Objects (C++) คืน Status ให้ OS ไวยา

  12. Static Functions (ฟังก์ชัน Static) ใน C

    ฟังก์ชัน Static (Static Function) ใน C คือ ฟังก์ชันที่มี Scope จำกัดอยู่ใน Object File (Translation Unit) เดียวกัน ไม่สามารถเรียกใช้จากไฟล์อื่นได้ ใช้คีย์เวิร์ด static นำหน้าประกาศฟังก์ชัน ไวยากรณ์ static return_type function_name(parameters) { // function body } ตัวอย่าง: Static Function ในไฟ

  13. ฟังก์ชัน pow() ใน C: การคำนวณเลขยกกำลัง

    ฟังก์ชัน pow() ใช้คำนวณค่าของฐาน (Base) ยกกำลัง (Exponent) อยู่ใน Header <math.h> ไวยากรณ์ #include <math.h> double pow(double base, double exponent); base - ค่าฐาน (Base Value) exponent - ค่ากำลัง (Power Value) คืนค่า: baseexponent เป็น double ตัวอย่าง #include <stdio.h> #inc

  14. ค่าเริ่มต้นของตัวแปร Static ใน C

    ตัวแปร Static ใน C จะถูกเริ่มต้นค่าเป็น ศูนย์ (0) อัตโนมัติ หากไม่ได้ระบุค่าเริ่มต้นชัดเจน นี่เป็นพฤติกรรมที่รับประกันโดยมาตรฐาน C คุณสมบัติ Static Variables Initialized Once: เริ่มต้นค่าเพียงครั้งเดียว (Compile-time/Load-time) Default Zero: ค่าเริ่มต้นคือ 0 สำหรับทุกชนิดข้อมูล Lifetime: ตลอดการทำ

  15. ชนิดข้อมูลที่คืนของ getchar(), fgetc() และ getc() ใน C

    ฟังก์ชันอ่านตัวอักษร 3 ตัวนี้คืนค่าเป็น int ไม่ใช่ char เพื่อให้สามารถแทนค่า EOF (End of File) ได้ ซึ่งมักเป็น -1 และอยู่นอกช่วงของ char getchar() อ่านตัวอักษรจาก stdin (Standard Input) ไวยากรณ์ #include <stdio.h> int getchar(void); ตัวอย่าง #include <stdio.h> int main() { int ch

  16. ค่าที่คืนของ printf() และ scanf() ใน C

    ฟังก์ชัน printf() และ scanf() เป็น Library Functions ใน <stdio.h> ที่คืนค่าข้อมูลสำคัญซึ่งมักถูกมองข้าม printf() - ค่าที่คืน printf() คืนค่า จำนวนตัวอักษรที่พิมพ์สำเร็จ (Number of Characters Printed) หากเกิดข้อผิดพลาด จะคืนค่า ลบ (Negative Value) ตัวอย่าง #include <stdio.h> int main()

  17. การเริ่มต้นตัวแปร Static ใน C

    คีย์เวิร์ด static ใน C มีความหมายหลากหลายขึ้นอยู่กับบริบท แต่สำหรับตัวแปร (Variables) จะทำให้ตัวแปรมี Lifetime ตลอดการทำงานของโปรแกรม และ Scope จำกัด ตามที่ประกาศ คุณสมบัติของ Static Variables Initialized Once: เริ่มต้นค่าเพียงครั้งเดียว (ครั้งแรกที่ผ่านบรรทัดนั้น) Persist Value: รักษาค่าไว้ระหว่า

  18. ฟังก์ชัน C สำหรับสลับสตริง (Swap Strings)

    การสลับสตริงใน C สามารถทำได้โดยการสลับตัวอักษรทีละตัว (Character-by-character) เนื่องจาก C ไม่มี String Type ในตัว ใช้ Char Array แทน ตัวอย่าง: สลับสตริงด้วยลูป While #include <stdio.h> #include <string.h> int main() { char st1[] = My 1st string; char st2[] = My 2nd string;

  19. return statement เทียบกับ exit() ใน main() ของ C++

    ทั้ง return และ exit() ในฟังก์ชัน main() ทำให้โปรแกรมจบการทำงาน แต่มีความแตกต่างสำคัญในการทำ Cleanup และ Destructors return Statement return ใน main(): จบการทำงานของ main() และคืนค่าให้ OS เรียก Destructors ของ Object ที่มี Automatic Storage Duration (Local Objects) - Stack Unwinding เรียก Destruc

  20. ความแตกต่างระหว่าง exit() และ _Exit() ใน C/C++

    ทั้ง exit() และ _Exit() ใช้เพื่อจบโปรแกรม แต่มีความแตกต่างในการทำ Cleanup และการเรียก Destructors exit() exit() จบโปรแกรมทันที โดยทำ Cleanup ต่างๆ ก่อน: เรียกฟังก์ชันที่ลงทะเบียนด้วย atexit() (LIFO order) Flush I/O buffers (stdout, stderr, stdin) ปิด Stream/ไฟล์ที่เปิดอยู่ทั้งหมด เรียก Destructors

Total 1447 -คอมพิวเตอร์  FirstPage PreviousPage NextPage LastPage CurrentPage:7/73  20-คอมพิวเตอร์/Page Goto:1 2 3 4 5 6 7 8 9 10 11 12 13