Module 18: Thư viện chuẩn C

Thư viện chuẩn C

stdlib.h - Hàm tiện ích

atoi("123")     // chuoi sang int
atof("3.14")    // chuoi sang double
rand()          // so ngau nhien 0..RAND_MAX
srand(time(0))  // khoi tao seed
abs(-5)         // gia tri tuyet doi
system("cls")   // lenh he thong
qsort(arr, n, sizeof(int), cmp) // sap xep

math.h - Hàm toán học

sqrt(x), pow(x,y), sin(x), cos(x), tan(x)
exp(x), log(x), log10(x), ceil(x), floor(x)
fabs(x), fmod(x,y)

time.h - Thời gian

time_t t = time(NULL); // thoi gian hien tai
clock_t start = clock();
// do something
clock_t end = clock();
double time_spent = (double)(end-start) / CLOCKS_PER_SEC;

string.h - Xử lý chuỗi nâng cao

strtok(str, delim)   // tach chuoi
strstr(haystack, needle) // tim chuoi con
strrchr(s, c)      // vi tri cuoi cung cua c
sprintf(buf, "%d", n) // ghi vao buffer

setjmp.h - Nhảy phi cục bộ

jmp_buf buf;
if (setjmp(buf) == 0) {
    // code binh thuong
} else {
    // xu ly loi
}
longjmp(buf, 1); // nhay den setjmp

assert.h - Debug

assert(ptr != NULL); // crash neu dieu kien sai