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

    Structure เป็น user-defined datatype รวมข้อมูลประเภทต่างๆ เป็นชนิดเดียว มีสมาชิกหลายตัว ใช้ keyword struct เข้าถึงสมาชิกด้วย dot (.) operatorไวยากรณ์struct structure_name { member definition; } structure_variables;ตัวอย่าง#include <stdio.h> #include <string.h> struct Data { int

  2. Union ในภาษา C

    Union เป็น user-defined datatype สมาชิกทุกตัวใช้หน่วยความจำร่วมกัน (same memory location) ขนาด union = ขนาดสมาชิกที่ใหญ่ที่สุด ใช้เมื่อต้องการให้หลายสมาชิกใช้ที่อยู่หน่วยความจำเดียวกันUnion คล้าย struct แต่ใช้ keyword unionไวยากรณ์union union_name { member definition; } union_variables;ตัวอย่าง#

  3. ฟังก์ชัน strchr() ใน C/C++

    strchr() ค้นหาตัวอักษรในสตริง หาตำแหน่งแรกที่เจอ คืน pointer ไปตัวอักษรนั้น ถ้าไม่เจอคืน NULLไวยากรณ์char *strchr(const char *string, int character)string: สตริงที่จะค้นหา, character: ตัวอักษรที่ต้องการค้นหาตัวอย่าง#include <stdio.h> #include <string.h> int main() { char s[] = Hellow

  4. isgreaterequal() ใน C/C++

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

  5. fgetc() และ fputc() ใน C

    fgetc()อ่านตัวอักษรจากไฟล์ คืนค่าตัวอักษร ถ้าสำเร็จ มิฉะนั้น EOFไวยากรณ์: int fgetc(FILE *stream)ตัวอย่าง fgetc()ไฟล์ new.txt: 0,hell!o 1,hello! 2,gfdtrhtrhrt 3,demo#include <stdio.h> #include <conio.h> void main() { FILE *f; char s; clrscr(); f = fopen(new.txt, r); w

  6. เปรียบเทียบ calloc() และ malloc() ใน C

    calloc()จัดสรรหลายบล็อกขนาดเท่ากัน (contiguous location) เริ่มต้นค่าเป็น 0ไวยากรณ์: void *calloc(size_t number, size_t size);number: จำนวน elements, size: ขนาดแต่ละ element (bytes)ตัวอย่าง calloc()#include <stdio.h> #include <stdlib.h> int main() { int n = 4, i, *p, s = 0; p = (i

  7. เปรียบเทียบ fseek() และ rewind() ใน C

    fseek()ย้าย file pointer ไปตำแหน่งเฉพาะ (offset, whence) คืน 0 ถ้าสำเร็จไวยากรณ์: int fseek(FILE *stream, long int offset, int whence)whence: SEEK_END (ท้ายไฟล์), SEEK_SET (ต้นไฟล์), SEEK_CUR (ตำแหน่งปัจจุบัน)ตัวอย่าง fseek()ไฟล์ demo.txt มีข้อความยาว#include <stdio.h> void main() { FILE *

  8. EOF, getc() และ feof() ใน C

    EOF (End of File)getc() คืนค่า EOF เมื่ออ่านจบไฟล์ตัวอย่าง EOFไฟล์ new.txt: This is demo! This is demo!#include <stdio.h> int main() { FILE *f = fopen(new.txt, r); int c = getc(f); while(c != EOF) { putchar(c); c = getc(f); } fclose(f); getchar(); re

  9. การใช้ realloc() ใน C

    realloc() ปรับขนาดบล็อกหน่วยความจำที่จัดสรรด้วย malloc หรือ calloc แล้วไวยากรณ์void *realloc(void *pointer, size_t size)pointer: pointer ไปบล็อกเก่า, size: ขนาดใหม่ (bytes)ตัวอย่าง#include <stdio.h> #include <stdlib.h> int main() { int n = 4, i, *p, s = 0; p = (int*) calloc(n, s

  10. fopen() สำหรับไฟล์ที่มีอยู่แล้วในโหมดเขียน (write mode) ใน C

    fopen() เปิดไฟล์ที่ pointer ชี้ ไว้ read (r) หรือ write (w)ถ้าไฟล์มีอยู่แล้วและเปิดในโหมด w จะถูกทำให้เป็นไฟล์ว่างเปล่า (override เนื้อหาเดิม)ไวยากรณ์FILE *fopen(const char *filename, const char *access_mode)filename: ชื่อไฟล์, access_mode: โหมดการเข้าถึง (r, w, a, ...)ตัวอย่างไฟล์ one.txt มีเนื้อหา

  11. พิมพ์เนื้อหาไฟล์ใน C

    ตัวอย่างพิมพ์เนื้อหาไฟล์ new.txt ที่มีเนื้อหา:0,hell!o 1,hello! 2,gfdtrhtrhrt 3,demoตัวอย่างโค้ด#include <stdio.h> #include <conio.h> void main() { FILE *f; char s; clrscr(); f = fopen(new.txt, r); while((s = fgetc(f)) != EOF) { printf(%c, s); } fclos

  12. ฟังก์ชัน isalnum() ใน C: ตรวจสอบตัวอักษรและตัวเลข

    ฟังก์ชัน isalnum() ตรวจสอบว่าตัวอักษรเป็น Alphanumeric (ตัวอักษร A-Z, a-z หรือ ตัวเลข 0-9) หรือไม่ อยู่ใน Header <ctype.h> ไวยากรณ์ #include <ctype.h> int isalnum(int c); Parameter: int c - ตัวอักษรที่ตรวจสอบ (ต้องเป็น unsigned char หรือ EOF) คืนค่า: Non-zero (True) ถ้าเป็น Alphanumer

  13. ฟังก์ชัน isupper() ใน C: ตรวจสอบตัวอักษรพิมพ์ใหญ่

    ฟังก์ชัน isupper() ตรวจสอบว่าตัวอักษรเป็น ตัวพิมพ์ใหญ่ (Uppercase A-Z) หรือไม่ อยู่ใน Header <ctype.h> ไวยากรณ์ #include <ctype.h> int isupper(int c); Parameter: int c - ตัวอักษรที่ตรวจสอบ (ต้องเป็น unsigned char หรือ EOF) คืนค่า: Non-zero (True) ถ้าเป็น Uppercase, 0 (False) ถ้าไม่ใช่

  14. ฟังก์ชัน strtod() ใน C/C++: แปลง String เป็น Double

    ฟังก์ชัน strtod() (String to Double) แปลง String เป็น Floating-point Number (double) อยู่ใน Header <stdlib.h> เป็นมาตรฐาน C ไวยากรณ์ #include <stdlib.h> double strtod(const char *nptr, char **endptr); nptr - String ที่จะแปลง endptr - Output Parameter: ชี้ไปที่ตัวอักษรแรกที่ไม่ได้ Pars

  15. วิธีแปลง String เป็น Double ใน C/C++

    การแปลง String เป็น Double ใน C/C++ ทำได้หลายวิธี โดยหลักๆ ใช้ strtod() (C) หรือ std::stod (C++) วิธีที่ 1: strtod() - C Standard (แนะนำ) #include <stdio.h> #include <stdlib.h> int main() { char s[] = 18.2894 is a number; char *endptr; double result; result = strto

  16. ฟังก์ชัน memmove() ใน C/C++: ย้ายหน่วยความจำ (รองรับ Overlap)

    ฟังก์ชัน memmove() คล้าย memcpy() แต่ รองรับ Overlapping Memory Regions ทำงานถูกต้องแม้ Source และ Destination จะทับซ้อนกัน อยู่ใน Header <string.h> ไวยากรณ์ #include <string.h> void *memmove(void *dest, const void *src, size_t n); dest - Destination Buffer src - Source Buffer n - จำน

  17. ฟังก์ชัน memcpy() ใน C/C++: คัดลอกหน่วยความจำ

    ฟังก์ชัน memcpy() ใช้คัดลอกบล็อกหน่วยความจำ (Memory Block) จาก Source ไป Destination โดยไม่ตรวจสอบ Overlap อยู่ใน Header <string.h> ไวยากรณ์ #include <string.h> void *memcpy(void *dest, const void *src, size_t n); dest - Pointer ไปยัง Destination Buffer src - Pointer ไปยัง Source Buff

  18. ความแตกต่างระหว่าง strlen() และ sizeof() สำหรับ String ใน C

    ทั้งสองฟังก์ชันใช้หาขนาด/ความยาว แต่วัดสิ่งที่ต่างกันและมี Semantics ที่ต่างกันอย่างสิ้นเชิง strlen() - String Length นับจำนวนตัวอักษรใน String จนกว่าจะเจอ Null Terminator (\0) ไม่รวม Null Terminator เอง ไวยากรณ์ #include <string.h> size_t strlen(const char *str); ตัวอย่าง #include <stdi

  19. ฟังก์ชัน ungetc() ใน C/C++: ส่งคืนตัวอักษรกลับ Stream

    ฟังก์ชัน ungetc() ใช้ส่งคืนตัวอักษร (Push Back) กลับเข้า Input Stream เพื่อให้อ่านซ้ำได้ในครั้งถัดไป ทำงานกับ stdin, File Streams, และ Streams อื่นๆ อยู่ใน Header <stdio.h> ไวยากรณ์ #include <stdio.h> int ungetc(int c, FILE *stream); c - ตัวอักษรที่จะส่งคืน (Converted to unsigned char

  20. ฟังก์ชัน atexit() ใน C/C++: ลงทะเบียนฟังก์ชัน Cleanup ตอนจบโปรแกรม

    ฟังก์ชัน atexit() ใช้ลงทะเบียนฟังก์ชันที่จะถูกเรียกเมื่อโปรแกรมจบอย่างปกติ (Normal Termination) ผ่าน exit() หรือ return จาก main() อยู่ใน Header <stdlib.h> ไวยากรณ์ #include <stdlib.h> int atexit(void (*func)(void)); func - Pointer to Function ที่ไม่รับ Parameter และไม่คืนค่า (void fu

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