Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> การเขียนโปรแกรม C
การเขียนโปรแกรม C
  1. Function-level static variables ถูก initialize เมื่อไหร่ใน C/C++

    Static variables กำหนดด้วย static keyword เป็น variables ที่คงอยู่ใน memory ตลอดที่โปรแกรมรัน lifetime คือ entire program run ต่างจาก automatic variables ที่อยู่ใน memory เฉพาะตอนฟังก์ชันรัน และถูกทำลายตอนฟังก์ชันจบFunction-level static variables ถูกสร้างและ initialize ครั้งแรกที่ถูกใช้งาน แม้ memor

  2. ความแตกต่างระหว่าง pointer และ array ใน C

    รายละเอียดเกี่ยวกับ pointer และ array ที่แสดงความต่าง:PointerPointer คือ variable ที่เก็บ address ของ variable อื่น เมื่อจัดสรร memory ให้ variable pointer จะชี้ไปที่ memory address ของ variable ใช้ unary operator ( * ) ประกาศ pointer variableSyntax ของ pointer declaration:datatype *variable_name;da

  3. วิธี dynamically allocate 2D array ใน C

    2D array จัดสรร dynamically ใน C ได้ด้วย single pointer หมายถึง memory block ขนาด row*column*dataTypeSize จัดสรรด้วย malloc และ pointer arithmetic ใช้เข้าถึง matrix elements โปรแกรมสาธิต: ตัวอย่าง #include <stdio.h> #include <stdlib.h> int main() { int row = 2, col = 3; int *arr = (in

  4. วิธีส่ง 2D array เป็น parameter ใน C

    2-D array ส่งเป็น parameter ให้ function ใน C ได้ง่าย โปรแกรมสาธิตเมื่อ array dimensions ระบุ globally:ตัวอย่าง#include <stdio.h> const int R = 4; const int C = 3; void func(int a[R][C]) {    int i, j;    for (i = 0; i < R; i++)    for (j = 0; j < C; j++) &n

  5. Data Types ที่ไม่สามารถสร้าง array ได้ใน C

    Array สร้างได้จาก data types ทั้งหมด: int, char, float, double ฯลฯ แต่สร้าง array ด้วย void data type ไม่ได้ จะแสดง errorโปรแกรมสาธิต:ตัวอย่าง#include <stdio.h> #include <stdlib.h> int main() {    void arr1[4];    printf("A void array");    return

  6. วิธีเขียน long strings หลายบรรทัดใน C/C++

    Long strings เขียนหลายบรรทัดได้โดยใช้ two double quotes ( “ “ ) ตัด string ที่จุดไหนก็ได้กลาง stringโปรแกรมสาธิตใน C:ตัวอย่าง#include <stdio.h> int main() {    char *str = "This is the method "                "to writ

  7. Accessing array out of bounds ใน C/C++

    ในภาษาเช่น Java อาจเกิด exception java.lang.ArrayIndexOutOfBoundsException ถ้า access array out of bounds แต่ใน C ไม่มี functionality นี้ undefined behaviour อาจเกิดขึ้นโปรแกรมสาธิตใน C:ตัวอย่าง#include <stdio.h> int main() {    int arr[] = {1,2,3,4,5};    printf("The

  8. ทำไม strncpy ถือว่าไม่ปลอดภัยใน C/C++

    ฟังก์ชัน strncpy() ใช้ copy จำนวน characters ที่ระบุจาก source ไป destinationSyntax ของ strncpy():char *strncpy( char *destination, char *source, size_t n);destination คือ pointer ไป destination array source คือ string ที่จะ copy n คือ max number of characters ที่ copy จาก sourcestrncpy() ไม่ปลอดภัย

  9. ทำไม array index เริ่มจากศูนย์ใน C/C++

    Array arr[i] ถูก interpret เป็น *(arr+i) arr หมายถึง address ของ first array element หรือ 0 index element ดังนั้น *(arr+i) หมายถึง element ที่ห่างจาก first element i positions ดังนั้น array index เริ่มจาก 0 เนื่องจาก i เริ่มต้นเป็น 0 หมายถึง first elementโปรแกรมสาธิตใน C++:ตัวอย่าง#include <iostr

  10. C Program พิมพ์ “Even” หรือ “Odd” โดยไม่ใช้ Conditional statement

    ในส่วนนี้เราจะดูวิธีตรวจสอบเลขคู่หรือคี่โดยไม่ใช้ conditional statements (<, <=, !=, >, >=, ==)ปกติตรวจสอบง่ายด้วย conditional statements หาร 2 ดู remainder เป็น 0 หรือไม่ หรือ AND กับ 1 ถ้า 0 เป็น even ไม่ใช่ oddที่นี่ไม่ใช้ conditional statements จะแสดง 2 วิธีMethod 1สร้าง array of str

  11. Program พิมพ์ตัวเลข 1 ถึง 100 โดยไม่ใช้ loop

    ที่นี่เราจะดูวิธีเขียน C program พิมพ์ตัวเลข 1 ถึง 100 โดยไม่ใช้ loops ใดๆแก้ปัญหาได้ด้วย recursion สร้างฟังก์ชันที่เรียกตัวเอง recursively recursive function มี 2 ส่วน: base case และ recursive call ในฟังก์ชันนี้ base case คือ argument n มากกว่า 1 จนกว่าจะถึง 1 ฟังก์ชันจะถูกเรียกซ้ำ ตอนท้ายพิมพ์ค่า

  12. C Program พิมพ์ “Hello World!” โดยไม่ใช้ semicolon

    ลองดูวิธีเขียน C program พิมพ์ข้อความ “Hello World” โดยไม่ใช้ semicolon ใดๆปกติเขียน printf(“Hello World”); ใน main() แต่มี semicolon ท้ายบรรทัด เพื่อหลีกเลี่ยง semicolon ใช้ trick ใส่ printf() statement ใน if condition เนื่องจาก printf() คืนความยาวของข้อความ (non-zero) if st

  13. เขียน C program ที่ไม่จบการทำงานเมื่อกด Ctrl+C

    ในส่วนนี้เราจะดูวิธีเขียนโปรแกรมใน C ที่ไม่สามารถ terminate ด้วย Ctrl + C ได้Ctrl + C สร้าง keyboard interrupt หยุด execution ของ current process ที่นี่เมื่อกด Ctrl + C จะพิมพ์ข้อความแล้วทำงานต่อ ใช้ signal handling technique ใน C Ctrl + C สร้าง SIGINT signal มี signals อื่นๆ และ functions:SignalDes

  14. C program พิมพ์ตัวอักษรโดยไม่ใช้ format specifiers

    ในบทความนี้เราจะดูวิธีพิมพ์ตัวอักษรโดยไม่ใช้ format specifiers ใดๆ format specifiers ใน C คือ %d, %f, %c ใช้พิมพ์ตัวอักษรและตัวเลขด้วย printf()ที่นี่เราจะดูอีกวิธีพิมพ์ตัวอักษรโดยไม่ใช้ %c format specifier ทำได้โดยใส่ ASCII values โดยตรงในรูป hexadecimalตัวอย่างโค้ด#include <stdio.h> main () {

  15. C Program ตรวจสอบความถูกต้องของ IP address

    ในโปรแกรมนี้เราจะดูวิธี validate IP address ด้วย C IPv4 addresses แสดงใน dot-decimal notation มี 4 ตัวเลขทศนิยม (0-255) คั่นด้วย 3 จุดตัวอย่าง IP ถูกต้อง: 192.168.4.1ขั้นตอน validate IP address:Tokenize string (IP address) ด้วย dot “.” delimiterถ้า sub strings มี non-numeric character คื

  16. C program เขียนภาพใน PGM format

    PGM คือ Portable Gray Map ถ้าอยากเก็บ 2d array ใน C เป็น images ใน PNG, JPEG หรือ format อื่น ต้อง encode ข้อมูลใน format ที่กำหนดก่อนเขียนไฟล์Netpbm format ให้ easy และ portable solution Netpbm เป็น open source graphics programs ใช้ใน Linux/Unix ทำงานบน Windows ได้ด้วยแต่ละไฟล์เริ่มด้วย two-byte ma

  17. C program พิมพ์นาฬิกาดิจิตอลพร้อมเวลาปัจจุบัน

    ในส่วนนี้เราจะดูวิธีทำ digital clock ด้วย C ทำงานกับเวลาใช้ time.h header file มี function signatures สำหรับจัดการ date และ timeสี่ components สำคัญของ time.h:size_t unsigned integral type ผลลัพธ์ของ sizeof()clock_t ใช้เก็บ processor timetime_t ใช้เก็บ calendar timestruct tm structure ช่วยเก็บ date

  18. C program พิมพ์ hollow pyramid และ diamond pattern

    ที่นี่เราจะดูวิธี generate hollow pyramid และ diamond patterns ด้วย C สร้าง solid Pyramid patterns ได้ง่าย เพื่อทำให้ hollow ต้องเพิ่ม tricks นิดหน่อยHollow Pyramidสำหรับ pyramid บรรทัดแรกพิมพ์ดาว 1 ดวง บรรทัดสุดท้ายพิมพ์ n ดาว บรรทัดอื่นพิมพ์ดาว 2 ดวงที่ต้นและท้ายบรรทัด มี blank spaces ระหว่างดาวสอ

  19. C Program แสดง hostname และ IP address

    ในส่วนนี้เราจะดูวิธีดู Host name และ IP address ของ local system แบบง่าย เขียน C program หา host name และ IPฟังก์ชันต่อไปนี้ใช้งาน ฟังก์ชันแต่ละตัวมีหน้าที่ต่างกัน:FunctionDescriptiongethostname()หา standard host name สำหรับ local computergethostbyname()หา host information จาก host name ใน host data

  20. ทำไม sizeof struct ไม่เท่ากับผลรวม sizeof ของแต่ละ member ใน C/C++

    ขนาดของ struct type element ที่ sizeof() คืน ไม่เสมอไปเท่ากับขนาดของแต่ละ member บางครั้ง compiler เพิ่ม padding เพื่อหลีกเลี่ยง alignment issues ทำให้ขนาดเปลี่ยน Padding เพิ่มเมื่อ structure member ตามด้วย member ที่มีขนาดใหญ่กว่า หรือที่ท้ายของ structure Compiler แต่ละตัวมี alignment constraints ต

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