Professional Documents
Culture Documents
ThuVienC (HieuIt)
ThuVienC (HieuIt)
ThuVienC (HieuIt)
vn
Mr.V Kim Hiu 1
Th Vin C
Hi! y l Th vin cc hm trong C cn bn m mnh su tm c! n rt hay v hu
ch cc bn. Hi vng bi vit ny s gip ch cho cc bn trong vic hc lp trnh C. Thn!
Mr.V Hiu.
1. In/Out
Danh sch cc hm s dng In/Out
1.clearerr
2.fclose
3.feof
4.ferror
5.fflush
6.fgetc
7.fgetpos
8.fgets
9.fopen
10.fprintf
11.fputc
12.fputs
13.fread
14.freopen
15.fscanf
16.fseek
17.fsetpos
18.ftell
19.fwrite
20.getc
21.getchar
22.gets
Th Vien C Y!M: Information.Technology@yahoo.com.vn
Mr.V Kim Hiu 2
23.perror
24.printf
25.putc
26.putchar
27.puts
28.remove
29.rename
30.rewind
31.scanf
32.setbuf
33.setvbuf
34.sprintf
35.sscanf
36.tmpfile
37.tmpnam
38.ungetc
39.vprintf, vfprintf, and vsprintf
Hm clearerr
Khai bo :
Trch dn:
void clearerr(FILE *stream);
Xo tn hiu li gy ra bi h thng khi thao tc trn tp tin gp li .
Example
Code:
#include <stdio.h>
int main(void)
{
FILE *fp;
char ch;
Th Vien C Y!M: Information.Technology@yahoo.com.vn
Mr.V Kim Hiu 3
/* mo tep tin de ghi */
fp = fopen("DUMMY.FIL", "w");
/*gay nen mot loi bang cach doc file*/
ch = fgetc(fp);
printf("%c\n",ch);
if (ferror(fp))
{
/* xuat ra man hinh thong bao loi */
printf("Error reading from DUMMY.FIL\n");
/* xoa tin hieu loi */
clearerr(fp);
}
fclose(fp);
return 0;
}
Hm fclose
Khai bo :
Trch dn:
int fclose(FILE *stream);
ng tp li sau khi thao tc , stream l tn con tr tp . Khi thnh cng hm tr v 0 , tri li
tr v EOF ( hng ch th kt thc tp )
Example
Code:
#include <string.h>
#include <stdio.h>
int main(void)
{
FILE *fp;
char buf[11] = "0123456789";
fp = fopen("DUMMY.FIL", "w");
fwrite(&buf, strlen(buf), 1, fp);
/* dong tep */
fclose(fp);
return 0;
}
Hm feof
Khai bo :
Trch dn:
Th Vien C Y!M: Information.Technology@yahoo.com.vn
Mr.V Kim Hiu 4
int feof(FILE *stream);
Hm dng kim tra con tr tp c n cui tp hay cha . Nu n n cui tp th hm tr v
gi tr khc 0 , ngc li tr v 0
Example
Code:
#include <stdio.h>
int main(void)
{
FILE *stream;
/* mo mot tep de doc */
stream = fopen("DUMMY.FIL", "r");
/* doc mot ky tu tu tep */
fgetc(stream);
/*kiem tra xem cuoi tep hay chua */
if (feof(stream))
printf("We have reached end-of-file\n");
/* dong tep*/
fclose(stream);
return 0;
}
Hm ferror
Khai bo :
Trch dn:
int ferror(FILE *stream);
Kim tra li thao tc trn tp . Hm tr v gi tr khc 0 nu c li , ngc li tr v 0 .
Example :
Code:
#include <stdio.h>
int main(void)
{
FILE *stream;
/* mo tep de ghi */
stream = fopen("DUMMY.FIL", "w");
/* gay ra mot loi bang cach doc*/
(void) getc(stream);
if (ferror(stream)) /* kiem tra loi tren tep*/
{
/* ghi loi ra man hinh*/
printf("Error reading from DUMMY.FIL\n");
Th Vien C Y!M: Information.Technology@yahoo.com.vn
Mr.V Kim Hiu 5
/* xoa tin hieu loi*/
clearerr(stream);
}
fclose(stream);
return 0;
}
Hm fflush
Khai bo :
Trch dn:
int fflush(FILE *stream);
Hm dng lm sch vng m ca tp . Nu thnh cng hm cho gi tr 0 , tri li hm tr v
EOF
Example
Code:
#include <string.h>
#include <stdio.h>
#include <conio.h>
#include <io.h>
void flush(FILE *stream);
int main(void)
{
FILE *stream;
char msg[] = "This is a test";
/* tao 1 tep */
stream = fopen("DUMMY.FIL", "w");
/*ghi mot vai du lieu len tep */
fwrite(msg, strlen(msg), 1, stream);
clrscr();
printf("Press any key to flush DUMMY.FIL:");
getch();
flush(stream);
printf("\nFile was flushed, Press any key to quit:");
getch();
return 0;
}
void flush(FILE *stream)
{
int duphandle;
/* lam sach vung dem */
fflush(stream);
Th Vien C Y!M: Information.Technology@yahoo.com.vn
Mr.V Kim Hiu 6
duphandle = dup(fileno(stream));
close(duphandle);
}
Hm fgetc
Khai bo :
Trch dn:
int fgetc(FILE *stream);
Hm dng ly 1 k t t tp do con tr tp stream tr n . Nu thnh cng hm tr v m
ASCII ca k t c c . Nu gp li hay gp kt thc tp th hm tr v EOF .
Example
Code:
#include <string.h>
#include <stdio.h>
#include <conio.h>
int main(void)
{
FILE *stream;
char string[] = "This is a test";
char ch;
/* mo tep de ghi va doc */
stream = fopen("DUMMY.FIL", "w+");
/* ghi len tep mot chuoi */
fwrite(string, strlen(string), 1, stream);
/* dat con tro tep len dau tep */
fseek(stream, 0, SEEK_SET);
do
{
/* doc mot ky tu tu tep */
ch = fgetc(stream);
/* in ra man hinh ky tu doc duoc */
putch(ch);
} while (ch != EOF);
fclose(stream);
return 0;
}
Hm fgetpos
Khai bo :
Trch dn:
Th Vien C Y!M: Information.Technology@yahoo.com.vn
Mr.V Kim Hiu 7
int fgetpos( FILE *stream,fpos_t *pos )
Hm dng ly v tr con tr tp ( byte th my ) lu vo trong bin pos . Nu thnh cng hm
tr v 0 , ngc li hm tr v gi tr khc 0 .
Example
Code:
#include <stdlib.h>
#include <stdio.h>
void showpos(FILE *stream);
int main(void)
{
FILE *stream;
fpos_t filepos;
/* mo file de doc ghi */
stream = fopen("DUMMY.FIL", "w+");
/* luu lai vi tri con tro tep hien thoi */
fgetpos(stream, &filepos);
/* ghi du lieu len tep */
fprintf(stream, "This is a test");
/* in ra man hinh vi tri con tro hien thoi */
showpos(stream);
/* dat lai vi tri con tro tep */
if (fsetpos(stream, &filepos) == 0)
showpos(stream);
else
{
fprintf(stderr, "Error setting file pointer.\n");
exit(1);
}
/* dong tep*/
fclose(stream);
return 0;
}
void showpos(FILE *stream)
{
fpos_t pos;
/* in ra man hinh vi tri con tro tep hien thoi */
fgetpos(stream, &pos);
printf("File position: %ld\n", pos);
}
Hm fgets
Khai bo :
Trch dn:
Th Vien C Y!M: Information.Technology@yahoo.com.vn
Mr.V Kim Hiu 8
char *fgets(char *s,int n,FILE *fp);
Hm dng c mt chui k t di n-1 k t t tp fp vo chui s . Vic c kt thc khi
c n-1 k t hay gp du xung dng hay gp kt thc tp . Sau khi c , chui s s c
t ng thm vo k t NULL vo cui chui . Hm tr v i ch chui s nu thnh cng , tri li
tr v NULL .
Example
Code:
#include <string.h>
#include <stdio.h>
int main(void)
{
FILE *stream;
char string[] = "This is a test";
char msg[20];
/* mo tep de doc ghi */
stream = fopen("DUMMY.FIL", "w+");
/* ghi mot chuoi vao tep */
fwrite(string, strlen(string), 1, stream);
/* dat con tro tep len dau tep */
fseek(stream, 0, SEEK_SET);
/* doc mot chuoi tu tep */
fgets(msg, strlen(string)+1, stream);
/* in chuoi ra man hinh */
printf("%s", msg);
fclose(stream);
return 0;
}
Hm fopen
Khai bo :
Trch dn:
FILE *fopen(char *filename,char *type );
Hm dng m tp . Vi filename l chui cha tn tp cn m ( bao gm c ng dn c
th ) . Lu mt cht vi filename ny . Nu bn nhp chui t bn phm th khc vi bn nh
sn n trong code ca bn . V d tp ta cn m tn l abc.jpg nm C . i vi nh tn tp
t bn phm th ta nh nh sau : C:\abc.jpg . Nhng nu ta son sn trong code th ta son
nh sau : C:\\abc.jpg ( tc l thm 1 gch \ ) .
Kiu m tp c rt nhiu cch nhng chung quy ta c cc trng hp sau cho kiu vn bn :
"r" : c
"w" : ghi
"r+" hay "w+" : c v ghi
"a" : ghi b sung
"a+" : c ghi b sung
Th Vien C Y!M: Information.Technology@yahoo.com.vn
Mr.V Kim Hiu 9
i vi m file nh phn bn ch cn thm ch b vo sau cc chui trn .
Nu vic m tp thnh cng hm cho a ch con tr tp , tri li hm tr v NULL .
Example
Code:
#include <stdio.h>
int main(void)
{
FILE *in, *out;
if ((in = fopen("\\AUTOEXEC.BAT", "r"))
== NULL)
{
fprintf(stderr, "Cannot open input file.\n");
return 1;
}
if ((out = fopen("\\AUTOEXEC.BAK", "w"))
== NULL)
{
fprintf(stderr, "Cannot open output file.\n");
return 1;
}
while (!feof(in))
fputc(fgetc(in), out);
fclose(in);
fclose(out);
return 0;
}
Hm fprintf
Khai bo :
Trch dn:
int fprintf(FILE *fp,char *s,...);
Hm ny chc nng y chang nh hm printf nhng thay v ghi ra mn hnh th n ghi ln tp fp .
Nu thnh cng hm tr v s byte ghi c ln tp , tri li hm tr v EOF .
Example
Code:
#include <stdio.h>
int main(void)
{
FILE *stream;
int i = 100;
char c = 'C';
float f = 1.234;
Th Vien C Y!M: Information.Technology@yahoo.com.vn
Mr.V Kim Hiu 10
/* mo tep de doc ghi */
stream = fopen("DUMMY.FIL", "w+");
/* ghi du lieu len tep */
fprintf(stream, "%d %c %f", i, c, f);
/* dong tep */
fclose(stream);
return 0;
}
Hm fputc
Khai bo :
Trch dn:
int fputc(int ch,FILE *fp);
Hm ghi mt k t c m ASCII l ch ln tp fp . khi thnh cng tr v m ASCII ca
k t c ghi , tri li hm cho EOF .
Example
Code:
#include <stdio.h>
int main(void)
{
char msg[] = "Hello world";
int i = 0;
while (msg[i])
{
fputc(msg[i], stdout);
i++;
}
return 0;
}
Hm fputs
Khai bo :
Trch dn:
int fputs(char *s,FILE *fp);
Hm ghi ln tp fp chui s . Ch rng chui s nn kt thc bng k t NULL , nu ko vic ghi
ln c th b sai lch . K t NULL ny ch ra v tr kt thc tp cho r rng ch n ko c ghi
ln tp . Hm tr v k t cui cng c ghi ln tp nu thnh cng , tri li hm cho EOF .
Example
Code:
Th Vien C Y!M: Information.Technology@yahoo.com.vn
Mr.V Kim Hiu 11
#include <stdio.h>
int main(void)
{
/* ghi mot chuoi ra dong xuat chuan ( man hinh ) */
fputs("Hello world\n", stdout);
return 0;
}
Hm fread
Khai bo :
Trch dn:
int fread(void *ptr,int size,int n,FILE *fp);
Hm c n mu tin , mi mu c kch thc size t tp fp lu vo trong vng nh ptr . Hm tr
v mt gi tr bng s mu tin thc s c c . Nu gp li hay gp kt thc tp hm tr v 0
.
Example
Code:
#include <string.h>
#include <stdio.h>
int main(void)
{
FILE *stream;
char msg[] = "this is a test";
char buf[20];
if ((stream = fopen("DUMMY.FIL", "w+"))
== NULL)
{
fprintf(stderr, "Cannot open output file.\n");
return 1;
}
/* ghi du lieu len tep */
fwrite(msg, strlen(msg)+1, 1, stream);
/* dat con tro tep len dau tep */
fseek(stream, SEEK_SET, 0);
/* doc du lieu tu tep va xuat ra man hinh */
fread(buf, strlen(msg)+1, 1, stream);
printf("%s\n", buf);
fclose(stream);
return 0;
}
Th Vien C Y!M: Information.Technology@yahoo.com.vn
Mr.V Kim Hiu 12
Hm freopen
Khai bo :
Trch dn:
FILE *freopen(char *filename,char *type,FILE *fp);
Hm ny c chc nng gn con tr tp fp vo tp filename . Sau c th m c ghi trn tp
ny thng qua con tr tp fp . Hm ny c bit c ch khi ta mun gn cc con tr tp chun (
stdin , stdout , stderr ) vo 1 tp ca ta . Hm tr v NULL nu gp li .
Example
Code:
#include <stdio.h>
int main(void)
{
/* gan con tro tep chuan stdout ( man hinh ) vao tep output.fil */
if (freopen("OUTPUT.FIL", "w", stdout)
== NULL)
fprintf(stderr, "error redirecting stdout\n");
/* khi do cau lenh nay se khong con xuat ra man hinh nua ma xuat vao
tep output.fil */
printf("This will go into a file.");
/*dong tep stdout lai */
fclose(stdout);
return 0;
}
Hm fscanf
Khai bo :
Trch dn:
int fscanf(FILE *fp,char *s,...);
Hm c chc nng c d liu t tp . Lm vic ging nh hm scanf nhng thay v c t mn
hnh th hm fscanf c t tp thi . Hm tr v 1 gi tr bng s trng c c .
Example
Code:
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
int i;
printf("Input an integer: ");
/* doc mot so nguyen tu dong vao chuan */
Th Vien C Y!M: Information.Technology@yahoo.com.vn
Mr.V Kim Hiu 13
if (fscanf(stdin, "%d", &i))
printf("The integer read was: %i\n", i);
else
{
fprintf(stderr, "Error reading an integer from
stdin.\n");
exit(1);
}
return 0;
}
Hm fseek
Khai bo :
Trch dn:
int fseek(FILE *fp,long sb,int xp);
Hm dng di chuyn con tr tp fp n v tr bt k . Vi sb l s byte cn di chuyn , xp l v tr
xut pht . Nu sb dng tc l di chuyn v cui tp , nu m th di chuyn v u tp . Nu xp
l 0 hay SEEK_SET tc l xut pht t u tp , nu l SEEK_CUR hay 1 tc l xut pht ti v tr
hin thi ca con tr tp , nu l SEEK_END hay 2 tc l xut pht ti cui tp .
Example
Code:
#include <stdio.h>
long filesize(FILE *stream);
int main(void)
{
FILE *stream;
stream = fopen("MYFILE.TXT", "w+");
fprintf(stream, "This is a test");
printf("Filesize of MYFILE.TXT is %ld bytes\n",
filesize(stream));
fclose(stream);
return 0;
}
long filesize(FILE *stream)
{
long curpos, length;
curpos = ftell(stream);
fseek(stream, 0L, SEEK_END);
length = ftell(stream);
fseek(stream, curpos, SEEK_SET);
return length;
}
Th Vien C Y!M: Information.Technology@yahoo.com.vn
Mr.V Kim Hiu 14
Hm fsetpos
Khai bo :
Trch dn:
int fsetpos(FILE *stream , fpos_t pos);
Hm ny thng i km cng hm fgetpos . Hm dng di chuyn con tr tp n v tr c
lu trong con tr pos . V tr ny do hm fgetpos lu li t trc . Hm tr v 0 nu thnh cng ,
ngc li tr v gi tr khc 0 .
Example
Code:
#include <stdlib.h>
#include <stdio.h>
void showpos(FILE *stream);
int main(void)
{
FILE *stream;
fpos_t filepos;
/* mo file de doc ghi */
stream = fopen("DUMMY.FIL", "w+");
/* luu lai vi tri con tro tep hien thoi */
fgetpos(stream, &filepos);
/* ghi du lieu len tep */
fprintf(stream, "This is a test");
/* in ra man hinh vi tri con tro hien thoi */
showpos(stream);
/* dat lai vi tri con tro tep */
if (fsetpos(stream, &filepos) == 0)
showpos(stream);
else
{
fprintf(stderr, "Error setting file pointer.\n");
exit(1);
}
/* dong tep*/
fclose(stream);
return 0;
}
void showpos(FILE *stream)
{
fpos_t pos;
/* in ra man hinh vi tri con tro tep hien thoi */
fgetpos(stream, &pos);
printf("File position: %ld\n", pos);
Th Vien C Y!M: Information.Technology@yahoo.com.vn
Mr.V Kim Hiu 15
}
Hm ftell
Khai bo :
Trch dn:
long ftell(FILE *fp);
Khi thnh cng hm cho vit v tr con tr tp hin thi ang v tr th my ( tnh t u tp ) ,
s hiu ca byte tnh t 0 . Ngc li hm tr v -1L . Ni thm 1 t v -1L c ngha l -1 m lu
di dng long ( dng 4 byte lu ) .
Example
Code:
int main(void)
{
FILE *stream;
stream = fopen("MYFILE.TXT", "w+");
fprintf(stream, "This is a test");
printf("The file pointer is at byte %ld\n",
ftell(stream));
fclose(stream);
return 0;
}
Hm fwrite
Khai bo :
Trch dn:
int fwrite( void *ptr,int size,int n,FILE *fp );
Hm dng ghi n mu tin kch thc size byte t vng nh ptr ln tp fp . Hm tr v 1 gi tr
bng s mu tin thc s c c .
Example
Code:
#include <stdio.h>
struct mystruct
{
int i;
char ch;
};
int main(void)
{
FILE *stream;
struct mystruct s;
Th Vien C Y!M: Information.Technology@yahoo.com.vn
Mr.V Kim Hiu 16
if ((stream = fopen("TEST.$$$", "wb")) == NULL) /* mo tep TEST.$$$
*/
{
fprintf(stderr, "Cannot open output file.\n");
return 1;
}
s.i = 0;
s.ch = 'A';
fwrite(&s, sizeof(s), 1, stream); /* vit cu trc s ln tep */
fclose(stream); /* dong tep */
return 0;
}
2. String & Character
Danh sch cc hm vi String & Character
1.atof
2.atoi
3.atol
4.isalnum
5.isalpha
6.iscntrl
7.isdigit
8.isgraph
9.islower
10.isprint
11.ispunct
12.isspace
13.isupper
14.isxdigit
15.memchr
16.memcmp
17.memcpy
18.memmove
Th Vien C Y!M: Information.Technology@yahoo.com.vn
Mr.V Kim Hiu 17
19.memset
20.strcat
21.strchr
22.strcmp
23.strcoll
24.strcpy
25.strcspn
26.strerror
27.strlen
28.strncat
29.strncmp
30.strncpy
31.strpbrk
32.strrchr
33.strspn
34.strstr
35.strtod
36.strtok
37.strtol
38.strtoul
39.strxfrm
40.tolower
41.toupper
1. atof()
Tn hm: atof()
nh ngha:
PHP Code:
Th Vien C Y!M: Information.Technology@yahoo.com.vn
Mr.V Kim Hiu 18
#include <stdlib.h>
double atof( const char *str );
Chui 'str' phi bt u bng k t s, nu khng s b li chng trnh (termination)
V d v 3 trng hp
PHP Code:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char *strA="30.4";
char *strB="17.3 thu xem dc khong?";
char *strC="Cai nay chac co loi 33.4";
double a,b,c;
a = atof(strA);
b = atof(strB);
//c = atof(strC); <--- Dong nay convert se bi loi
printf("\nGia tri cua chuoi %s sau khi convert la %.2f",strA,a);
printf("\nGia tri cua chuoi %s sau khi convert la %.2f",strB,b);
//printf("\nGia tri cua chuoi %s sau khi convert la %.2f",strC,c);
<--- bo comment ra se co loi
return 0;
}
2. atoi()
Tn hm: atoi()
nh ngha:
PHP Code:
#include <cstdlib>
int atoi( const char *str );
'str' phi bt u bng s hoc k t trng <space> nu khng convert s fail v tr v 0.
Code minh ha
PHP Code:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int i;
i = atoi( "512" );
printf("\nGia tri cua i la: %d",i);
i = atoi( "512.035" );
printf("\nGia tri cua i la: %d",i);
i = atoi( " 512.035" );
printf("\nGia tri cua i la: %d",i);
i = atoi( " 512+34" );
printf("\nGia tri cua i la: %d",i);
Th Vien C Y!M: Information.Technology@yahoo.com.vn
Mr.V Kim Hiu 19
i = atoi( " 512 bottles of beer on the wall" );
printf("\nGia tri cua i la: %d",i);
// Truong hop duoi day se in ra 0
i = atoi(" that bai 512 ");
printf("\nGia tri cua i la: %d",i);
return 0;
}
3. atol()
Tn hm: atol()
nh ngha:
PHP Code:
#include <stdlib.h>
long atol( const char *str );
Tng t nh atoi()
Minh ha
PHP Code:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int i;
i = atol( "512" );
printf("\nGia tri cua i la: %d",i);
i = atol( "512.035" );
printf("\nGia tri cua i la: %d",i);
i = atol( " 512.035" );
printf("\nGia tri cua i la: %d",i);
i = atol( " 512+34" );
printf("\nGia tri cua i la: %d",i);
i = atol( " 512 bottles of beer on the wall" );
printf("\nGia tri cua i la: %d",i);
// Truong hop duoi day se in ra 0
i = atol(" that bai 512 ");
printf("\nGia tri cua i la: %d",i);
return 0;
}
4. isalnum()
Tn hm: isalnum()
nh ngha:
PHP Code:
#include <ctype.h>
int isalnum( int ch );
Kim tra mt k t l s hay ch ci.
Nu ng tr v gi tr khc 0
Nu sai tr v 0
PHP Code:
#include <stdio.h>
#include <ctype.h>
Th Vien C Y!M: Information.Technology@yahoo.com.vn
Mr.V Kim Hiu 20
int main(void)
{
char x='X';
if(isalnum(x)) {
printf("Day la ki tu %c",x);
} else {
printf("Ki tu %c ko phai la so hoac chu",x);
}
return 0;
}
5.isalpha()
Tn hm: isalpha()
nh ngha:
PHP Code:
#include <ctype.h>
int isalpha( int ch );
Kim tra mt k t c phi l ch ci hay khng.
ng th tr v khc 0
Sai th tr v 0
PHP Code:
#include <stdio.h>
#include <ctype.h>
int main(void)
{
char x='0';
if(isalpha(x)) {
printf("Day la ki tu chu cai %c",x);
} else {
printf("Ki tu %c ko phai la chu cai",x);
}
return 0;
}
6. iscntrl()
Tn hm: iscntrl()
nh ngha:
PHP Code:
#include <ctype.h>
int iscntrl( int ch );
Kim tra mt k t c thuc nhm k t iu khin hay khng
Nhm k t iu khin (Control Character) nm t : 0x00 n 0x1F v k t 0x7F
ng th tr v khc 0
Sai th tr v 0
Th Vien C Y!M: Information.Technology@yahoo.com.vn
Mr.V Kim Hiu 21
Xem bng k t y:
PHP Code:
#include <stdio.h>
#include <ctype.h>
int main(void)
{
char x='\x7F';
if(iscntrl(x)) {
printf("Day la ki tu dieu khien ",x);
} else {
printf("Ki tu nay ko thuoc nhom ki tu dieu khien",x);
}
return 0;
}
7. isdigit()
Tn hm: isdigit()
nh ngha:
Th Vien C Y!M: Information.Technology@yahoo.com.vn
Mr.V Kim Hiu 22
PHP Code:
#include <ctype.h>
int isdigit( int ch );
Kim tra 1 k t c phi l k t s hay khng
ng th tr v khc 0
Sai th tr v 0
PHP Code:
#include <stdio.h>
#include <ctype.h>
int main(void)
{
char x='7';
if(isdigit(x)) {
printf("Ki tu %c la ki tu so",x);
} else {
printf("Ki tu %c ko phai ki tu so",x);
}
return 0;
}
8. isgraph()
Tn hm: isgraph()
nh ngha:
PHP Code:
#include <ctype.h>
int isgraph( int ch );
Bt c k t no c th in ra c (printable character) u gi l Graphical Character, ngoi tr
k t <space>
Kim tra xem k t c thuc nhm graph khng
ng th tr v khc 0
Sai th tr v 0
PHP Code:
#include <stdio.h>
#include <ctype.h>
int main(void)
{
char x='t';
if(isgraph(x)) {
printf("Ki tu %c thuoc nhom graphical char",x);
} else {
printf("Ki tu %c ko thuoc nhom graphical char",x);
}
return 0;
}
9. islower()
Th Vien C Y!M: Information.Technology@yahoo.com.vn
Mr.V Kim Hiu 23
Tn hm: islower()
nh ngha
PHP Code:
#include <ctype.h>
int islower( int ch );
Kim tra mt k t xem c phi ch in thng khng
ng th tr v khc 0
Sai th tr v 0
PHP Code:
#include <stdio.h>
#include <ctype.h>
int main(void)
{
char x='K';
if(islower(x)) {
printf("Ki tu %c la chu in thuong",x);
} else {
printf("Ki tu %c ko phai chu in thuong",x);
}
return 0;
}
10. isprint()
Tn hm: isprint()
nh ngha:
PHP Code:
#include <ctype.h>
int isprint( int ch );
Printable character bao gm Graphical Character v k t trng <space>
kim tra 1 k t c phi l printable character hay khng
ng th tr v khc 0
Sai th tr v 0
PHP Code:
#include <stdio.h>
#include <ctype.h>
int main(void)
{
char x='\x7F';
if(isprint(x)) {
printf("Ki tu %c la printable char",x);
} else {
printf("Ki tu %c ko phai printable char",x);
}
return 0;
}
11. ispunct()
Th Vien C Y!M: Information.Technology@yahoo.com.vn
Mr.V Kim Hiu 24
Tn hm: ispunct()
nh ngha:
PHP Code:
#include <ctype.h>
int ispunct( int ch );
Punctuation Character l cc k t trong nhm Graphical Character nhng khng phi l ch ci
hay s
ng th tr v khc 0
Sai th tr v 0
PHP Code:
#include <stdio.h>
#include <ctype.h>
int main(void)
{
char x='\'';
if(ispunct(x)) {
printf("Ki tu %c la punct char",x);
} else {
printf("Ki tu %c ko phai punct char",x);
}
return 0;
}
12. isspace()
Tn hm: isspace()
nh ngha:
PHP Code:
#include <ctype.h>
int isspace( int ch );
Space Character l nhng k t to nn khong trng (line feed, form feed...)
ng th tr v khc 0
Sai th tr v 0
PHP Code:
#include <stdio.h>
#include <ctype.h>
int main(void)
{
char x='\x0B';
if(isspace(x)) {
printf("Ki tu %c la space char",x);
} else {
printf("Ki tu %c ko phai space char",x);
}
return 0;
}
Th Vien C Y!M: Information.Technology@yahoo.com.vn
Mr.V Kim Hiu 25
13. isupper()
Tn hm: isupper()
nh ngha:
PHP Code:
#include <ctype.h>
int isupper( int ch );
Kim tra mt k t c phi ch in Hoa hay khng
ng th tr v khc 0
Sai th tr v 0
PHP Code:
#include <stdio.h>
#include <ctype.h>
int main(void)
{
char x='x';
if(isupper(x)) {
printf("Ki tu %c la chu in hoa",x);
} else {
printf("Ki tu %c ko phai chu in hoa",x);
}
return 0;
}
14. isxdigit()
Tn hm: isxdigit()
nh ngha:
PHP Code:
#include <ctype.h>
int isxdigit( int ch );
Kim tra xem mt k t c phi l s h thp lc (Hex - 16) hay khng
ng th tr v khc 0
Sai th tr v 0
PHP Code:
#include <stdio.h>
#include <ctype.h>
int main(void)
{
char x='B';
if(isxdigit(x)) {
printf("Ki tu %c la hex char",x);
} else {
printf("Ki tu %c ko phai hex char",x);
}
return 0;
}
15. memchr()
Th Vien C Y!M: Information.Technology@yahoo.com.vn
Mr.V Kim Hiu 26
Tn hm: memchr()
nh ngha:
PHP Code:
#include <string.h>
void *memchr( const void *buffer, int ch, size_t count );
Tm v tr xut hin u tin ca k t 'ch' trong buffer ca mt chui 'buffer' trong khong k t
gii hn 'count' cho trc.
Nu tm thy th tr v v tr k t xut hin trong chui 'buffer'
Ngc li NULL tr v
PHP Code:
#include <stdio.h>
#include <string.h>
int main(void)
{
char buffer[]="Xcross87 is a chick of programming";
char x='X';
int found = memchr(buffer,x,strlen(buffer));
if(found) {
printf(" Tim thay %c ",x);
} else {
printf(" Khong tim thay ki tu %c ",x);
}
return 0;
}
16. memcmp()
Tn hm: memcmp()
nh ngha:
PHP Code:
#include <string.h>
int memcmp( const void *buffer1, const void *buffer2, size_t count );
So snh 'count' k t u tin ca 2 chui buffer1 v buffer2
Gi tr tr v:
PHP Code:
Nh hn 0 : buffer1 < buffer2
= 0 : buffer1=buffer2
Ln hn 0 : buffer1 > buffer2
PHP Code:
#include <stdio.h>
#include <string.h>
int main(void)
{
char buffer1[]="Xcross87 is a chick of programming";
char buffer2[]="Xcross87 is a crazy";
Th Vien C Y!M: Information.Technology@yahoo.com.vn
Mr.V Kim Hiu 27
int compare_result = memcmp(buffer1,buffer2,strlen(buffer2));
if(compare_result < 0) {
printf(" buffer1 < buffer2 ");
} else if (compare_result == 0) {
printf(" buffer1 = buffer2 ");
} else {
printf(" buffer1 > buffer2 ");
}
return 0;
}
17. memcpy()
Tn hm: memcpy()
nh ngha:
PHP Code:
#include <string.h>
void *memcpy( void *to, const void *from, size_t count );
Copy 'count' k t t 'from' sang 'to'.
Gi tr tr v l 'to'
Ch : hm ny s copy nguyn chnh xc s bytes cn copy v khng copy bytes cui cng ca
string '\0'. V vy cn phi copy n+1 nu mun copy n byte
PHP Code:
#include <stdio.h>
#include <string.h>
int main(void)
{
const char from[]="Xcross87 is a chick of programming";
char* to;
memcpy(to,from,strlen(from)+1);
printf(" Gia tri cua \'to\' sau khi copy la: %s ",to);
return 0;
}
18. memmove()
Tn hm: memmove()
nh ngha:
PHP Code:
#include <string.h>
void *memmove( void *to, const void *from, size_t count );
Ging hm memcpy() nhng x l c tnh hung overlap vi from v to.
Ch : nn s dng memmove() thay v s dng memcpy()
PHP Code:
Th Vien C Y!M: Information.Technology@yahoo.com.vn
Mr.V Kim Hiu 28
#include <stdio.h>
#include <string.h>
int main(void)
{
const char from[]="Xcross87 is a chick of programming";
char* to;
memmove(to,from,strlen(from));
printf("\n Gia tri cua \'to\' sau khi move la: %s ",to);
return 0;
}
19. memset()
Tn hm: memset()
nh ngha:
PHP Code:
#include <string.h>
void* memset( void* buffer, int ch, size_t count );
Lp y 'count' bytes u tin trong 'buffer' vi k t 'ch'
Hm chuyn s dng lp y hoc khi to memory block hay array, tc x l tt hn
vng lp.
PHP Code:
#include <stdio.h>
#include <string.h>
#define LENGTH 10
int main(void)
{
char x='X';
char buffer[LENGTH];
int cnt=0;
memset(buffer,x,LENGTH);
for(;cnt < LENGTH; cnt++) {
printf("\nbuffer[%d] = %c",cnt,buffer[cnt]);
}
return 0;
}
20. strcat()
Tn hm: strcat()
nh ngha:
PHP Code:
#include <string.h>
char *strcat( char *str1, const char *str2 );
Ni 'str2' vo 'str1' v tr v 'str1'
Ch : Hm d xy ra overflow, copy qu gii hn cho php (over bound)
PHP Code:
Th Vien C Y!M: Information.Technology@yahoo.com.vn
Mr.V Kim Hiu 29
#include <stdio.h>
#include <string.h>
int main(void)
{
char *str1="Xcross87 ";
char *str2="is a crazy";
str1 = strcat(str1,str2);
printf(" Sau khi noi chuoi: %s ",str1);
return 0;
}
21. strchr()
tn hm: strchr()
nh ngha:
PHP Code:
#include <string.h>
char *strchr( const char *str, int ch );
Hm tr v gi tr chui bt u vi 'ch' trong chui 'str'. Tr v NULL nu khng tm thy g ht.
v d di y s cho ra kt qu l : 'ss87'
PHP Code:
#include <stdio.h>
#include <string.h>
int main(void)
{
const char *str="Xcross87 ";
char* found;
found = strchr(str,'s');
if ( NULL == found ) {
printf(" Not found ");
} else {
printf(" Found %s ",found);
}
return 0;
}
22. strcmp()
tn hm: strcmp()
nh ngha:
PHP Code:
#include <string.h>
int strcmp( const char *str1, const char *str2 );
So snh 2 chui 'str1' v 'str2'
Gi tr tr v ;
Th Vien C Y!M: Information.Technology@yahoo.com.vn
Mr.V Kim Hiu 30
PHP Code:
< 0 : str1 < str2
= 0 : str1 = str2
> 0 : str1 > str2
PHP Code:
#include <stdio.h>
#include <string.h>
int main(void)
{
const char *str1="Xcross87 ";
const char *str2="Xcross";
int result = strcmp(str1,str2);
if(result < 0) {
printf(" str1 < str2 ");
} else if (result == 0) {
printf(" str1 = str2 ");
} else {
printf(" str1 > str2 ");
}
return 0;
}
23. strcoll()
tn hm: strcoll()
nh ngha:
PHP Code:
#include <cstring>
int strcoll( const char *str1, const char *str2 );
24. strcpy()
tn hm: strcpy()
nh ngha:
PHP Code:
#include <string.h>
char *strcpy( char *to, const char *from );
Copy t chui 'from' sang chui 'to'
Ch : hm d xy ra buffer overflow
PHP Code:
#include <stdio.h>
#include <string.h>
int main(void)
{
const char *from="Xcross87 ";
char *to;
Th Vien C Y!M: Information.Technology@yahoo.com.vn
Mr.V Kim Hiu 31
to = strcpy(to,from);
printf("sau khi copy: %s",to);
return 0;
}
25. strcspn()
tn hm: strcspn()
nh ngha:
PHP Code:
#include <string.h>
size_t strcspn( const char *str1, const char *str2 );
Tr v v tr u tin ca bt c k t no trong 'str1' tm thy trong 'str2'
Nu khng tm thy th tr v di chui str1
PHP Code:
#include <stdio.h>
#include <string.h>
int main(void)
{
const char *str="Xcross87 ";
char *key="123456789";
int pos = strcspn(str,key);
printf("tim thay o vi tri %d",pos);
return 0;
}
26. strerror()
tn hm: strerror()
nh ngha:
PHP Code:
#include <string.h>
char *strerror( int num );
Ly chui error bo li tng ng ca li tm c
V d di y minh ha bo li khng tm thy file
PHP Code:
#include <stdio.h>
#include <string.h>
#include <errno.h>
int main(void)
Th Vien C Y!M: Information.Technology@yahoo.com.vn
Mr.V Kim Hiu 32
{
FILE* pfile;
pfile = fopen("file_name.o","r");
if (NULL == pfile) {
printf("%s",strerror(errno));
}
return 0;
}
27. strlen()
tn hm: strlen()
nh ngha:
PHP Code:
#include <string.h>
size_t strlen( char *str );
Hm tr v di ca chui
PHP Code:
#include <stdio.h>
#include <string.h>
int main(void)
{
const char *str="congdongcviet.com";
printf("do dai chuoi la: %d",strlen(str));
return 0;
}
28. strncat()
tn hm: strncat()
nh ngha:
PHP Code:
#include <string.h>
char *strncat( char *str1, const char *str2, size_t count );
Ni chui c ban nh hm strcat() nhng ch nh s byte ca chui str2 ni vo str1
PHP Code:
#include <stdio.h>
#include <string.h>
int main(void)
{
const char *str1="con ga";
const char *str2=" xcross87 is crazy";
char *result;
result = strncat(str1,str2,9);
Th Vien C Y!M: Information.Technology@yahoo.com.vn
Mr.V Kim Hiu 33
printf("Chuoi sau khi noi: %s",result);
return 0;
}
29. strncmp()
tn hm: strncmp()
nh ngha:
PHP Code:
#include <string.h>
int strncmp( const char *str1, const char *str2, size_t count );
Ging hm strcmp() nhng ch so snh 'count' bytes cho trc.
30. strncpy()
tn hm: strncpy()
nh ngha:
PHP Code:
#include <string.h>
char *strncpy( char *to, const char *from, size_t count );
Copy t from vo to vi s byte quy nh
31. strpbrk()
tn hm: strpbrk()
nh ngha:
PHP Code:
#include <string.h>
char* strpbrk( const char* str1, const char* str2 );
tr v k t xut hin u tin trong 'str1' xut hin trong 'str2'.
Tr v NULL nu khng tm thy.
PHP Code:
#include <stdio.h>
#include <string.h>
int main(void)
{
const char *str1="con ga trong ko phai la con ga mai";
const char *str2="zzzzzzzzzzzza";
char* found;
found = strpbrk(str1,str2);
if (NULL != found) {
printf(" Tim thay ki tu dau tien: %c ",*found);
}
return 0;
}
Th Vien C Y!M: Information.Technology@yahoo.com.vn
Mr.V Kim Hiu 34
32. strpbrk()
tn hm: strpbrk()
nh ngha:
PHP Code:
#include <string.h>
char* strpbrk( const char* str1, const char* str2 );
tr v k t xut hin u tin trong 'str1' xut hin trong 'str2'.
Tr v NULL nu khng tm thy.
PHP Code:
#include <stdio.h>
#include <string.h>
int main(void)
{
const char *str1="con ga trong ko phai la con ga mai";
const char *str2="zzzzzzzzzzzza";
char* found;
found = strpbrk(str1,str2);
if (NULL != found) {
printf(" Tim thay ki tu dau tien: %c ",*found);
}
return 0;
}
33. strrchr
tn hm: strrchr()
nh ngha:
PHP Code:
#include <string.h>
char *strrchr( const char *str, int ch );
Tr v s xut hin cui cng ca k t 'ch' trong chui 'str'
35. strstr(0
tn hm: strstr()
nh ngha:
PHP Code:
#include <string.h>
size_t strspn( const char *str1, const char *str2 );
tr v con tr ti v tr u tin ca str2 trong str1 nu tm thy
tr v NULL nu khng thy
nu di ca str2 l 0 th tr v di ca str1.
PHP Code:
#include <stdio.h>
#include <string.h>
int main(void)
Th Vien C Y!M: Information.Technology@yahoo.com.vn
Mr.V Kim Hiu 35
{
const char *str1="con ga trong ko phai la con ga mai";
const char *str2="la";
char* result;
result = strstr(str1,str2);
if(NULL != result) {
printf(" Doan tim thay: %s", result);
}
return 0;
}
36. strtod()
tn hm: strtod()
nh ngha:
PHP Code:
#include <stdlib.h>
double strtod( const char *start, char **end );
start l chui u vo ca convert; end l phn cn li ca chui sau khi convert.
convert thnh cng th tr v s double convert
nu tht bi th tr v: 0.0
nu convert thnh cng m kt qu li qu to so vi gii hn ca double th HUGE_VAL c tr
v v errno=ERANGE
nu underflow th tr v 0.0 v errno=ERANGE
PHP Code:
#include <stdio.h>
#include <stdlib.h>
int main ()
{
char *start="12.3 xcross87 in congdongcviet.com";
char *end;
double result;
result = strtod(start,&end);
printf("Sau khi convert: ");
printf("\n start = %s ",start);
printf("\n end = %s ",end);
printf("\n result = %lf",result);
return 0;
}
37. strtok()
tn hm: strtok()
nh ngha:
PHP Code:
Th Vien C Y!M: Information.Technology@yahoo.com.vn
Mr.V Kim Hiu 36
#include <string.h>
char *strtok( char *str1, const char *str2 );
tr v chui u tin c ca ra trong str1 theo kha str2.
nu khng tm thy th tr v NULL.
sau khi kt thc ton b token trong str1 th str1=NULL
PHP Code:
#include <stdio.h>
#include <string.h>
int main ()
{
char *str1="xcross87 | xcross87.info | hcegroup.net | congdongcviet
.com";
const char *str2="|";
char *token = NULL;
// token lan 1
token = strtok(str1,str2);
printf("\nstr1 = %s",str1);
printf("\nstr2 = %s",str2);
printf("\ntoken = %s\n",token);
// token lan 2
token = strtok(NULL,str2);
printf("\nstr1 = %s",str1);
printf("\nstr2 = %s",str2);
printf("\ntoken = %s\n",token);
return 0;
}
38. strtol()
tn hm: strtol()
nh ngha:
PHP Code:
#include <stdlib.h>
long strtol( const char *start, char **end, int base );
tng t strtod() nhng chui vo start c base cho trc convert sang.
39. strtoul()
tn hm: strtoul()
nh ngha:
PHP Code:
#include <stdlib.h>
unsigned long strtoul( const char *start, char **end, int base );
ging nh strtol() nhng cho kiu unsigned long
Th Vien C Y!M: Information.Technology@yahoo.com.vn
Mr.V Kim Hiu 37
40. strxfrm()
tn hm: strxfrm()
nh ngha:
PHP Code:
#include <string.h>
size_t strxfrm( char *str1, const char *str2, size_t num );
copy 'num' bytes t str2 sang str1
41. tolower()
tn hm: tolower()
nh ngha:
PHP Code:
#include <ctype.h>
int tolower( int ch );
convert sang k t thng
PHP Code:
#include <stdio.h>
#include <ctype.h>
int main ()
{
char x='X';
x = tolower(x);
printf("sau khi convert: %c",x);
return 0;
}
42. toupper()
tn hm: toupper()
nh ngha:
PHP Code:
#include <ctype.h>
int toupper( int ch );
convert k t sang k t hoa
PHP Code:
#include <stdio.h>
#include <ctype.h>
int main ()
{
char x='x';
x = toupper(x);
printf("sau khi convert: %c",x);
Th Vien C Y!M: Information.Technology@yahoo.com.vn
Mr.V Kim Hiu 38
return 0;
}
3. Math
Cc hm lm vic lin quan n MATH
1.abs
2.acos
3.asin
4.tan
5.atan2
6.ceil
7.cos
8.cosh
9.div
10.exp
11.fabs
12.floor
13.fmod
14.frexp
15.labs
16.ldexp
17.ldiv
19.log
20.log10
21.modf
22.pow
23.sin
24.sinh
25.sqrt
26.tan
Th Vien C Y!M: Information.Technology@yahoo.com.vn
Mr.V Kim Hiu 39
27.tanh
1. abs()
tn hm: abs()
nh ngha:
PHP Code:
#include <stdlib.h>
int abs( int num );
tr v gi tr tuyt i ca 1 s
PHP Code:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int x=-10;
printf("Gia tri tuyet doi cua %d la %d",x,abs(x));
return 0;
}
2. acos()
tn hm: acos()
nh ngha:
PHP Code:
#include <math.h>
double acos( double arg );
tr v gi tr arc cosine ca 'arg' trong khong [0,PI], arg nm trong khong [-1,1]. Nu arg
ngoi gii hn cho php th acos() tr v gi tr NAN
PHP Code:
#include <stdio.h>
#include <math.h>
int main(void)
{
double x=-1;
printf("acos(%.2lf) = %.2lf",x,acos(x));
return 0;
}
3. asin()
tn hm: asin()
nh ngha:
PHP Code:
#include <math.h>
double asin( double arg );
hm tr v arc sine ca arg
Th Vien C Y!M: Information.Technology@yahoo.com.vn
Mr.V Kim Hiu 40
PHP Code:
#include <stdio.h>
#include <math.h>
int main(void)
{
double x=0;
printf("asin(%.2lf) = %.2lf",x,asin(x));
return 0;
}
4. atan()
tn hm: atan()
nh ngha:
PHP Code:
#include <math.h>
double atan( double arg );
Hm tr v arc tangent ca mt s
PHP Code:
#include <stdio.h>
#include <math.h>
int main(void)
{
double x=0;
printf("atan(%.2lf) = %.2lf",x,atan(x));
return 0;
}
5. atan2()
tn hm: atan2()
nh ngha:
PHP Code:
#include <math.h>
double atan2( double y, double x );
tr v arc tangent ca y/x
PHP Code:
#include <stdio.h>
#include <math.h>
int main(void)
{
double y=0;
double x=100;
printf("atan2(%.2lf/%.2lf) = %.2lf",y,x,atan(y/x));
return 0;
}
7. ceil()
Th Vien C Y!M: Information.Technology@yahoo.com.vn
Mr.V Kim Hiu 41
tn hm: ceil()
nh ngha:
PHP Code:
#include <math.h>
double ceil( double num );
tr v gi tr trn ca mt s
PHP Code:
#include <stdio.h>
#include <math.h>
int main(void)
{
double x = 1.3;
printf("ceil(%.2lf) = %.2lf",x,ceil(x));
return 0;
}
8. cos()
tn hm: cos()
nh ngha:
PHP Code:
#include <math.h>
double cos( double arg );
tr v gi tr cosine ca mt s
PHP Code:
#include <stdio.h>
#include <math.h>
int main(void)
{
double x = 0;
printf("cos(%.2lf) = %.2lf",x,cos(x));
return 0;
}
9. cosh()
tn hm: cosh()
nh ngha:
PHP Code:
#include <math.h>
double cosh( double arg );
tr v gi tr hyperbolic cosine ca mt s
PHP Code:
#include <stdio.h>
#include <math.h>
int main(void)
{
Th Vien C Y!M: Information.Technology@yahoo.com.vn
Mr.V Kim Hiu 42
double x = 0;
printf("cosh(%.2lf) = %.2lf",x,cosh(x));
return 0;
}
10.div()
tn hm: div()
nh ngha:
PHP Code:
#include <stdlib.h>
div_t div( int numerator, int denominator );
tr v phn nguyn v phn d ca mt php chia
PHP Code:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int x = 17;
int y = 4;
div_t result;
result = div(x,y);
printf("Phan nguyen: %d || Phan du: %d ",result.quot,result.rem);
return 0;
}
11. exp()
tn hm: exp()
nh ngha:
PHP Code:
#include <math.h>
double exp( double arg );
tr v ly tha ca e
PHP Code:
#include <stdio.h>
#include <math.h>
int main(void)
{
double x = 2;
printf("e^2 = %.2lf",exp(x));
return 0;
}
12. fabs()
tn hm: fabs()
nh ngha:
PHP Code:
Th Vien C Y!M: Information.Technology@yahoo.com.vn
Mr.V Kim Hiu 43
#include <math.h>
double fabs( double arg );
tr v gi tr tuyt i ca s thc
PHP Code:
#include <stdio.h>
#include <math.h>
int main(void)
{
double x = -2.2;
printf("|-2.2| = %.2lf",fabs(x));
return 0;
}
13. floor()
tn hm: floor()
nh ngha:
PHP Code:
#include <math.h>
double floor( double arg );
tr v gi tr sn ca mt s
PHP Code:
#include <stdio.h>
#include <math.h>
int main(void)
{
double x = -2.2;
printf("floor(%.2lf) = %.2lf",x,floor(x));
return 0;
}
14. fmod()
tn hm: fmod()
nh ngha:
PHP Code:
#include <math.h>
double fmod( double x, double y );
tr v phn d ca php chia: x/y
PHP Code:
#include <stdio.h>
#include <math.h>
int main(void)
{
double x = 2.2;
double y = 1.1;
Th Vien C Y!M: Information.Technology@yahoo.com.vn
Mr.V Kim Hiu 44
printf("%.2lf/%.2lf du %.2lf",x,y,fmod(x,y));
return 0;
}
15. frexp()
tn hm: frexp()
nh ngha:
PHP Code:
#include <math.h>
double frexp( double num, int* exp );
hm ny dng vi phng trnh: num = result * ( 2 ^ exp );
hm tr v result:
PHP Code:
#include <stdio.h>
#include <math.h>
int main(void)
{
double x = 2.2;
int exp;
double result;
result = frexp(x,&exp);
printf(" %f = %f * ( 2 ^ %d ) ", x, result,exp);
return 0;
}
16. labs()
tn hm: labs()
nh ngha:
PHP Code:
#include <stdlib.h>
long labs( long num );
tr v gi tr tuyt i ca mt s kiu long integer
PHP Code:
#include <stdio.h>
#include <math.h>
int main(void)
{
long x = -3333333;
printf(" |%ld| = %ld " , x , labs(x));
return 0;
}
17. ldexp()
tn hm: ldexp()
nh ngha:
PHP Code:
Th Vien C Y!M: Information.Technology@yahoo.com.vn
Mr.V Kim Hiu 45
#include <math.h>
double ldexp( double num, int exp );
hm tr v gi tr ca php ton: num * (2 ^ exp)
PHP Code:
#include <stdio.h>
#include <math.h>
int main(void)
{
double num = 3.2;
int exp = 2;
printf(" Ket qua la : %.2lf ", ldexp(num,exp));
return 0;
}
18. ldiv()
tn hm: ldiv()
nh ngha:
PHP Code:
#include <stdlib.h>
ldiv_t ldiv( long numerator, long denominator );
tr v phn nguyn v d ca s kiu long
PHP Code:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
long x = 30;
long y = 17;
ldiv_t result = ldiv(x,y);
printf(" Phan nguyen = %ld || Phan du = %ld ",result.quot, result.r
em);
return 0;
}
19. log()
tn hm: log()
nh ngha:
PHP Code:
#include <math.h>
double log( double num );
tr v logarit t nhin ca num
PHP Code:
#include <stdio.h>
#include <math.h>
int main(void)
Th Vien C Y!M: Information.Technology@yahoo.com.vn
Mr.V Kim Hiu 46
{
double num = 2;
printf(" log(%.2lf) = %.2lf " , num, log(num));
return 0;
}
20. log10()
tn hm: log10()
nh ngha:
PHP Code:
#include <math.h>
double log10( double num );
tr v logarit c s 10 ca num
PHP Code:
#include <stdio.h>
#include <math.h>
int main(void)
{
double num = 2;
printf(" log10(%.2lf) = %.2lf " , num, log10(num));
return 0;
}
21. modf()
tn hm: modf()
nh ngha:
PHP Code:
#include <math.h>
double modf( double num, double *i );
tch phn nguyn v thp phn ca mt s thp phn
hm tr v phn thp phn
PHP Code:
#include <stdio.h>
#include <math.h>
int main(void)
{
double num = 1.17;
double intpart;
double fractpart = modf(num,&intpart);
printf(" %lf = %lf + %lf " , num,intpart,fractpart);
return 0;
}
22. pow()
Th Vien C Y!M: Information.Technology@yahoo.com.vn
Mr.V Kim Hiu 47
tn hm: pow()
nh ngha:
PHP Code:
#include <math.h>
double pow( double base, double exp );
tnh ly tha ca mt s
PHP Code:
#include <stdio.h>
#include <math.h>
int main(void)
{
double base = 2;
double exp = 3;
printf(" %lf^%lf = %lf ", base,exp,pow(base,exp));
return 0;
}
23. sin()
tn hm: sin()
nh ngha:
PHP Code:
#include <math.h>
double sin( double arg );
tr v sin ca 'arg'
PHP Code:
#include <stdio.h>
#include <math.h>
int main(void)
{
double x = 0;
printf(" sin(%.1lf) = %lf ",x,sin(x));
return 0;
}
24. sinh()
tn hm: sinh()
nh ngha:
PHP Code:
#include <math.h>
double sinh( double arg );
tr v hyperbolic sine ca 'arg'
PHP Code:
#include <stdio.h>
#include <math.h>
int main(void)
Th Vien C Y!M: Information.Technology@yahoo.com.vn
Mr.V Kim Hiu 48
{
double x = 0;
printf(" sinh(%.1lf) = %lf ",x,sinh(x));
return 0;
}
25. sqrt()
tn hm: sqrt()
nh ngha:
PHP Code:
#include <math.h>
double sqrt( double num );
tr v cn bc 2 ca mt s
PHP Code:
#include <stdio.h>
#include <math.h>
int main(void)
{
double x = 4;
printf(" sqrt(%.1lf) = %lf ",x,sqrt(x));
return 0;
}
26. tan()
tn hm: tan()
nh ngha:
PHP Code:
#include <math.h>
double tan( double arg );
tr v tangent ca 'arg'
PHP Code:
#include <stdio.h>
#include <math.h>
int main(void)
{
double x = 4;
printf(" tan(%.1lf) = %lf ",x,tan(x));
return 0;
}
27. tanh()
tn hm: tanh()
nh ngha:
PHP Code:
#include <math.h>
double tanh( double arg );
tr v hyperbolic tangent ca 'arg'
Th Vien C Y!M: Information.Technology@yahoo.com.vn
Mr.V Kim Hiu 49
PHP Code:
#include <stdio.h>
#include <math.h>
int main(void)
{
double x = 4;
printf(" tanh(%.1lf) = %lf ",x,tanh(x));
return 0;
}
4. Date & Time
Cc hm x l Date & time
1.asctime
2.clock
3.ctime
4.difftime
5.gmtime
6.localtime
7.mktime
8.setlocale
9.strftime
10.time
1. asctime()
tn hm: asctime()
nh ngha:
PHP Code:
#include <time.h>
char *asctime( const struct tm *ptr );
convert cu trc 'ptr'(pointer to time struct) thnh chui thi gian c dng
Code:
day month date hours:minutes:seconds year
v d:
Code:
Mon Jun 26 12:03:53 2000
v d
PHP Code:
#include <stdio.h>
#include <time.h>
Th Vien C Y!M: Information.Technology@yahoo.com.vn
Mr.V Kim Hiu 50
int main(void)
{
time_t rawtime;
struct tm *timeinfo;
time(&rawtime);
timeinfo = localtime(&rawtime);
printf("The current date/time is: %s",asctime(timeinfo));
return 0;
}
2. clock()
tn hm: clock()
nh ngha:
PHP Code:
#include <ctime>
clock_t clock( void );
tr v s tick ng h k t khi bt u chng trnh
v d
PHP Code:
#include <stdio.h>
#include <time.h>
void wait(int sec)
{
clock_t endwait;
endwait = clock() + sec * CLOCKS_PER_SEC;
while(clock() < endwait) {}
}
int main(void)
{
int cnt = 0;
printf("I'm sleeping for 5 seconds...\n");
for(; cnt < 5; cnt++) {
printf("%d\n",cnt);
wait(1);
}
printf("Wake up NOW !");
return 0;
}
3. ctime()
tn hm: ctime()
nh ngha:
PHP Code:
#include <time.h>
char *ctime( const time_t *time );
ly thi gian di dng
PHP Code:
Th Vien C Y!M: Information.Technology@yahoo.com.vn
Mr.V Kim Hiu 51
day month date hours:minutes:seconds year
tng ng vi asctime(localtime(&timeptr))
v d
PHP Code:
#include <stdio.h>
#include <time.h>
int main(void)
{
time_t rawtime;
time(&rawtime);
printf("Time now is: %s",ctime(&rawtime));
return 0;
}
4. difftime()
tn hm: difftime()
nh ngha:
PHP Code:
#include <time.h>
double difftime( time_t time2, time_t time1 );
Tr v s giy so snh bng giy
5. gmtime()
tn hm: gmtime()
nh ngha:
PHP Code:
#include <time.h>
struct tm *gmtime( const time_t *time );
tr v pointer ti GMT
v d ly gi H Ni GMT +7 [ICT]
PHP Code:
#include <stdio.h>
#include <time.h>
#define ICT (+7)
int main(void)
{
struct tm *gmt;
time_t raw;
time(&raw);
gmt = gmtime(&raw);
printf("GMT +7:00 | %2d:%2d ",(gmt->tm_hour+ICT)%24,gmt-
>tm_min);
Th Vien C Y!M: Information.Technology@yahoo.com.vn
Mr.V Kim Hiu 52
return 0;
}
6. localtime()
tn hm: localtime()
nh ngha:
PHP Code:
#include <time.h>
struct tm *localtime( const time_t *time );
tr v local time
PHP Code:
#include <stdio.h>
#include <time.h>
int main(void)
{
time_t rawtime;
struct tm *local;
time(&rawtime);
local = localtime(&rawtime);
printf("Local time: %s",asctime(local));
return 0;
}
7. mktime()
tn hm: mktime()
nh ngha:
PHP Code:
#include <time.h>
time_t mktime( struct tm *time );
tr v calendar time tng ng vi localtime, nu tht bi th tr v -1
v d: kim tra xem ngy 17 thng 1 nm 1987 l th my
PHP Code:
#include <stdio.h>
#include <time.h>
int main(void)
{
time_t rawtime;
struct tm *info;
char *weekday[] = { "Sunday", "Monday",
"Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday"};
time(&rawtime);
info = localtime(&rawtime);
info->tm_year = 1987 - 1900;
Th Vien C Y!M: Information.Technology@yahoo.com.vn
Mr.V Kim Hiu 53
info->tm_mon = 1;
info->tm_mday = 17;
mktime(info);
printf("That weekday is %s", weekday[info->tm_wday]);
return 0;
}
8.setlocale
8. strftime()
tn hm: strftime()
nh ngha:
PHP Code:
#include <time.h>
size_t strftime( char *str, size_t maxsize, const char *fmt, struct tm
*time );
convert time format t time sang str vi kiu format 'fmt' v buffer size 'maxsize'
v d ly thi gian hin ti
PHP Code:
#include <stdio.h>
#include <time.h>
int main(void)
{
time_t rawtime;
struct tm *now;
char buffer[80];
time(&rawtime);
now = localtime(&rawtime);
strftime(buffer,80,"time now is %I:%M %p",now);
puts(buffer);
return 0;
}
9. time()
tn hm: time()
nh ngha:
PHP Code:
#include <time.h>
time_t time( time_t *time );
ly calendar thi gian hin ti (thi im t 01-Jan-1970)
nu tht bi tr v -1
8. strftime()
tn hm: strftime()
nh ngha:
PHP Code:
Th Vien C Y!M: Information.Technology@yahoo.com.vn
Mr.V Kim Hiu 54
#include <time.h>
size_t strftime( char *str, size_t maxsize, const char *fmt, struct tm
*time );
convert time format t time sang str vi kiu format 'fmt' v buffer size 'maxsize'
v d ly thi gian hin ti
PHP Code:
#include <stdio.h>
#include <time.h>
int main(void)
{
time_t rawtime;
struct tm *now;
char buffer[80];
time(&rawtime);
now = localtime(&rawtime);
strftime(buffer,80,"time now is %I:%M %p",now);
puts(buffer);
return 0;
}
10. time()
tn hm: time()
nh ngha:
PHP Code:
#include <time.h>
time_t time( time_t *time );
ly calendar thi gian hin ti (thi im t 01-Jan-1970)
nu tht bi tr v -1
5. Memory
Cc hm s dng vi Memory
1. calloc
2. free
3. malloc
4. realloc
1. Calloc()
tn hm : calloc()
nh ngha:
PHP Code:
#include <stdlib.h>
void* calloc( size_t num, size_t size );
cung cp khong trng cho mng trong memory. thnh cng th gi tr khi to l 0. tht bi tr
v NULL
Th Vien C Y!M: Information.Technology@yahoo.com.vn
Mr.V Kim Hiu 55
PHP Code:
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
int cnt = 0;
int *data;
data = (int*) calloc(4,sizeof(int));
if(data == NULL) exit(1);
for(; cnt < 4; cnt++)
data[cnt] = cnt;
for(cnt = 0; cnt < 4; cnt++) {
printf(" %d",data[cnt]);
}
free(data);
return 0;
}
2. free()
tn hm: free()
nh ngha:
PHP Code:
#include <stdlib.h>
void free( void* ptr );
gii phng b nh sau khi cp pht
3. malloc()
tn hm: malloc()
nh ngha:
PHP Code:
#include <stdlib.h>
void *malloc( size_t size );
cp pht b nh, NULL nu tht bi
PHP Code:
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
char *buffer;
int cnt = 0;
buffer = (char*) malloc(10);
if(buffer == NULL) exit(1);
for(;cnt < 9; cnt++)
buffer[cnt] = rand()%26 + 'a';
buffer[10] = '\0';
printf("Random string: %s\n",buffer);
Th Vien C Y!M: Information.Technology@yahoo.com.vn
Mr.V Kim Hiu 56
return 0;
}
4. realloc()
tn hm: realloc()
nh ngha:
PHP Code:
#include <stdlib.h>
void *realloc( void *ptr, size_t size );
cung cp li b nh cho con tr no , thnh cng th tr v khong trng mi cp pht, NULL
nu tht bi
6. Other Standard Functions
Danh sch cc hm chun cn li:
1. abort
2. assert
3. atexit
4. bsearch
5. exit
6. getenv
7. longjmp
8. qsort
9. raise
10. rand
11. setjmp
12. signal
13. srand
14. system
15. va_arg
__________________
1. abort()
Tn hm: abort()
nh ngha:
PHP Code:
Th Vien C Y!M: Information.Technology@yahoo.com.vn
Mr.V Kim Hiu 57
#include <stdlib.b>
void abort(void);
Dng abort process hin ti.
S tr v tn hiu SIGABRT cho host environment trong POSIX-platforms, nh ngha trong
signal.h (*nix-like system).
PHP Code:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
FILE *pfile;
pfile = fopen("file.txt","r");
if(pfile == NULL)
{
fputs("error opening file\n",stderr);
abort();
}
fclose(pfile);
return 0;
}
Thn: Mr.Hiu (ID:freetime719)
[o "t - -o-- - |"- /o
U"oas -o ta "a" "o- - -o- --a o-as! "|<!