Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> การเขียนโปรแกรม C
การเขียนโปรแกรม C
  1. เมื่อไหร่ควรใช้ forward declaration ใน C/C++

    ใน C++ forward declaration ให้โค้ดหลัง declaration รู้ว่ามี classes ชื่อ Person นี้ทำให้ compiler พอใจเมื่อเห็นชื่อเหล่านี้ถูกใช้ ต่อมา linker จะหา definition ของ classesตัวอย่างโค้ดClass Person; void myFunc(Person p1) { // ... } Class Person { // Class definition here };ในกรณีนี้เมื่อ compiler พบ m

  2. หา current C หรือ C++ standard documents ที่ไหน

    ในโพสต์นี้คุณจะได้รายละเอียดว่าซื้อและดู free drafts ของ current และ past C/C++ standards ได้ที่ไหนC DocumentsC11:198 CHF (https://www.iso.org/standard/57853.html)Publicly at (https://www.open-std.org/JTC1/SC22/WG14/www/docs/n1570.pdf)Wikipedia Link (https://en.wikipedia.org/wiki/C11_(C_standard_rev

  3. C Program แปลงตัวเลขเป็น string

    ในส่วนนี้เราจะดูวิธีแปลงตัวเลข (integer หรือ float หรือ numeric type อื่น) เป็น stringตรรกะง่าย ใช้ sprintf() function พิมพ์ค่าหรือบรรทัดเข้า string ไม่ใช่ console ความต่างกับ printf() คืออาร์กิวเมนต์แรกคือ string buffer ที่ต้องการเก็บข้อมูลInput: User will put some numeric value say 42.26 Output: T

  4. C program copy string โดยไม่ใช้ strcpy()

    ในส่วนนี้เราจะดูวิธี copy string ไปอีก string โดยไม่ใช้ strcpy() function แก้ปัญหาได้ด้วยการเขียนฟังก์ชันเองเหมือน strcpy() แต่ที่นี่ใช้ trick ใช้ library function อื่น copy stringตรรกะง่าย ใช้ sprintf() function พิมพ์ค่าเข้า string ไม่ใช่ console ความต่างกับ printf() คืออาร์กิวเมนต์แรกคือ string bu

  5. C program พิมพ์ string โดยไม่ใช้ quotation marks ในโปรแกรม

    นี่คือ tricky problem โปรแกรมนี้จะพิมพ์ string โดยไม่มี quotation marks ใดๆใช้ macro function กำหนด macro function เช่น#define getString(x) #xgetString() เป็น macro function คืน x โดยแปลงเป็น string # นำหน้า x หมายถึง function จะแปลง x เป็น stringInput: Take one string without quote Output: Print th

  6. C/C++ Macro สำหรับ string concatenation

    ในโปรแกรมนี้เราจะดูว่า macros ใช้ concatenate สอง strings ยังไง สร้างสตริงสองตัวขึ้นไปใน macro แล้วเขียนต่อกันเพื่อรวมเป็น concatenated string syntax เหมือนด้านล่าง:#define STR1 "str1" #define STR2 " str2" #define STR3 STR1 STR2 //it will concatenate str1 and str2Input: Take two

  7. แปลง floating point number เป็น string ใน C

    ในส่วนนี้เราจะดูวิธีแปลงตัวเลข (integer หรือ float หรือ numeric type อื่น) เป็น stringตรรกะง่าย ใช้ sprintf() function พิมพ์ค่าหรือบรรทัดเข้า string ไม่ใช่ console ความต่างกับ printf() คืออาร์กิวเมนต์แรกคือ string buffer ที่ต้องการเก็บข้อมูลInput: User will put some numeric value say 42.26 Output: T

  8. วิธี reverse string in place ใน C หรือ C++

    ในส่วนนี้เราจะดูวิธี reverse string in place ไม่ใช้ memory อื่น ใน C++ ใช้ std::string แต่ใน C ต้องใช้ character array โปรแกรมนี้ใช้ character array รับ string แล้ว reverseInput: A string “This is a string” Output: The reversed string “gnirts a si sihT”Algorithmreverse_string

  9. วิธีตรวจจับ integer overflow ใน C/C++

    วิธีปลอดภัยที่สุดคือตรวจสอบ overflow ก่อนที่จะเกิด มี hacky ways บางอย่างเช่น ถ้า aimed at detecting overflow ใน unsigned int addition สามารถตรวจสอบได้ว่า result น้อยกว่าค่าที่บวกกันหรือไม่ เช่น:ตัวอย่างโค้ดunsigned int x, y; unsigned int value = x + y; bool overflow = value < x; // Alternatively

  10. Segmentation fault ใน C/C++ คืออะไร

    Segmentation fault เกิดเมื่อโปรแกรมพยายาม access พื้นที่หน่วยความจำที่ไม่ได้รับอนุญาต กล่าวคือ พยายาม access memory ที่เกินขีดจำกัดที่ OS จัดสรรให้Segmentation faults เกิดจาก pointers ที่:Used to being properly initialized. (หมายถึง: ไม่ได้ initialize อย่างถูกต้อง)Used after the memory they point to

  11. How to return a local array from a C/C++ function

    นี่คือ C++ program return a local array from a functionAlgorithmBegin We can use dynamically allocated array to return a local array from function Array(). Print the elements of the array. Endตัวอย่างโค้ด#include <iostream> using namespace std; int* Array() { int* a = new int[100]; a[0] = 7; a

  12. Initialization of a multidimensional arrays ใน C/C++

    ใน multidimensional array array ควรมี dimension มากกว่า 1 ภาพแสดง memory allocation strategy สำหรับ dimension 3 x 3 x 3นี่คือ C++ program initialize multidimensional arrayAlgorithmBegin    Initialize the elements of a multidimensional array.    Print the size of the array.  

  13. Multidimensional Arrays ใน C / C++

    ใน C/C++ Multidimensional array นิยามง่ายๆ คือ array of arrays ใน multidimensional arrays ข้อมูลเก็บในรูปตาราง (row major order) ภาพต่อไปแสดง memory allocation strategy สำหรับ multidimensional array dimension 3 x 3 x 3AlgorithmBegin    Declare dimension of the array.    Dynamic a

  14. หาขนาด array ใน C/C++ โดยไม่ใช้ sizeof

    ในโปรแกรมนี้ เราหา Find size of array ใน C/C++ โดยไม่ใช้ sizeofAlgorithmBegin Initialize the elements of the array. &a => This is the pointer to array which points at the same memory address as a. &a + 1 => It points at the address after the end of the array. *(a+1) => Dereferencing

  15. Flexible Array Members ใน structure ใน C

    Flexible Array members ใน structure ใน C หมายถึงการประกาศ array โดยไม่ระบุ dimension ภายใน structure และขนาดจะยืดหยุ่นได้ Flexible array member ต้องเป็น last member ของ classตัวอย่าง:ตัวอย่าง#include #include #include //structure of type employee and must contain at least one more named member in ad

  16. Short hand array notation ใน C/C++

    ถ้ามีค่าซ้ำใน C ใช้ shorthand array notation กำหนด array นั้นตัวอย่าง:ตัวอย่างโค้ด#include <stdio.h> int main() { int array[10] = {[0 ... 3]7, [4 ... 5]6,[6 ... 9]2}; for (int i = 0; i < 10; i++) printf("%d ", array[i]); return 0; }ผลลัพธ์7 7 7 7 6 6 2 2 2 2ในโปรแกรมนี้:int array[

  17. ทำไม C ถือ array parameters ว่าเป็น pointers

    C ถือ array parameter เป็น pointers เพราะ less time consuming และ more efficient ถ้าส่ง address ของแต่ละ element ไปเป็น argument จะ time consuming มากกว่า ดีกว่าส่ง base address ของ first element:void fun(int a[]) { … } void fun(int *a) { //more efficient. ….. }ตัวอย่างโค้ดใน C:#includ

  18. An Uncommon representation of array elements in C/C++

    นี่คือ C++ program แสดง uncommon representation ของ array elements#include<iostream> using namespace std; int main() { int array[5] = {7,7,7, 6, 6}; for (int i = 0; i < 5; i++) cout<<*(array+i); return 0; }ผลลัพธ์7 7 7 6 6

  19. Pointer vs Array ใน C

    Pointers และ array ใน C มักถูก treat เหมือนกัน แต่มีความต่างบางอย่าง:&operator:&pointer = คืน address ของ pointer&array = คืน address ของ first elementsizeof operator:sizeof(array) = คืน total memory ของ all elementssizeof(pointer) = คืน memory ของ pointer variable เองArray variable ไม่ส

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

    เนื่องจาก Array index เริ่มที่ 0 a[i] สามารถ implement เป็น *(a + i) ได้ถ้า Array index เริ่มที่ 1 a[i] จะเป็น *(a+i-1) ซึ่งใช้เวลา compile มากกว่าและส่งผลต่อ performanceดังนั้น เริ่ม index ของ array จาก 0 ดีกว่าตัวอย่างโปรแกรม array เรียบง่าย:ตัวอย่างโค้ดint main() {    int array[5] = {7,

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