Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> การเขียนโปรแกรม C
การเขียนโปรแกรม C
  1. Pre-increment และ Post-increment ใน C/C++

    Pre-increment (++x) เพิ่มค่าก่อน แล้วคืนค่าใหม่ Post-increment (x++) คืนค่าเก่าก่อน แล้วค่อยเพิ่ม ตัวอย่าง #include <iostream> using namespace std; int main() { int x = 10, y = 10, z; z = ++x; // z = 11, x = 11 cout << Z: << z << endl; z = y++; // z = 10,

  2. การจัดเก็บค่า Integer และ Character ในหน่วยความจำ C

    ใน C ตัวอักษรก็เก็บเป็น Integer (ASCII) เช่นกัน การเก็บค่าที่เกินขนาด char จะเกิด Overflow และเก็บเฉพาะ 8-bit ล่าง ตัวอย่าง เก็บ 8 bit ล่าง = 126 printf(The value of x is: %d\n, x); printf(The value of y is: %d, y); return 0; } ผลลัพธ์ The value of x is: 14 The value of y is: 126 คำอ

  3. การกำหนดค่าเริ่มต้น 0 หรือ 1 แบบ Implicit ใน C

    ตัวแปร Global และ Static ที่ไม่ได้กำหนดค่าเริ่มต้น จะได้ค่า 0 อัตโนมัติ ตัวแปร Local (Auto) จะมีค่า Garbage ตัวอย่าง Global/Static #include <stdio.h> int x, y, array[3]; // Global - initialized to 0 int main(int i) { // i = 1 (argc) printf(x = %d, y = %d\n\n, x, y); for (int index =

  4. ที่อยู่ของฟังก์ชันใน C/C++

    ฟังก์ชันเก็บอยู่ในหน่วยความจำเช่นเดียวกับตัวแปร สามารถดึงที่อยู่ได้โดยใช้ชื่อฟังก์ชันโดยไม่ใส่วงเล็บ ตัวอย่าง #include <stdio.h> void my_function() { printf(Hello World); } int main() { printf(The address of my_function is: %p\n, my_function); printf(The address of main is: %p\n

  5. การกำหนดหลายตัวอักษรในตัวแปร int ใน C

    ตัวอักษรเก็บในรูปค่า ASCII หากใส่หลายตัวอักษรใน Single Quote เช่น AA จะถูกรวมเป็นค่าจำนวนเต็มหนึ่งค่า ตัวอย่าง #include <stdio.h> int main() { printf(%d\n, A); // 65 printf(%d\n, AA); // 16705 printf(%d\n, ABC); // 4276803 return 0; } ผลลัพธ์ 65 16705 4276803 คำอธิบาย A

  6. ฟังก์ชัน mbtowc ในภาษา C

    mbtowc() แปลง Multibyte Sequence เป็น Wide Character ไวยากรณ์ int mbtowc(wchar_t *pwc, const char *str, size_t n) พารามิเตอร์ pwc: ตัวชี้ wchar_t สำหรับเก็บผลลัพธ์ str: ตัวชี้ไปยังไบต์แรกของ Multibyte Character n: จำนวนไบต์สูงสุดที่ตรวจสอบ ค่าคืน str ไม่เป็น NULL: จำนวนไบต์ที่ใช้, 0 ถ้าเป็น NU

  7. Variable Length Argument ในภาษา C

    C รองรับฟังก์ชันที่รับพารามิเตอร์จำนวนไม่คงที่ (Variadic Function) โดยใช้ stdarg.h ขั้นตอน ประกาศฟังก์ชันพารามิเตอร์สุดท้ายเป็น ... และก่อนหน้าเป็น int แทนจำนวนอาร์กิวเมนต์ สร้างตัวแปร va_list ใช้ va_start เริ่มต้น ใช้ va_arg ดึงค่าแต่ละอาร์กิวเมนต์ ใช้ va_end ทำความสะอาด ตัวอย่าง: หาค่าเฉลี่ย #

  8. เขียนฟังก์ชัน C บรรทัดเดียวสำหรับปัดเศษจำนวนจริง

    ฟังก์ชันปัดเศษบรรทัดเดียว: ถ้าบวก ให้บวก 0.5 ถ้าลบ ให้ลบ 0.5 แล้วแปลงเป็น int ตัวอย่าง #include <stdio.h> int my_round(float number) { return (int)(number < 0 ? number - 0.5 : number + 0.5); } int main() { printf(Rounding of (2.48): %d\n, my_round(2.48)); printf(Rounding of (

  9. วิธีเปลี่ยนพฤติกรรม printf() ใน main() ด้วย Macro

    ใช้ #define macro ภายในฟังก์ชันเพื่อเปลี่ยนพฤติกรรมของ printf() ทั้งโปรแกรม ตัวอย่าง #include <stdio.h> void changePrintf() { #define printf(x, y) printf(x, 50); } int main() { int x = 40; changePrintf(); printf(%d\n, x); // พิมพ์ 50 แทน 40 x = 60; printf(%d, x); /

  10. วิธีวัดเวลาที่ฟังก์ชันใช้ในการทำงานใน C

    ใช้ฟังก์ชัน clock() จาก time.h วัดเวลา CPU ที่ใช้ วิธีการ บันทึกเวลาเริ่มต้นด้วย clock() รันงานที่ต้องการวัด บันทึกเวลาสิ้นสุด และลบเพื่อหาความต่าง หารด้วย CLOCKS_PER_SEC เพื่อได้วินาที ตัวอย่าง #include <stdio.h> #include <time.h> void take_enter() { printf(Press enter to stop

  11. setjmp() และ longjmp() ในภาษา C

    setjmp() และ longjmp() ใน setjmp.h ใช้สำหรับ Exception Handling แบบต่ำระดับ ไวยากรณ์ setjmp(jmp_buf buf) // บันทึกตำแหน่งปัจจุบัน คืนค่า 0 longjmp(jmp_buf buf, int val) // กระโดดกลับไปตำแหน่งที่ buf บันทึกไว้ คืนค่า val setjmp คล้าย try block, longjmp คล้าย throw statement ตัวอย่าง: พิมพ์เลข 10

  12. ฟังก์ชัน trunc(), truncf(), truncl() ในภาษา C

    ฟังก์ชันเหล่านี้ใช้ตัดส่วนทศนิยม (Truncate) ให้เหลือส่วนจำนวนเต็มเท่านั้น trunc() - สำหรับ double double trunc(double argument) ตัวอย่าง double x = 53.26, y = 75.86; printf(%lf\n, trunc(x)); // 53.000000 printf(%lf\n, trunc(y)); // 75.000000 truncf() - สำหรับ float float truncf(float argument) ตัว

  13. Modulus ของจำนวนจริง (float/double) ใน C

    ใช้ฟังก์ชัน remainder() จาก math.h สำหรับหาเศษของการหารจำนวนจริง สูตร remainder(x, y) = x - rquote * y โดย rquote = ค่า x/y ที่ปัดเป็นจำนวนเต็มใกล้เคียงที่สุด ตัวอย่าง #include <stdio.h> #include <math.h> int main() { double x = 14.5, y = 4.1; double res = remainder(x, y);

  14. โปรแกรมแสดง Hostname และ IP Address ใน C

    ใช้ฟังก์ชันเครือข่ายเพื่อดึงชื่อโฮสต์และ IP Address ของเครื่องท้องถิ่น ฟังก์ชันหลัก ฟังก์ชันคำอธิบาย gethostname()หาชื่อโฮสต์มาตรฐานของคอมพิวเตอร์ท้องถิ่น gethostbyname()หาข้อมูลโฮสต์จากฐานข้อมูลโดยใช้ชื่อโฮสต์ inet_ntoa()แปลง IPv4 Address เป็นสตริงรูปแบบ Dotted Decimal ตัวอย่าง #include <std

  15. พิมพ์ตัวเลข 100 ครั้งโดยไม่ใช้ลูป Recursion และ Macro ใน C

    ใช้ setjmp() และ longjmp() จาก setjmp.h เพื่อสร้างการทำงานแบบลูปโดยไม่ใช้คำสั่งลูป ตัวอย่าง #include <stdio.h> #include <setjmp.h> jmp_buf buf; int main() { int x = 1; setjmp(buf); printf(5); x++; if (x <= 100) longjmp(buf, 1); return 0; } ผลลัพธ์ 5

  16. วิธีหาขนาดของอาร์เรย์ int[] ใน C/C++

    ขนาดของอาร์เรย์ int[] คือจำนวนสมาชิกภายใน ใช้ sizeof() หาขนาดหน่วยความจำทั้งหมด แล้วหารด้วยขนาดของแต่ละสมาชิก ตัวอย่าง #include <iostream> using namespace std; int main() { int data[] = {11, 22, 33, 44, 55, 66, 77, 88, 99, 91, 82, 73, 64}; cout << Memory occupied by data[]: &l

  17. แปลง int เป็นอักขระ ASCII ใน C/C++

    ใน C/C++ ค่าตัวอักษรเก็บในรูป ASCII เพื่อแปลง int เป็น ASCII ให้บวกค่า ASCII ของ 0 เข้าไป ตัวอย่าง #include <stdio.h> int intToAscii(int number) { return 0 + number; } int main() { printf(The ASCII of 5 is %d\n, intToAscii(5)); printf(The ASCII of 8 is %d\n, intToAscii(8));

  18. การใช้ %p ใน printf ในภาษา C

    %p เป็น Format Specifier สำหรับพิมพ์ค่าพอยน์เตอร์ (ที่อยู่หน่วยความจำ) ตัวอย่าง #include <stdio.h> int main() { int x = 50; int *ptr = &x; printf(The address is: %p, the value is %d, ptr, *ptr); return 0; } ผลลัพธ์ The address is: 000000000022FE44, the value is 50

  19. ความแตกต่างระหว่าง %p และ %x ใน C/C++

    %p ใช้พิมพ์ค่าพอยน์เตอร์ %x ใช้พิมพ์ค่า Hexadecimal แม้พอยน์เตอร์จะพิมพ์ได้ด้วย %u หรือ %x แต่ %p จะพิมพ์ Leading Zeros (ศูนย์นำหน้า) ส่วน %x จะไม่พิมพ์ ตัวอย่าง #include <stdio.h> int main() { int x = 59; printf(Value using %%p: %p\n, x); printf(Value using %%x: %x\n, x); re

  20. เปรียบเทียบ C qsort() และ C++ sort()

    C มี qsort() สำหรับเรียงลำดับอาร์เรย์ ในขณะที่ C++ มี sort() ใน STL qsort() ใน C void qsort(void *base, size_t num, size_t size, int (*comparator)(const void*, const void*)); sort() ใน C++ void sort(T first, T last, Compare c); ตารางเปรียบเทียบ qsort() (C)sort() (C++) ใช้อัลกอริทึม Quicksortใช้

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