1、C语言函数大全楼主 Posted:2008-4-16 14:14:09222.240.188.229信息 日志 短讯 邮箱 好友 搜索 引用 回复 C语言函数大全 c函数名: cabs功 能: 计算复数的绝对值用 法: double cabs(struct complex z);程序例: #include #include int main(void) struct complex z; double val; z.x = 2.0; z.y = 1.0; val = cabs(z); printf(The absolute value of %.2lfi %.2lfj is %.2lf, z.
2、x, z.y, val); return 0;函数名: calloc功 能: 分配主存储器用 法: void *calloc(size_t nelem, size_t elsize);程序例:#include #include int main(void) char *str = NULL; /* allocate memory for string */ str = calloc(10, sizeof(char); /* copy Hello into string */ strcpy(str, Hello); /* display string */ printf(String is %s
3、n, str); /* free memory */ free(str); return 0;函数名: ceil功 能: 向上舍入用 法: double ceil(double x);程序例:#include #include int main(void) double number = 123.54; double down, up; down = floor(number); up = ceil(number); printf(original number %5.2lfn, number); printf(number rounded down %5.2lfn, down); print
4、f(number rounded up %5.2lfn, up); return 0;函数名: cgets功 能: 从控制台读字符串用 法: char *cgets(char *str);程序例:#include #include int main(void) char buffer83; char *p; /* Theres space for 80 characters plus the NULL terminator */ buffer0 = 81; printf(Input some chars:); p = cgets(buffer); printf(ncgets read %d c
5、haracters: %sn, buffer1, p); printf(The returned pointer is %p, buffer0 is at %pn, p, &buffer); /* Leave room for 5 characters plus the NULL terminator */ buffer0 = 6; printf(Input some chars:); p = cgets(buffer); printf(ncgets read %d characters: %sn, buffer1, p); printf(The returned pointer is %p,
6、 buffer0 is at %pn, p, &buffer); return 0;函数名: chdir功 能: 改变工作目录用 法: int chdir(const char *path);程序例:#include #include #include char old_dirMAXDIR;char new_dirMAXDIR;int main(void) if (getcurdir(0, old_dir) perror(getcurdir(); exit(1); printf(Current directory is: %sn, old_dir); if (chdir() perror(ch
7、dir(); exit(1); if (getcurdir(0, new_dir) perror(getcurdir(); exit(1); printf(Current directory is now: %sn, new_dir); printf(nChanging back to orignal directory: %sn, old_dir); if (chdir(old_dir) perror(chdir(); exit(1); return 0;函数名: _chmod, chmod功 能: 改变文件的访问方式用 法: int chmod(const char *filename,
8、int permiss);程序例:#include #include #include void make_read_only(char *filename);int main(void) make_read_only(NOTEXIST.FIL); make_read_only(MYFILE.FIL); return 0;void make_read_only(char *filename) int stat; stat = chmod(filename, S_IREAD); if (stat) printf(Couldnt make %s read-onlyn, filename); els
9、e printf(Made %s read-onlyn, filename);函数名: chsize功 能: 改变文件大小用 法: int chsize(int handle, long size);程序例:#include #include #include int main(void) int handle; char buf11 = 0123456789; /* create text file containing 10 bytes */ handle = open(DUMMY.FIL, O_CREAT); write(handle, buf, strlen(buf); /* trun
10、cate the file to 5 bytes in size */ chsize(handle, 5); /* close the file */ close(handle); return 0;函数名: circle功 能: 在给定半径以(x, y)为圆心画圆用 法: void far circle(int x, int y, int radius);程序例:#include #include #include #include int main(void) /* request auto detection */ int gdriver = DETECT, gmode, errorco
11、de; int midx, midy; int radius = 100; /* initialize graphics and local variables */ initgraph(&gdriver, &gmode, ); /* read result of initialization */ errorcode = graphresult(); if (errorcode != grOk) /* an error occurred */ printf(Graphics error: %sn, grapherrormsg(errorcode); printf(Press any key
12、to halt:); getch(); exit(1); /* terminate with an error code */ midx = getmaxx() / 2; midy = getmaxy() / 2; setcolor(getmaxcolor(); /* draw the circle */ circle(midx, midy, radius); /* clean up */ getch(); closegraph(); return 0;函数名: cleardevice功 能: 清除图形屏幕用 法: void far cleardevice(void);程序例:#include
13、 #include #include #include int main(void) /* request auto detection */ int gdriver = DETECT, gmode, errorcode; int midx, midy; /* initialize graphics and local variables */ initgraph(&gdriver, &gmode, ); /* read result of initialization */ errorcode = graphresult(); if (errorcode != grOk) /* an err
14、or occurred */ printf(Graphics error: %sn, grapherrormsg(errorcode); printf(Press any key to halt:); getch(); exit(1); /* terminate with an error code */ midx = getmaxx() / 2; midy = getmaxy() / 2; setcolor(getmaxcolor(); /* for centering screen messages */ settextjustify(CENTER_TEXT, CENTER_TEXT);
15、/* output a message to the screen */ outtextxy(midx, midy, press any key to clear the screen:); /* wait for a key */ getch(); /* clear the screen */ cleardevice(); /* output another message */ outtextxy(midx, midy, press any key to quit:); /* clean up */ getch(); closegraph(); return 0;函数名: clearerr
16、功 能: 复位错误标志用 法:void clearerr(FILE *stream);程序例:#include int main(void) FILE *fp; char ch; /* open a file for writing */ fp = fopen(DUMMY.FIL, w); /* force an error condition by attempting to read */ ch = fgetc(fp); printf(%cn,ch); if (ferror(fp) /* display an error message */ printf(Error reading fr
17、om DUMMY.FILn); /* reset the error and EOF indicators */ clearerr(fp); fclose(fp); return 0;函数名: clearviewport功 能: 清除图形视区用 法: void far clearviewport(void);程序例:#include #include #include #include #define CLIP_ON 1 /* activates clipping in viewport */int main(void) /* request auto detection */ int gdr
18、iver = DETECT, gmode, errorcode; int ht; /* initialize graphics and local variables */ initgraph(&gdriver, &gmode, ); /* read result of initialization */ errorcode = graphresult(); if (errorcode != grOk) /* an error occurred */ printf(Graphics error: %sn, grapherrormsg(errorcode); printf(Press any k
19、ey to halt:); getch(); exit(1); /* terminate with an error code */ setcolor(getmaxcolor(); ht = textheight(W); /* message in default full-screen viewport */ outtextxy(0, 0, * - (0, 0) in default viewport); /* create a smaller viewport */ setviewport(50, 50, getmaxx()-50, getmaxy()-50, CLIP_ON); /* d
20、isplay some messages */ outtextxy(0, 0, * - (0, 0) in smaller viewport); outtextxy(0, 2*ht, Press any key to clear viewport:); /* wait for a key */ getch(); /* clear the viewport */ clearviewport(); /* output another message */ outtextxy(0, 0, Press any key to quit:); /* clean up */ getch(); closegr
21、aph(); return 0;函数名: _close, close功 能: 关闭文件句柄用 法: int close(int handle);程序例:#include #include #include #include main() int handle; char buf11 = 0123456789; /* create a file containing 10 bytes */ handle = open(NEW.FIL, O_CREAT); if (handle -1) write(handle, buf, strlen(buf); /* close the file */ clo
22、se(handle); else printf(Error opening filen); return 0;函数名: clock功 能: 确定处理器时间用 法: clock_t clock(void);程序例:#include #include #include int main(void) clock_t start, end; start = clock(); delay(2000); end = clock(); printf(The time was: %fn, (end - start) / CLK_TCK); return 0;函数名: closegraph功 能: 关闭图形系统
23、用 法: void far closegraph(void);程序例:#include #include #include #include int main(void) /* request auto detection */ int gdriver = DETECT, gmode, errorcode; int x, y; /* initialize graphics mode */ initgraph(&gdriver, &gmode, ); /* read result of initialization */ errorcode = graphresult(); if (errorc
24、ode != grOk) /* an error occurred */ printf(Graphics error: %sn, grapherrormsg(errorcode); printf(Press any key to halt:); getch(); exit(1); /* terminate with an error code */ x = getmaxx() / 2; y = getmaxy() / 2; /* output a message */ settextjustify(CENTER_TEXT, CENTER_TEXT); outtextxy(x, y, Press
25、 a key to close the graphics system:); /* wait for a key */ getch(); /* closes down the graphics system */ closegraph(); printf(Were now back in text mode.n); printf(Press any key to halt:); getch(); return 0;函数名: clreol功 能: 在文本窗口中清除字符到行末用 法: void clreol(void);程序例:#include int main(void) clrscr(); cprintf(The function CLREOL clears all characters from thern); cprintf(cursor position to the end of the line within thern); cprintf(current text window, without moving the cursor.rn); cprintf(Press any key to continue . . .); gotoxy(14, 4); getch(); clreol();
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1