Thö Vieän C Y!M:
Mr.Vũ Kim Hiếu 1
Thư Viện C
Hi! Đây là Thư viện các hàm trong C căn bản mà mình sưu tầm được! nó rất hay và hữu
ích đó các bạn. Hi vọng bài viết này sẽ giúp ích cho các bạn trong việc học lập trình C. Thân!
Mr.Vũ Hiếu.
1. In/Out
Danh sách các hàm sử dụng 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ö Vieän C Y!M:
Mr.Vũ Kim Hiếu 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
Hàm clearerr
Khai báo :
Trích dẫn:
void clearerr(FILE *stream);
Xoá tín hiệu lỗi gây ra bởi hệ thống khi thao tác trên tập tin gặp lỗi .
Example
Code:
#include <stdio.h>
int main(void)
{
FILE *fp;
char ch;
Thö Vieän C Y!M:
Mr.Vũ Kim Hiếu 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;
}
Hàm fclose
Khai báo :
Trích dẫn:
int fclose(FILE *stream);
Đóng tệp lại sau khi thao tác , stream là tên con trỏ tệp . Khi thành công hàm trả về 0 , trái lại
trả về EOF ( hằng chỉ thị kết thúc tệp )
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;
}
Hàm feof
Khai báo :
Trích dẫn:
Thö Vieän C Y!M:
Mr.Vũ Kim Hiếu 4
int feof(FILE *stream);
Hàm dùng để kiểm tra con trỏ tệp có đến cuối tệp hay chưa . Nếu nó đến cuối tệp thì hàm trả về
giá trị khác 0 , ngược lại 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;
}
Hàm ferror
Khai báo :
Trích dẫn:
int ferror(FILE *stream);
Kiểm tra lỗi thao tác trên tệp . Hàm trả về giá trị khác 0 nếu có lỗi , ngược lại 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ö Vieän C Y!M:
Mr.Vũ Kim Hiếu 5
/* xoa tin hieu loi*/
clearerr(stream);
}
fclose(stream);
return 0;
}
Hàm fflush
Khai báo :
Trích dẫn:
int fflush(FILE *stream);
Hàm dùng làm sạch vùng đệm của tệp . Nếu thành công hàm cho giá trị 0 , trái lại hàm 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ö Vieän C Y!M:
Mr.Vũ Kim Hiếu 6
duphandle = dup(fileno(stream));
close(duphandle);
}
Hàm fgetc
Khai báo :
Trích dẫn:
int fgetc(FILE *stream);
Hàm dùng lấy 1 ký tự từ tệp do con trỏ tệp stream trỏ đến . Nếu thành công hàm trả về mã
ASCII của ký tự đọc được . Nếu gặp lỗi hay gặp kết thúc tệp thì hàm 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;
}
Hàm fgetpos
Khai báo :
Trích dẫn:
Thö Vieän C Y!M:
Mr.Vũ Kim Hiếu 7
int fgetpos( FILE *stream,fpos_t *pos )
Hàm dùng lấy vị trí con trỏ tệp ( ở byte thứ mấy ) lưu vào trong biến pos . Nếu thành công hàm
trả về 0 , ngược lại hàm trả về giá trị khác 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);
}
Hàm fgets
Khai báo :
Trích dẫn:
Thö Vieän C Y!M:
Mr.Vũ Kim Hiếu 8
char *fgets(char *s,int n,FILE *fp);
Hàm dùng để đọc một chuỗi ký tự dài n-1 ký tự từ tệp fp vào chuỗi s . Việc đọc kết thúc khi đã
đọc đủ n-1 ký tự hay gặp dấu xuống dòng hay gặp kết thúc tệp . Sau khi đọc , chuỗi s sẽ được
tự động thêm vào ký tự NULL vào cuối chuỗi . Hàm trả về điạ chỉ chuỗi s nếu thành công , trái lại
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;
}
Hàm fopen
Khai báo :
Trích dẫn:
FILE *fopen(char *filename,char *type );
Hàm dùng để mở tệp . Với filename là chuỗi chứa tên tệp cần mở ( bao gồm cả đường dẫn cụ
thể ) . Lưu ý một chút với filename này . Nếu bạn nhập chuỗi từ bàn phím thì khác với bạn đánh
sẵn nó trong code của bạn . Ví dụ tệp ta cần mở tên là abc.jpg nằm ở ổ C . Đối với đánh tên tệp
từ bàn phím thì ta đánh như sau : C:\abc.jpg . Nhưng nếu ta soạn sẵn trong code thì ta soạn
như sau : C:\\abc.jpg ( tức là thêm 1 gạch \ ) .
Kiểu mở tệp có rất nhiều cách nhưng chung quy ta có các trường hợp sau cho kiểu văn bản :
"r" : đọc
"w" : ghi
"r+" hay "w+" : đọc và ghi
"a" : ghi bổ sung
"a+" : đọc ghi bổ sung
Thö Vieän C Y!M:
Mr.Vũ Kim Hiếu 9
Đối với mở file nhị phân bạn chỉ cần thêm chữ b vào sau các chuỗi trên .
Nếu việc mở tệp thành công hàm cho địa chỉ con trỏ tệp , trái lại hàm 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;
}
Hàm fprintf
Khai báo :
Trích dẫn:
int fprintf(FILE *fp,char *s, );
Hàm này chức năng y chang như hàm printf nhưng thay vì ghi ra màn hình thì nó ghi lên tệp fp .
Nếu thành công hàm trả về số byte ghi được lên tệp , trái lại hàm trả về EOF .
Example
Code:
#include <stdio.h>
int main(void)
{
FILE *stream;
int i = 100;
char c = 'C';
float f = 1.234;
Thö Vieän C Y!M:
Mr.Vũ Kim Hiếu 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;
}
Hàm fputc
Khai báo :
Trích dẫn:
int fputc(int ch,FILE *fp);
Hàm ghi một ký tự có mã ASCII là ch lên tệp fp . khi thành công trả về mã ASCII của
ký tự được ghi , trái lại hàm 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;
}
Hàm fputs
Khai báo :
Trích dẫn:
int fputs(char *s,FILE *fp);
Hàm ghi lên tệp fp chuỗi s . Chú ý rằng chuỗi s nên kết thúc bằng ký tự NULL , nếu ko việc ghi
lên có thể bị sai lệch . Ký tự NULL này để chỉ ra vị trí kết thúc tệp cho rõ ràng chứ nó ko được ghi
lên tệp . Hàm trả về ký tự cuối cùng được ghi lên tệp nếu thành công , trái lại hàm cho EOF .
Example
Code:
Thö Vieän C Y!M:
Mr.Vũ Kim Hiếu 11
#include <stdio.h>
int main(void)
{
/* ghi mot chuoi ra dong xuat chuan ( man hinh ) */
fputs("Hello world\n", stdout);
return 0;
}
Hàm fread
Khai báo :
Trích dẫn:
int fread(void *ptr,int size,int n,FILE *fp);
Hàm đọc n mẫu tin , mỗi mẫu có kích thước size từ tệp fp lưu vào trong vùng nhớ ptr . Hàm trả
về một giá trị bằng số mẫu tin thực sự đọc được . Nếu gặp lỗi hay gặp kết thúc tệp hàm 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ö Vieän C Y!M:
Mr.Vũ Kim Hiếu 12
Hàm freopen
Khai báo :
Trích dẫn:
FILE *freopen(char *filename,char *type,FILE *fp);
Hàm này có chức năng gán con trỏ tệp fp vào tệp filename . Sau đó có thể mở đọc ghi trên tệp
này thông qua con trỏ tệp fp . Hàm này đặc biệt có ích khi ta muốn gán các con trỏ tệp chuẩn (
stdin , stdout , stderr ) vào 1 tệp của ta . Hàm trả về NULL nếu gặp lỗi .
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;
}
Hàm fscanf
Khai báo :
Trích dẫn:
int fscanf(FILE *fp,char *s, );
Hàm có chức năng đọc dữ liệu từ tệp . Làm việc giống như hàm scanf nhưng thay vì đọc từ màn
hình thì hàm fscanf đọc từ tệp thôi . Hàm trả về 1 giá trị bằng số trường đọ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ö Vieän C Y!M:
Mr.Vũ Kim Hiếu 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;
}
Hàm fseek
Khai báo :
Trích dẫn:
int fseek(FILE *fp,long sb,int xp);
Hàm dùng di chuyển con trỏ tệp fp đến vị trí bất kỳ . Với sb là số byte cần di chuyển , xp là vị trí
xuất phát . Nếu sb dương tức là di chuyển về cuối tệp , nếu âm thì di chuyển về đầu tệp . Nếu xp
là 0 hay SEEK_SET tức là xuất phát từ đầu tệp , nếu là SEEK_CUR hay 1 tức là xuất phát tại vị trí
hiện thời của con trỏ tệp , nếu là SEEK_END hay 2 tức là xuất phát tại cuối tệp .
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ö Vieän C Y!M:
Mr.Vũ Kim Hiếu 14
Hàm fsetpos
Khai báo :
Trích dẫn:
int fsetpos(FILE *stream , fpos_t pos);
Hàm này thường đi kèm cùng hàm fgetpos . Hàm dùng để di chuyển con trỏ tệp đến vị trí đươc
lưu trong con trỏ pos . Vị trí này do hàm fgetpos lưu lại từ trước . Hàm trả về 0 nếu thành công ,
ngược lại trả về giá trị khác 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ö Vieän C Y!M:
Mr.Vũ Kim Hiếu 15
}
Hàm ftell
Khai báo :
Trích dẫn:
long ftell(FILE *fp);
Khi thành công hàm cho viết vị trí con trỏ tệp hiện thời đang ở vị trí thứ mấy ( tính từ đầu tệp ) ,
số hiệu của byte tính từ 0 . Ngược lại hàm trả về -1L . Nói thêm 1 tí về -1L có nghĩa là -1 mà lưu
dưới dạng long ( dùng 4 byte để lưu ) .
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;
}
Hàm fwrite
Khai báo :
Trích dẫn:
int fwrite( void *ptr,int size,int n,FILE *fp );
Hàm dùng để ghi n mẫu tin kích thước size byte từ vùng nhớ ptr lên tệp fp . Hàm trả về 1 giá trị
bằng số mẫu tin thực sự đọc được .
Example
Code:
#include <stdio.h>
struct mystruct
{
int i;
char ch;
};
int main(void)
{
FILE *stream;
struct mystruct s;
Thö Vieän C Y!M:
Mr.Vũ Kim Hiếu 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); /* viết cấu trúc s lên tep */
fclose(stream); /* dong tep */
return 0;
}
2. String & Character
Danh sách các hàm với 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ö Vieän C Y!M:
Mr.Vũ Kim Hiếu 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()
Tên hàm: atof()
Định nghĩa:
PHP Code:
Thö Vieän C Y!M:
Mr.Vũ Kim Hiếu 18
#include <stdlib.h>
double atof( const char *str );
Chuỗi 'str' phải bắt đầu bằng kí tự số, nếu không sẽ bị lỗi chương trình (termination)
Ví dụ về 3 trường hợp
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()
Tên hàm: atoi()
Định nghĩa:
PHP Code:
#include <cstdlib>
int atoi( const char *str );
'str' phải bắt đầu bằng số hoặc kí tự trắng <space> nếu không convert sẽ fail và trả về 0.
Code minh họa
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ö Vieän C Y!M:
Mr.Vũ Kim Hiếu 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()
Tên hàm: atol()
Định nghĩa:
PHP Code:
#include <stdlib.h>
long atol( const char *str );
Tương tự như atoi()
Minh họa
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()
Tên hàm: isalnum()
Định nghĩa:
PHP Code:
#include <ctype.h>
int isalnum( int ch );
Kiểm tra một kí tự là số hay chữ cái.
Nếu đúng trả về giá trị khác 0
Nếu sai trả về 0
PHP Code:
#include <stdio.h>
#include <ctype.h>
Thö Vieän C Y!M:
Mr.Vũ Kim Hiếu 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()
Tên hàm: isalpha()
Định nghĩa:
PHP Code:
#include <ctype.h>
int isalpha( int ch );
Kiểm tra một kí tự có phải là chữ cái hay không.
Đúng thì trả về khác 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()
Tên hàm: iscntrl()
Định nghĩa:
PHP Code:
#include <ctype.h>
int iscntrl( int ch );
Kiểm tra một kí tự có thuộc nhóm kí tự điều khiển hay không
Nhóm kí tự điều khiển (Control Character) nằm từ : 0x00 đến 0x1F và kí tự 0x7F
Đúng thì trả về khác 0
Sai thì trả về 0
Thö Vieän C Y!M:
Mr.Vũ Kim Hiếu 21
Xem bảng 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()
Tên hàm: isdigit()
Định nghĩa:
Thö Vieän C Y!M:
Mr.Vũ Kim Hiếu 22
PHP Code:
#include <ctype.h>
int isdigit( int ch );
Kiểm tra 1 kí tự có phải là kí tự số hay không
Đúng thì trả về khác 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()
Tên hàm: isgraph()
Định nghĩa:
PHP Code:
#include <ctype.h>
int isgraph( int ch );
Bất cứ kí tự nào có thể in ra được (printable character) đều gọi là Graphical Character, ngoại trừ
kí tự <space>
Kiểm tra xem kí tự có thuộc nhóm graph không
Đúng thì trả về khác 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ö Vieän C Y!M:
Mr.Vũ Kim Hiếu 23
Tên hàm: islower()
Định nghĩa
PHP Code:
#include <ctype.h>
int islower( int ch );
Kiểm tra một kí tự xem có phải chữ in thường không
Đúng thì trả về khác 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()
Tên hàm: isprint()
Định nghĩa:
PHP Code:
#include <ctype.h>
int isprint( int ch );
Printable character bao gồm Graphical Character và kí tự trắng <space>
kiểm tra 1 kí tự có phải là printable character hay không
Đúng thì trả về khác 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ö Vieän C Y!M:
Mr.Vũ Kim Hiếu 24
Tên hàm: ispunct()
Định nghĩa:
PHP Code:
#include <ctype.h>
int ispunct( int ch );
Punctuation Character là các kí tự trong nhóm Graphical Character nhưng không phải là chữ cái
hay số
Đúng thì trả về khác 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()
Tên hàm: isspace()
Định nghĩa:
PHP Code:
#include <ctype.h>
int isspace( int ch );
Space Character là những kí tự tạo nên khoảng trắng (line feed, form feed )
Đúng thì trả về khác 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ö Vieän C Y!M:
Mr.Vũ Kim Hiếu 25
13. isupper()
Tên hàm: isupper()
Định nghĩa:
PHP Code:
#include <ctype.h>
int isupper( int ch );
Kiểm tra một kí tự có phải chữ in Hoa hay không
Đúng thì trả về khác 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()
Tên hàm: isxdigit()
Định nghĩa:
PHP Code:
#include <ctype.h>
int isxdigit( int ch );
Kiểm tra xem một kí tự có phải là số ở hệ thập lục (Hex - 16) hay không
Đúng thì trả về khác 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()