Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> การเขียนโปรแกรม C
การเขียนโปรแกรม C
  1. รัน Zombie และ Orphan Process ในโปรแกรม C เดียว

    Zombie Process: Process จบแล้วแต่ยังอยู่ใน Process Table (รอ Parent อ่าน Exit Status) Orphan Process: Process ยังทำงานอยู่แต่ Parent จบไปแล้ว (ถูก Adopt โดย init) ตัวอย่างรันพร้อมกัน Zombie } } return 0; } ผลลัพธ์ Parent PID: 3821 Child PID: 3822, Parent PID: 686 (init) Grandchild

  2. SQL ด้วย C/C++ และ SQLite

    ใช้ SQLite ใน C/C++ ผ่าน C Interface API API หลัก APIคำอธิบาย sqlite3_open(filename, &db)เปิด/สร้าง Database คืน Connection Object sqlite3_exec(db, sql, callback, data, &errmsg)Execute SQL หลายคำสั่ง ใช้ Callback รับผลลัพธ์ sqlite3_close(db)ปิด Database Connection ตัวอย่าง: Connect, Create Table

  3. Octal Literals ใน C

    ใน C/C++ เขียน Octal Literal โดยนำหน้าด้วย 0 (ศูนย์) ตัวอย่าง int a = 025; // Octal 25 = Decimal 21 int b = 063; // Octal 63 = Decimal 51 printf(%d %d, a, b); ผลลัพธ์ 21 51

  4. Get/Set Stack Size ของ Thread Attribute ใน C (pthread)

    ใช้ pthread Attribute Functions: pthread_attr_getstacksize() int pthread_attr_getstacksize(pthread_attr_t *attr, size_t *stacksize) pthread_attr_setstacksize() int pthread_attr_setstacksize(pthread_attr_t *attr, size_t stacksize) ตัวอย่าง #include <stdio.h> #include <pthread.h> int m

  5. Dynamic Memory Allocation ใน C

    C จัดสรรหน่วยความจำ Dynamic ด้วยฟังก์ชันใน <stdlib.h>: FunctionDescription calloc(num, size)จัดสรร Array num องค์ประกอบ ขนาด size bytes เริ่มต้นเป็น 0 free(ptr)ปล่อยหน่วยความจำที่ ptr ชี้ malloc(num)จัดสรร num bytes ไม่ Initialize realloc(ptr, newsize)ปรับขนาดหน่วยความจำที่ ptr ชี้ ตัวอย่า

  6. สร้าง Multiple Processes ด้วย fork() ใน C

    fork() สร้าง Child Process คืนค่า: 0: Parent Process (คืน PID ของ Child) = 0: Child Process < 0: Error ตัวอย่าง 0) printf(Parent process\n); else printf(Child process\n); return 0; } ผลลัพธ์ Parent process Child process (ลำดับอาจสลับได้ตาม Scheduler)

  7. ความสำคัญของ Function Prototype ใน C

    Function prototype บอก compiler เกี่ยวกับจำนวน arguments, datatype ของ parameters, และ return type ช่วยตรวจสอบ function signature ก่อนเรียกใช้ถ้าไม่มี prototype: compiler อาจ compile ได้แต่มี warning และอาจให้ผลลัพธ์ผิดปกติถ้าฟังก์ชันถูกเรียกก่อน define (define อยู่หลัง) → ต้องมี prototype มิฉะนั้น

  8. ฟังก์ชันที่รันก่อนและหลัง main() ใน C (Constructor/Destructor Attributes)

    ใช้ GCC Attribute กำหนดฟังก์ชันรันก่อน/หลัง main: __attribute__((constructor)): รันก่อน main() __attribute__((destructor)): รันหลัง main() ตัวอย่าง #include <stdio.h> void before_main() __attribute__((constructor)); void after_main() __attribute__((destructor)); void before_main() { print

  9. นับจำนวน Arguments แบบ Variable ใน C

    นับจำนวน Variadic Arguments ได้ 3 วิธี: ส่งจำนวนเป็น Argument แรก (เช่น printf format string) ใช้ Sentinel Value (เช่น NULL) เป็น Argument ตัวสุดท้าย ใช้ Format String แบบ printf/scanf ตัวอย่าง: ส่ง Count เป็น Argument แรก #include <stdio.h> #include <stdarg.h> int get_sum(int count,

  10. ลำดับการ Evaluate พารามิเตอร์ฟังก์ชันใน C

    ลำดับการ Evaluate Function Arguments ใน C เป็น Unspecified Behavior (ขึ้นกับ Compiler) แต่ส่วนใหญ่ทำ Right-to-Left ตัวอย่างทดสอบ void test(int x, int y, int z) { printf(x=%d y=%d z=%d\n, x, y, z); } int main() { int a = 10; test(a++, a++, a++); } ผลลัพธ์ทั่วไป (Right-to-Left) x=12 y=1

  11. วิธี Return หลายค่าจากฟังก์ชันใน C/C++

    C/C++ ไม่ Return หลายค่าโดยตรง ใช้ Call by Address หรือ Call by Reference แทน ตัวอย่าง: หารeturn Quotient และ Remainder พร้อมกัน #include <stdio.h> void div(int a, int b, int *quotient, int *remainder) { *quotient = a / b; *remainder = a % b; } int main() { int a = 76, b = 10, q

  12. วัตถุประสงค์ของ Function Prototype ใน C/C++

    Function Prototype บอกคอมไพเลอร์ล่วงหน้าเกี่ยวกับ: จำนวนพารามิเตอร์ ชนิดข้อมูลพารามิเตอร์ และ Return Type ประโยชน์: Cross-check Function Signature ก่อนเรียกใช้ ป้องกัน Warning/Error จาก Implicit Declaration อนุญาตให้เรียกฟังก์ชันก่อน Definition ได้ ตัวอย่างโดยไม่มี Prototype (มี Warning) main()

  13. เกิดอะไรขึ้นเมื่อเรียกฟังก์ชันก่อน Declaration ใน C?

    หากเรียกฟังก์ชันก่อน Declaration โดยไม่มี Prototype คอมไพเลอร์สมมติ Return Type เป็น int (Implicit int) ทำงานได้ (อาจมี Warning) Error: Conflicting Types ตัวอย่าง Error main() { printf(%d, function()); } char function() { return T; } // Error: conflicting types ตัวอย่าง OK main() { printf(%d,

  14. _Noreturn Function Specifier ใน C

    _Noreturn บอกคอมไพเลอร์ว่าฟังก์ชันนี้ไม่ Return (เช่น exit(), abort()) หากมี return statement จะ Error ตัวอย่าง Error _Noreturn void function() { return; // Warning: function declared noreturn has a return statement } ตัวอย่างปกติ int function() { return 86; } // OK

  15. Callback Function ในภาษา C

    Callback function คือโค้ดที่ executable ถูกส่งเป็น argument ให้โค้ดอื่น เพื่อเรียกกลับ (call back) หรือ execute ในเวลาที่กำหนดใน C ใช้ function pointer เพื่อเรียก callback functionตัวอย่างโค้ด#include <stdio.h> void my_function() { printf(This is a normal function.); } void my_callback_f

  16. Command Line Arguments ใน C

    รับค่าจาก Command Line ผ่าน main(int argc, char *argv[]) argc: จำนวนอาร์กิวเมนต์ (รวมชื่อโปรแกรม) argv[]: Array ของ String แต่ละอาร์กิวเมนต์ ตัวอย่าง 2) printf(Too many arguments supplied.\n); else printf(One argument expected.\n); return 0; } ผลลัพธ์ $ ./a.out testing T

  17. Callback Functions ในภาษา C

    Callback: ส่ง Function Pointer เป็น Argument ให้ฟังก์ชันอื่น เรียกใช้ภายหลัง ตัวอย่าง #include <stdio.h> void my_function() { printf(This is a normal function.); } void my_callback_function(void (*ptr)()) { printf(This is callback function.\n); (*ptr)(); // เรียก Callback } in

  18. ความหมายของ c = a+++b ใน C/C++

    นิพจน์ c = a+++b คอมไพเลอร์ Parse ตาม Maximal Munch Rule (อ่าน Token ยาวที่สุด) การ Parse (a++) + b // Post-increment แล้วบวก b ไม่ใช่ a + (++b) ตัวอย่าง int a = 2, b = 5; int c = a+++b; // (a++) + b = 2 + 5 = 7 ผลลัพธ์ C is: 7

  19. C/C++ Struct vs Class

    ใน C++ Struct และ Class เหมือนกันเกือบทุกอย่าง แตกต่างแค่ Default Access: Class: Members เป็น private โดย Default Struct: Members เป็น public โดย Default ตัวอย่าง Class (Private Default) class MyClass { int x = 10; }; MyClass obj; obj.x; // Error: private ตัวอย่าง Struct (Public Default) struct

  20. ข้อดีของภาษา C เทียบกับภาษาอื่นๆ

    C พัฒนาโดย Dennis Ritchie (1970s) เพื่อ Redesign UNIX OS แทนภาษา B ที่ไม่รองรับ Struct และ Data Types ข้อดีของ C Middle-level: ทำได้ทั้ง Low-level (Driver/Kernel) และ High-level (Application) Structured: แบ่งโปรแกรมซับซ้อนเป็น Functions ย่อย Embedded Systems: เข้าถึง Hardware API, Dynamic Memory ไ

Total 1447 -คอมพิวเตอร์  FirstPage PreviousPage NextPage LastPage CurrentPage:13/73  20-คอมพิวเตอร์/Page Goto:1 7 8 9 10 11 12 13 14 15 16 17 18 19