Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> การเขียนโปรแกรม C
การเขียนโปรแกรม C
  1. Escape Sequences ในภาษา C

    Escape Sequence: Backslash (\) + ตัวอักษร = ความหมายพิเศษ Sequenceคำอธิบาย \tTab \bBackspace \nNewline \rCarriage Return \fForm Feed \Single Quote \Double Quote \\Backslash ตัวอย่าง char ch1 = \t, ch2 = \n; printf(Test for tabspace %c and a newline %c will start here, ch1, ch2);

  2. ความจำเป็นของ Long Data Type ใน C

    C มี Integer Types 4 แบบ: short, int, long, long long ขนาดแตกต่างตาม Architecture/Compiler ตัวอย่างขนาด Size of int: 4 Bytes Size of long: 4 Bytes (Windows 64-bit) / 8 Bytes (Linux 64-bit) Size of long long: 8 Bytes เหตุผลทางฮาร์ดแวร์ CPU เรียกข้อมูลจาก RAM ผ่าน Memory Address Register (MAR) ขนาด

  3. Const Qualifier ในภาษา C

    const ประกาศตัวแปรแบบ Read-only ไม่สามารถเปลี่ยนค่าหลัง Initialize ประโยชน์ ป้องกันการเปลี่ยนค่าผิดพลาด (เช่นค่าคงที่ PI) คอมไพเลอร์อาจวางใน Read-only Memory ถ้าไม่ได้取 Address อาจไม่จัดสรรหน่วยความจำเลย ตัวอย่าง const int x = 10; x = 12; // Error: assignment of read-only variable x

  4. Scansets ในภาษา C

    Scanset (%[]) ใน scanf ระบุชุดตัวอักษรที่ยอมรับ (Case Sensitive) ตัวอย่าง: รับเฉพาะ A-Z scanf(%[A-Z]s, str); Output: HE (หยุดที่ l เพราะเป็น Lowercase) ตัวอย่าง: ยกเว้นตัวอักษร (^) scanf(%[^r]s, str); Output: HelloWo (หยุดที่ r) ตัวอย่าง: รับทั้งบรรทัด (รวม Space) scanf(%[^\n]s, str); Input: He

  5. Comma Operator และ Comma Separator ใน C/C++

    Comma (,) ใน C/C++ มี 2 บทบาท: 1. Comma Operator (Binary Operator) Evaluate operand ซ้าย ทิ้งผล แล้ว Evaluate operand ขวา คืนค่าขวา Precedence ต่ำสุด int x = (50, 60); // x = 60 int y = (func1(), func2()); // เรียก func1() แล้ว func2() คืนค่า func2() 2. Comma Separator คั่นพารามิเตอร์ใน Functio

  6. Modulus ของจำนวนลบใน C

    ใน C ผลลัพธ์ของ a % b จะมีเครื่องหมายเดียวกับ ตัวถูกหาร (a) ตัวอย่าง ผลลบ ความสำคัญของ Precedence a % b / c // % และ / มี Precedence เท่ากัน Associativity ซ้าย-ขวา // ทำ % ก่อน แล้วค่อย /

  7. เขียน C Macro PRINT(x) ที่พิมพ์ค่า x

    ใช้ Stringize Operator (#) แปลง Argument เป็น String แล้วพิมพ์ด้วย printf ตัวอย่าง #include <stdio.h> #define PRINT(x) printf(#x) int main() { PRINT(Hello); printf(\n); PRINT(26); printf(\n); PRINT(2.354721); printf(\n); return 0; } ผลลัพธ์ Hello 26 2.354721

  8. Multiline Macros ในภาษา C

    เขียน Macro หลายบรรทัด ต้องทุกบรรทัดด้วย Backslash (\) และควรห่อด้วย Parentheses ป้องกันข้อผิดพลาด ตัวอย่าง #include <stdio.h> #define PRINT(x, str) ({ \ printf(The number %d, x); \ printf( is ); \ printf(#str); \ printf(\n); \ }) int main() { int x = 10; if (x % 2 =

  9. ความแตกต่างระหว่าง C Structures และ C++ Structures

    C++ Struct ใกล้เคียง Class ความแตกต่างหลัก: C StructureC++ Structure ไม่มี Member Functionsมี Member Functions ได้ ไม่สามารถ Initialize โดยตรงได้Initialize โดยตรงได้ ต้องเขียน struct ตอนประกาศตัวแปรไม่ต้องเขียน struct ไม่มี Static Membersมี Static Members ได้ sizeof(empty struct) = 0sizeof(empty s

  10. Anonymous Union และ Structure ใน C

    Anonymous Union/Structure คือ Union/Structure ที่ไม่มีชื่อ ใช้เป็น Nested Member เข้าถึง Member โดยตรงไม่ต้องผ่านชื่อ ตัวอย่าง #include <stdio.h> struct point { struct { // Anonymous Structure int x; int y; }; }; int main() { struct point pt; pt.x = 10; // เ

  11. Compound Literals ในภาษา C (C99)

    Compound Literals (มาตรฐาน C99) สร้าง Unnamed Object ได้โดยไม่ต้องประกาศตัวแปร ตัวอย่าง #include <stdio.h> struct point { int x; int y; }; void display_point(struct point pt) { printf((%d,%d)\n, pt.x, pt.y); } int main() { display_point((struct point){10, 20}); // Compound Literal

  12. Memory Layout ของโปรแกรม C

    หน่วยความจำของโปรแกรม C แบ่งเป็น 4 Segment หลัก: Segmentคำอธิบาย Stackข้อมูลชั่วคราว: พารามิเตอร์ฟังก์ชัน Return Address ตัวแปร Local ใช้ LIFO ขยายจาก Address สูงไปต่ำ HeapDynamic Memory: malloc() calloc() realloc() ขยายจาก Address ต่ำไปสูง Text (Code)Machine Instructions โค้ดโปรแกรม อ่านอย่างเดีย

  13. free() รู้ขนาดหน่วยความจำที่จะ Deallocate ยังไง?

    free(ptr) ไม่รับขนาดเป็นพารามิเตอร์ แต่รู้ขนาดได้เพราะ: เมื่อจัดสรรหน่วยความจำแบบ Dynamic (malloc/calloc/realloc) ระบบจะจัดสรรพื้นที่ ใหญ่กว่าที่ขอ 1 Word Word เพิ่มเติมนี้ใช้เก็บ ขนาดของบล็อก ที่จัดสรรไว้ free() จึงอ่านขนาดจากที่นี่เพื่อปล่อยคืนพื้นที่ที่ถูกต้อง

  14. แปลงโค้ด C/C++ เป็น Assembly Language

    ใช้ GCC ตัวเลือก -S เพื่อสร้าง Assembly Output คำสั่ง gcc -S program.cpp ตัวอย่างโค้ด C++ #include <iostream> using namespace std; int main() { int x = 50, y = 60, sum; sum = x + y; cout << Sum is: << sum << endl; } Output (Assembly x86-64) .file test_cpp.cpp

  15. Macros vs Functions ในภาษา C

    Macro: Pre-processed (Text Substitution) ก่อนคอมไพล์ Function: Compiled เป็น Machine Code ปัญหาของ Macro ไม่มี Type Checking Debug ยาก (แทนที่ข้อความโดยตรง) ไม่มี Namespace (ชนกันได้) เพิ่มขนาดโค้ด (Code Bloat) ไม่ตรวจสอบ Compile-time Error ตัวอย่างปัญหา: SQUARE Macro #define SQUARE(x) x * x int

  16. วิธีแก้ไขค่าตัวแปร const ใน C

    ตัวแปร const ไม่สามารถเปลี่ยนค่าโดยตรงได้ (Compile Error) แต่สามารถเปลี่ยนผ่าน Pointer ได้ ตัวอย่าง: พยายามเปลี่ยนโดยตรง (Error) const int x = 10; x = 15; // Error: assignment of read-only variable x ตัวอย่าง: เปลี่ยนผ่าน Pointer #include <stdio.h> int main() { const int x = 10; int

  17. ฟังก์ชัน wcspbrk() ใน C/C++

    wcspbrk() ค้นหาชุด Wide Characters ใน Wide String หนึ่ง ต่ออีก Wide String หนึ่ง อยู่ใน <cwchar> ไวยากรณ์ wchar_t* wcspbrk(const wchar_t* dest, const wchar_t* src) dest: Wide String ที่จะถูกค้นหา src: Wide String ที่มีชุดตัวอักษรที่ต้องการค้นหา ค่าคืน พบ: ตัวชี้ไปยัง Wide Character แรกที

  18. ค่าเริ่มต้นของ Primitive Data Types ที่ไม่ได้ Initialize ใน C/C++

    ค่าของตัวแปร Primitive ที่ไม่ได้ Initialize (Uninitialized) เป็น Indeterminate (Garbage Value) แตกต่างระหว่างระบบและคอมไพเลอร์ ตัวอย่าง #include <iostream> using namespace std; int main() { char a; float b; int c; double d; long e; cout << a << \n << b << \n &

  19. โปรแกรม C ทำงานอย่างไร? (Compilation Process)

    ขั้นตอนการคอมไพล์โปรแกรม C: C Source Code - โค้ดที่เขียน Preprocessing - รวม Header Files (#include), Expand Macro (#define) Compiler - แปลงเป็น Assembly Code Assembler - แปลง Assembly เป็น Object Code (Machine Code ใกล้เคียง) Linker - ลิงก์ Object Code กับ Library อื่นๆ สร้าง Executable Loader -

  20. แปลงโปรแกรม C/C++ เป็น Preprocessor Code

    ใช้ GCC/G++ ตัวเลือก -E เพื่อดูโค้ดหลัง Preprocessing (Expand Macro, Include Header) คำสั่ง g++ -E program.cpp ตัวอย่าง // Source #define PI 3.1415 int main() { float a = PI, r = 5; float c = a * r * r; return 0; } Output (Preprocessed) int main() { float a = 3.1415, r = 5; fl

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