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

    Register storage class = เก็บตัวแปรใน CPU register แทน RAM เร็วกว่า คุณสมบัติ Keyword: register เก็บใน CPU register (temporary storage) เข้าถึงเร็วกว่าตัวแปรปกติ ไม่สามารถ取 address ได้ (& ไม่ได้) เหมาะกับ loop counter, ตัวแปรที่ใช้บ่อย โปรแกรมที่ 1: Register loop counter #include <stdio.h

  2. Scope Rules ของฟังก์ชันใน C: Local vs Global Variables

    Scope rules กำหนด: เข้าถึงตัวแปรได้ที่ไหน, อายุของตัวแปร, ขอบเขตการใช้งาน กฎขอบเขตฟังก์ชัน ตัวแปรใน function = local variable ใช้ได้แค่ในฟังก์ชันนั้น สิ้นสุดเมื่อฟังก์ชัน return ฟังก์ชันอื่นและ main เข้าถึงไม่ได้ ตัวอย่างที่ 1: Call by Value (Local Variables) #include <stdio.h> void swap(i

  3. ส่ง Array ทั้งอาร์เรย์เป็น Argument ให้ Function ใน C

    ส่ง array ทั้งชุดให้ฟังก์ชัน: ส่งชื่ออาร์เรย์ (base address) ฟังก์ชันรับเป็น parameter array Syntax // เรียก display(array); // รับ void display(int arr[5]) { ... } // หรือ void display(int *arr) { ... } // หรือ void display(int arr[]) { ... } โปรแกรมที่ 1: ส่ง array เข้า display() #include <

  4. Pointer และ Two-Dimensional Array ในภาษา C

    Pointer เก็บ address ของตัวแปร 2D array จัดเก็บในหน่วยความจำแบบ row-major Memory Layout ของ 2D Array int a[3][3] = {1,2,3,4,5,6,7,8,9}; // Address base = 1234 (สมมติ) // a[i][j] = *(base + i*cols + j) * scale_factor โปรแกรม: ใช้ Pointer เข้าถึง 2D Array #include <stdio.h> main() { int a

  5. Array of Pointers to Strings ใน C: พิมพ์ค่าและ Address

    Array of pointers to strings = อาร์เรย์ที่แต่ละ element เป็น pointer ชี้ไปยัง string การประกาศ char *a[] = {one, two, three}; // a[0] ชี้ one, a[1] ชี้ two, ... ข้อดี ไม่ต้องกำหนดขนาดคงที่สำหรับแต่ละ string (ประหยัด memory) string แต่ละตัวใช้พื้นที่เท่าที่จำเป็น โปรแกรมที่ 1: พิมพ์ค่าและ Addre

  6. โปรแกรม C หาผลรวมขอบเขตเมทริกซ์ (Boundary Elements Sum)

    หาตัวเลขที่อยู่ขอบเมทริกซ์ (แถวแรก แถวสุดท้าย คอลัมน์แรก คอลัมน์สุดท้าย) และหาผลรวม ตัวอย่าง Given matrix: 1 2 3 4 5 6 7 8 9 Boundary matrix: 1 2 3 4 6 7 8 9 Sum = 1+2+3+4+6+7+8+9 = 40 ตรรกะ if (i == 0 || j == 0 || i == m-1 || j == n-1) { // เป็น boundary element sum += mat[i][j]; }

  7. Function Return ในภาษา C: การส่งค่ากลับจากฟังก์ชัน

    Function = บล็อกโค้ดทำงานเฉพาะเจาะจง ข้อดี: reuse, ลดโค้ด, debug ง่าย, modular การ Return ค่า ฟังก์ชันอาจ return ค่า หรือไม่ return (void) return type: int, float, char, struct, pointer ฯลฯ default return type = int ใช้ return expression; ส่งค่ากลับ ตัวอย่างฟังก์ชัน Return ค่า int total() {

  8. แปลงฐานสิบเป็นฐานสอง (Decimal to Binary) ใน C

    โจทย์ แปลงเลขฐานสิบ (Decimal) เป็นฐานสอง (Binary) โดยใช้ฟังก์ชัน ตรรกะ while (dno != 0) { rem = dno % 2; bno = bno + rem * f; f = f * 10; dno = dno / 2; } โปรแกรม Decimal to Binary #include <stdio.h> long tobinary(int); int main() { long bno; int dno; printf(Ente

  9. Constant Type Qualifier (const) ในภาษา C

    Type qualifiers เพิ่มคุณสมบัติพิเศษให้ datatype: const, volatile, restrict Const - ค่าคงที่ 3 ประเภทค่าคงที่ Literal constants - ค่าตัวเลข/ตัวอักษรโดยตรง: a = b + 7 (7 คือ literal) Defined constants - #define: #define PI 3.1415 Memory constants - ใช้ const qualifier: const float pi = 3.1415; Syn

  10. Pre-processor Commands ในภาษา C: Macro, Include, Conditional

    Preprocessor = โปรแกรมประมวลผลก่อน compile เริ่มด้วย # 3 ประเภทหลัก Macro substitution - #define File inclusion - #include Compiler control - #ifdef, #ifndef, #else, #endif 1. Macro Substitution #define PI 3.1415 #define SQUARE(x) x*x #undef PI // ยกเลิก macro ตัวอย่าง #define wait getch() main

  11. Inline Function ในภาษา C คืออะไร

    Inline function = ฟังก์ชันที่ compiler อาจแทนที่การเรียกด้วยโค้ดจริง (function substitution) เพื่อลด overhead คุณสมบัติ เหมาะสำหรับฟังก์ชันเล็ก คำนวณเร็ว ไม่เหมาะฟังก์ชันใหญ่/ซับซ้อน เพิ่ม keyword inline หน้าฟังก์ชัน เป็นเพียงคำแนะนำ compiler ตัดสินใจเอง Syntax inline return_type function_name(p

  12. Pointer to Pointer และ Void Pointer ในภาษา C

    Pointer to Pointer (Double Pointer) ตัวแปรที่เก็บ address ของ pointer อีกตัว Syntax datatype **pointer_name; ตัวอย่าง int a = 10; int *p = &a; int **q = &p; printf(%d, **q); // 10 โปรแกรม Pointer to Pointer #include <stdio.h> main() { int A = 10; int *p = &A; int **q

  13. Pointer to Constant (Pointer to const) ในภาษา C

    Pointer to constant = pointer ที่ชี้ไปยังค่าคงที่ ไม่สามารถแก้ไขค่าผ่าน pointer ได้ Syntax Data_Type const *Pointer_Name; // หรือ const Data_Type *Pointer_Name; ตัวอย่าง: int const *p; // p ชี้ไป int แบบ const โปรแกรมที่ 1: พยายามแก้ค่าผ่าน pointer to const (Error) #include <stdio.h> int ma

  14. Command Line Arguments ในภาษา C: argc และ argv

    Command line arguments = พารามิเตอร์ที่ส่งเข้ามาตอนรันโปรแกรมจาก terminal พารามิเตอร์หลัก argc (argument count) = จำนวน arguments (รวมชื่อโปรแกรม) argv (argument vector) = array ของ string แต่ละ argument ตัวอย่าง ./sample.exe hello how are you argc = 5 argv[0] = sample.exe argv[1] = hello argv[2

  15. Backslash Character Constants (Escape Sequences) ใน C

    Backslash (\) นำหน้าตัวอักษร เพื่อแทน special characters (escape sequences) ตาราง Escape Sequences Characterความหมาย \aAlert (beep) \bBackspace \fForm feed \nNewline (บรรทัดใหม่) \tHorizontal tab \rCarriage return \vVertical tab \\Backslash \ \Single quote \Double quote \?Question mark ? โปร

Total 1447 -คอมพิวเตอร์  FirstPage PreviousPage NextPage LastPage CurrentPage:61/73  20-คอมพิวเตอร์/Page Goto:1 55 56 57 58 59 60 61 62 63 64 65 66 67