Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> การเขียนโปรแกรม C
การเขียนโปรแกรม C
  1. ฟังก์ชัน ispunct() ในภาษา C

    ฟังก์ชัน ispunct() ใช้สำหรับตรวจสอบว่าตัวอักษรที่ส่งเข้ามาเป็นเครื่องหมายวรรคตอน (punctuation) หรือไม่ ฟังก์ชันนี้คืนค่า 0 หากไม่ใช่เครื่องหมายวรรคตอน มิฉะนั้นจะคืนค่าที่ไม่ใช่ 0 (non-zero) ไวยากรณ์ int ispunct(int character); ตัวอย่างการใช้งาน #include <stdio.h> #include <ctype.h> i

  2. การใช้งาน fflush(stdin) ในภาษา C

    ฟังก์ชัน fflush(stdin) ใช้สำหรับล้างบัฟเฟอร์เอาต์พุตของสตรีม ฟังก์ชันนี้คืนค่า 0 หากสำเร็จ มิฉะนั้นจะคืนค่า EOF และตั้งค่าตัวบ่งชี้ข้อผิดพลาด feof ไวยากรณ์ int fflush(FILE *stream); ตัวอย่างการใช้งาน #include <stdio.h> int main() { char s[20] = Helloworld; printf(The string : %s, s

  3. ฟังก์ชัน strdup() และ strndup() ใน C/C++

    ฟังก์ชัน strdup() ฟังก์ชัน strdup() ใช้สำหรับทำซ้ำสตริง (duplicate a string) ฟังก์ชันนี้คืนค่าเป็นพอยน์เตอร์ที่ชี้ไปยังสตริงใหม่ที่ลงท้ายด้วย null character ไวยากรณ์ char *strdup(const char *string); ตัวอย่างการใช้งาน strdup() #include <stdio.h> #include <string.h> int main() {

  4. ฟังก์ชัน rand() และ srand() ในภาษา C

    ฟังก์ชัน rand() ฟังก์ชัน rand() ใช้สำหรับสร้างเลขสุ่มแบบปลอม (pseudo-random number) ฟังก์ชันนี้คืนค่าเป็นจำนวนเต็ม โดยมีช่วงค่าตั้งแต่ 0 ถึง RAND_MAX ซึ่งโดยทั่วไปจะมีค่าเท่ากับ 32767 ไวยากรณ์ int rand(void); ตัวอย่างการใช้งาน rand() #include <stdio.h> #include <stdlib.h> int main()

  5. ฟังก์ชัน isgreater() ใน C/C++

    ฟังก์ชัน isgreater() ใช้สำหรับตรวจสอบว่าอาร์กิวเมนต์ตัวแรกมีค่ามากกว่าตัวที่สองหรือไม่ ฟังก์ชันนี้ประกาศอยู่ในไฟล์เฮดเดอร์ math.h ในภาษา C ฟังก์ชันจะคืนค่า true หากเงื่อนไขเป็นจริง มิฉะนั้นจะคืนค่า false ไวยากรณ์ (Syntax) bool isgreater(value1, value2); พารามิเตอร์ value1 – อาร์กิวเมนต์ตัวแรกที่

  6. modf() ใน C/C++

    modf() แยกส่วนจำนวนเต็มและทศนิยมของ floating point number ประกาศใน math.h คืนค่าส่วนทศนิยมไวยากรณ์double modf(double value, double *integral_pointer);value: ตัวเลขที่จะแยก, integral_pointer: pointer เก็บส่วนจำนวนเต็มตัวอย่าง#include <stdio.h> #include <math.h> int main() { double val

  7. Pointer Arithmetic ใน C/C++

    Pointer arithmetic ใช้ทำ arithmetic บน pointer: addition, subtraction, increment, decrementบน 32-bit machine: increment/decrement เปลี่ยน address 2 bytes, add/subtract เปลี่ยน 2*numberบน 64-bit machine: increment/decrement เปลี่ยน 4 bytes, add/subtract เปลี่ยน 4*numberตัวอย่าง#include <stdio.h>

  8. ชนิดข้อมูล size_t ใน C

    size_t เป็น unsigned integral type ใช้แทนขนาดของวัตถุในหน่วย bytes คืนค่าจาก sizeof operator ใช้สำหรับ array indexing และ counting ไม่มีค่าลบReturn type ของ strcspn, strlen เป็น size_tไวยากรณ์const size_t var_name;ตัวอย่าง#include <stdio.h> #include <stddef.h> #include <stdint.h> i

  9. main() ควรคืนค่าอะไรใน C/C++

    ค่าที่ main() คืนบ่งบอกสถานะการจบโปรแกรม: 0 = ปกติ (success) non-zero = error/abnormalใน C++ main() สามารถไม่เขียน return ได้ จะคืน 0 เริ่มต้นไวยากรณ์int main() { ... return 0; }ตัวอย่าง#include <stdio.h> int main() { int a = 10; char b = S; float c = 2.88; a = a + b; /

  10. fgets() และ gets() ใน C

    fgets()อ่านสตริงจนกว่าจะเจอ newline ตรวจสอบ array bound ปลอดภัยกว่า gets()ไวยากรณ์: char *fgets(char *string, int value, FILE *stream)string: pointer array char, value: จำนวนตัวอักษรสูงสุด, stream: file pointer (เช่น stdin)ตัวอย่าง fgets()#include <stdio.h> #define FUNC 8 int main() { char

  11. เปรียบเทียบ malloc() และ new() ใน C/C++

    malloc()จัดสรรหน่วยความจำขนาดที่ขอ คืน pointer ไป byte แรก ไม่ initialize ค่า คืน NULL ถ้าล้มเหลวไวยากรณ์: pointer_name = (cast-type*) malloc(size);ตัวอย่าง malloc()#include <stdio.h> #include <stdlib.h> int main() { int n = 4, i, *p, s = 0; p = (int*) malloc(n * sizeof(int));

  12. ขนาดของ Void Pointer ใน C/C++

    ขนาดของ void pointer ขึ้นอยู่กับระบบ (system architecture)16-bit system: 2 bytes32-bit system: 4 bytes64-bit system: 8 bytesตัวอย่างหาขนาด void pointer#include <stdio.h> int main() { void *ptr; printf(The size of pointer value: %d, sizeof(ptr)); return 0; }ผลลัพธ์ (บนระบบ 64-bit)Th

  13. isblank() ใน C/C++

    isblank() ตรวจสอบ character (char) ว่าเป็น blank (space หรือ tab \t) หรือไม่ ประกาศใน ctype.h (C) หรือ cctype (C++)ไวยากรณ์int isblank(int char);ตัวอย่าง#include <ctype.h> #include <iostream> using namespace std; int main() { string s = The space between words. ; int i = 0, count

  14. ฟังก์ชัน iswblank() ใน C/C++

    iswblank() ตรวจสอบ wide character ว่าเป็น blank (space หรือ tab \t) หรือไม่ ประกาศใน ctype.h (C) หรือ cctype (C++)ไวยากรณ์int iswblank(wint_t char);ตัวอย่าง#include <ctype.h> #include <iostream> using namespace std; int main() { wchar_t s[] = LThe space between words.; int i = 0,

  15. ฟังก์ชัน iswpunct() ใน C/C++

    iswpunct() ตรวจสอบ wide character ว่าเป็น punctuation (เครื่องหมายวรรคตอน) หรือไม่ คืนค่า non-zero ถ้าใช่ 0 ถ้าไม่ใช่ ประกาศใน cwctype (C++) หรือ wctype.h (C)ไวยากรณ์int iswpunct(wint_t character);ตัวอย่าง#include <cwctype> #include <stdio.h> using namespace std; int main() { wint_t

  16. strcspn() ใน C

    strcspn() นับจำนวนตัวอักษรใน string1 ก่อนเจอตัวอักษรที่ตรงกับ string2 ใดๆ ประกาศใน string.h คืนค่าจำนวนตัวอักษรก่อน match แรกไวยากรณ์size_t strcspn(const char *string1, const char *string2)string1: สตริงที่สแกน, string2: สตริงที่มีชุดตัวอักษรให้ค้นหาตัวอย่าง#include <stdio.h> #include <stri

  17. islessequal() ใน C/C++

    islessequal() ตรวจสอบ argument แรก <= argument ที่สอง ประกาศใน math.h คืน true (1) ถ้าจริง false (0) ถ้าเท็จไวยากรณ์bool islessequal(value1, value2);ตัวอย่างที่ 1 (true)#include <iostream> #include <math.h> using namespace std; int main() { int val1 = 8, val2 = 8; bool result = i

  18. strxfrm() ใน C/C++

    strxfrm() แปลง source string ให้เป็น current locale แล้ว copy ตัวอักษรแรก n ตัวไป destination ประกาศใน locale.hไวยากรณ์size_t strxfrm(char *destination, const char *source, size_t number)destination: pointer ปลายทาง, source: string ต้นทาง, number: จำนวนตัวอักษรที่ copyตัวอย่าง#include <stdio.h>

  19. ความคิดเห็น (Comments) ในภาษา C/C++

    ความคิดเห็น (Comments) เป็นส่วนของโค้ดที่คอมไพเลอร์จะข้ามไป ไม่มีผลต่อการทำงานของโปรแกรม ช่วยให้โค้ดอ่านง่าย เข้าใจง่าย และสะดวกในการบำรุงรักษา ประเภทของ Comments ใน C/C++ // Single Line Comment (บรรทัดเดียว) /* Multi Line Comments (หลายบรรทัด) */ ตัวอย่างโค้ดครบถ้วน #include <stdio.h> #incl

  20. Enum ในภาษา C

    Enumeration (enum) เป็น user-defined datatype กำหนดชื่อให้ค่าคงที่จำนวนเต็ม ทำให้โค้ดอ่านง่าย ใช้ keyword enumไวยากรณ์enum enum_name { const1, const2, ... };กำหนดตัวแปร enum: enum week day; หรือ宣告พร้อมกันตัวอย่าง#include <stdio.h> enum week { Mon=10, Tue, Wed, Thur, Fri=10, Sat=16, Sun }; enu

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