Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> การเขียนโปรแกรม C
การเขียนโปรแกรม C
  1. 4 Dimensional Array ใน C/C++

    4 dimensional array คือ array ของ 3D arraysAlgorithmBegin. Declare the variables. Declare the array elements. Take the no of elements as input. Take the elements as input. Print the elements stored in array. End.ตัวอย่าง 4D array:#include<iostream> using namespace std; int main() {   &nbs

  2. Sum of array ใช้ pointer arithmetic ใน C

    ในโปรแกรมนี้ ต้องการหาผลรวมของ array elements ด้วย pointer arithmeticใช้ * แทนค่าที่เก็บอยู่ที่ memory address ที่ address นั้นเก็บในตัวแปร ดังนั้น “int *ptr” หมายถึง ptr เป็นตัวแปรที่มี address และ content ของ address เป็น integer*p หมายถึง pointer variable ใช้ร่วมกับ sum() หาผลรวม arra

  3. Wrapping C/C++ สำหรับ Python ด้วย SWIG

    มีหลายวิธี wrap C/C++ functionality ให้ Python ในส่วนนี้เราจะดูวิธี wrap ด้วย SWIG ตัวเลือกอื่นๆ:Manual wrappingUsing pyrex to wrap C code.CtypesSIPBoost PythonSWIG (Simple Wrapper Interface Generator) wrap C code ให้ภาษาอื่นมากมาย: Perl, Python, PHP, Ruby, Tcl, C#, Common Lisp, Java, Modula-3, OCAM

  4. String tokenisation function ใน C

    ในส่วนนี้เราจะดูวิธี tokenize strings ใน C C มี library function สำหรับนี้ ฟังก์ชัน char *strtok(char *str, const char *delim) breaks string str เป็น series of tokens โดยใช้ delimiter delim.Declaration สำหรับ strtok():char *strtok(char *str, const char *delim)รับ 2 parameters str - เนื้อหาของ string

  5. หา current working directory ใน C/C++

    ในส่วนนี้เราจะดูวิธีหา current working directory ด้วย C หรือ C++ ได้กำหนด flags สำหรับ operating system ปัจจุบันตัวอย่างโค้ด#ifdef WINDOWS #include <direct.h> #define GetCurrentDir _getcwd #else #include <unistd.h> #define GetCurrentDir getcwd #endif #include<iostream> using namesp

  6. Standard Size of character ('a') ใน C/C++ บน Linux

    ใน C++ ขนาดของ character literal เป็น char ใน C ชนิดของ character literal เป็น integer (int) ดังนั้นใน C sizeof(a) เป็น 4 สำหรับ 32-bit architecture และ CHAR_BIT เป็น 8 แต่ sizeof(char) เป็น 1 byte ทั้ง C และ C++ตัวอย่างโค้ด (C)#include<stdio.h> main() {    printf("%d", siz

  7. มี pointer levels ได้กี่ระดับใน C/C++

    จริงๆ ใน C programs one หรือ two static levels of pointers เป็นเรื่องปกติ Triple indirection นادرة แต่ infinite เป็นเรื่องปกติ Infinite pointer indirection สามารถทำได้ด้วย structstruct list { struct list *next; ... } lst; lst->next->next->next->...->nextและด้วยวิธีนี้สามารถ implement

  8. Near, Far และ Huge pointers ใน C

    Near PointerNear pointer ใช้ address ถึง 16 bits ใน section ของ memory ที่ 16-bit enabled เข้าถึงข้อมูลขนาดเล็กประมาณ 64 KB เท่านั้น ซึ่งเป็นข้อเสียหลักFar PointerFar pointer เป็น 32-bit pointer เข้าถึงข้อมูลนอก segment ต้อง allocate sector register เก็บ data address ใน segment และอีก sector registe

  9. Command line arguments ใน C/C++

    สามารถส่งค่าจาก command line ไปยังโปรแกรม C ตอน execute ได้ ค่าเหล่านี้เรียกว่า command line arguments สำคัญเมื่อต้องการควบคุมโปรแกรมจากภายนอกแทน hard codeCommand line arguments จัดการด้วย main() function arguments argc คือจำนวน arguments argv[] คือ pointer array ชี้ไปแต่ละ argumentตัวอย่าง#include

  10. exit() vs _Exit() function ใน C และ C++

    ในส่วนนี้เราจะดูความแตกต่างระหว่าง exit() และ _Exit() ใน C และ C++ ใน C exit() ยุติ calling process โดยไม่ execute โค้ดที่เหลือหลัง exit()ใน C++11 มีฟังก์ชันใหม่ _Exit() ฟีเจอร์คืออะไร? exit() ทำ cleaning ก่อน terminate program เช่น connection termination, buffer flushes ฯลฯ _Exit() ไม่ทำ cleaning อ

  11. C Program หาขนาดไฟล์

    นี่คือ C Program หาขนาดไฟล์AlgorithmBegin    function findfileSize()    Open a file pointer fp in read only mode.    If fp is equals to null then       Print “File not found” and return -1.    Else count the file size.    

  12. Double Pointer (Pointer to Pointer) ใน C

    Pointer ใช้เก็บ address ของ variables ดังนั้น pointer to pointer ตัวแรกเก็บ address ของตัวที่สอง เรียกว่า double pointersAlgorithmBegin    Declare v of the integer datatype.       Initialize v = 76.    Declare a pointer p1 of the integer datatype.    Decl

  13. NULL pointer ใน C

    Null pointer คือ pointer ที่ชี้ไม่ไปที่ไหนการใช้งาน null pointer:a) Initialize pointer variable เมื่อ pointer ยังไม่ได้ assign valid memory addressb) ส่ง null pointer ให้ function argument เมื่อไม่ต้องการส่ง valid memory addressc) ตรวจสอบ null pointer ก่อน access pointer variable เพื่อ error handlin

  14. Void pointer ใน C

    Void pointer ใน C คือ pointer ที่ไม่เกี่ยวข้องกับ data types ใดๆ ชี้ไปที่ data location ใน storage หมายถึง address ของ variables เรียกว่า general purpose pointer ใน C malloc() และ calloc() คืน void * หรือ generic pointersมีข้อจำกัด:1) Pointer arithmetic ไม่ได้กับ void pointer เนื่องจาก concrete size

  15. ทำไม address zero ถูกใช้สำหรับ null pointer ใน C/C++

    Null pointer คือ pointer ที่ชี้ไม่ไปที่ไหนการใช้งาน null pointer:b) Initialize pointer variable เมื่อ pointer ยังไม่ได้ assign valid memory addressb) ส่ง null pointer ให้ function argument เมื่อไม่ต้องการส่ง valid memory addressc) ตรวจสอบ null pointer ก่อน access pointer variable เพื่อ error handlin

  16. Read/Write structure ไปยังไฟล์ด้วย C

    fwrite() และ fread() ใช้เขียน/อ่านไฟล์ใน Cfwrite() syntaxfwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)where ptr - pointer to array of elements to be written size - size in bytes of each element nmemb - number of elements stream - pointer to FILE object for output streamfread() syn

  17. อ่าน/เขียน binary file ใน C/C++

    การเขียน (Writing)เขียน binary file ใน C++ ใช้ write method เขียนจำนวน bytes ที่กำหนดลง stream ตำแหน่ง put pointer ไฟล์จะขยายถ้า put pointer อยู่ท้ายไฟล์ หากอยู่กลางไฟล์ จะ overwriteหากเกิด error ขณะเขียน stream จะอยู่ใน error stateSyntax of write methodostream& write(const char*, int);การอ่าน (

  18. Line Splicing ใน C/C++

    ในส่วนนี้เราจะดู line splicing ใน C หรือ C++ บางครั้งเราใส่ single line comment ด้วย double slash “//” one-line comment จบเมื่อขึ้นบรรทัดใหม่ แต่ถ้าใส่ back slash ท้าย comment line จะเกิดอะไรขึ้น?เมื่อใช้ back slash จะต่อไปบรรทัดถัดไป ดังนั้นหลัง comment line หากมีโค้ดต่อจะถูก ignore ด้ว

  19. Precedence ของ postfix ++ และ prefix ++ ใน C/C++

    ที่นี่เราจะดู precedence ของ postfix++ และ prefix++ ใน C หรือ C++ Precedence ของ prefix ++ หรือ -- สูงกว่า dereference operator * และ postfix ++ หรือ -- มี priority สูงกว่าทั้ง prefix ++ และ dereference operator *เมื่อ ptr เป็น pointer *ptr++ หมายถึง *(ptr++) และ ++*ptr หมายถึง ++(*ptr)ตัวอย่าง#incl

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