⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fc.htm

📁 C语言编程宝典.rar,学习C语言的经典初级教程
💻 HTM
📖 第 1 页 / 共 2 页
字号:
<HTML>
<HEAD>
   <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=gb2312">
   <META NAME="Author" CONTENT="wdg">
   <META NAME="GENERATOR" CONTENT="Mozilla/4.03 [en] (Win95; I) [Netscape]">
   <TITLE>fc</TITLE>
</HEAD>
<BODY>
&nbsp;

<P>函数名: cabs
<BR>功&nbsp; 能: 计算复数的绝对值
<BR>用&nbsp; 法: double cabs(struct complex z);
<BR>程序例:

<P>#include &lt;stdio.h>
<BR>#include &lt;math.h>

<P>int main(void)
<BR>{
<BR>&nbsp;&nbsp; struct complex z;
<BR>&nbsp;&nbsp; double val;

<P>&nbsp;&nbsp; z.x = 2.0;
<BR>&nbsp;&nbsp; z.y = 1.0;
<BR>&nbsp;&nbsp; val = cabs(z);

<P>&nbsp;&nbsp; printf("The absolute value of %.2lfi %.2lfj is %.2lf",
z.x, z.y, val);
<BR>&nbsp;&nbsp; return 0;
<BR>}
<BR>&nbsp;
<BR>&nbsp;
<BR>&nbsp;

<P>函数名: calloc
<BR>功&nbsp; 能: 分配主存储器
<BR>用&nbsp; 法: void *calloc(size_t nelem, size_t elsize);
<BR>程序例:

<P>#include &lt;stdio.h>
<BR>#include &lt;alloc.h>

<P>int main(void)
<BR>{
<BR>&nbsp;&nbsp; char *str = NULL;

<P>&nbsp;&nbsp; /* allocate memory for string */
<BR>&nbsp;&nbsp; str = calloc(10, sizeof(char));

<P>&nbsp;&nbsp; /* copy "Hello" into string */
<BR>&nbsp;&nbsp; strcpy(str, "Hello");

<P>&nbsp;&nbsp; /* display string */
<BR>&nbsp;&nbsp; printf("String is %s\n", str);

<P>&nbsp;&nbsp; /* free memory */
<BR>&nbsp;&nbsp; free(str);

<P>&nbsp;&nbsp; return 0;
<BR>}
<BR>&nbsp;
<BR>&nbsp;
<BR>&nbsp;

<P>函数名: ceil
<BR>功&nbsp; 能: 向上舍入
<BR>用&nbsp; 法: double ceil(double x);
<BR>程序例:

<P>#include &lt;math.h>
<BR>#include &lt;stdio.h>

<P>int main(void)
<BR>{
<BR>&nbsp;&nbsp; double number = 123.54;
<BR>&nbsp;&nbsp; double down, up;

<P>&nbsp;&nbsp; down = floor(number);
<BR>&nbsp;&nbsp; up = ceil(number);

<P>&nbsp;&nbsp; printf("original number&nbsp;&nbsp;&nbsp;&nbsp; %5.2lf\n",
number);
<BR>&nbsp;&nbsp; printf("number rounded down %5.2lf\n", down);
<BR>&nbsp;&nbsp; printf("number rounded up&nbsp;&nbsp; %5.2lf\n", up);

<P>&nbsp;&nbsp; return 0;
<BR>}
<BR>&nbsp;
<BR>&nbsp;
<BR>&nbsp;

<P>函数名: cgets
<BR>功&nbsp; 能: 从控制台读字符串
<BR>用&nbsp; 法: char *cgets(char *str);
<BR>程序例:

<P>#include &lt;stdio.h>
<BR>#include &lt;conio.h>

<P>int main(void)
<BR>{
<BR>&nbsp;&nbsp; char buffer[83];
<BR>&nbsp;&nbsp; char *p;

<P>&nbsp;&nbsp; /* There's space for 80 characters plus the NULL terminator
*/
<BR>&nbsp;&nbsp; buffer[0] = 81;

<P>&nbsp;&nbsp; printf("Input some chars:");
<BR>&nbsp;&nbsp; p = cgets(buffer);
<BR>&nbsp;&nbsp; printf("\ncgets read %d characters: \"%s\"\n", buffer[1],
p);
<BR>&nbsp;&nbsp; printf("The returned pointer is %p, buffer[0] is at %p\n",
p, &amp;buffer);

<P>&nbsp;&nbsp; /* Leave room for 5 characters plus the NULL terminator
*/
<BR>&nbsp;&nbsp; buffer[0] = 6;

<P>&nbsp;&nbsp; printf("Input some chars:");
<BR>&nbsp;&nbsp; p = cgets(buffer);
<BR>&nbsp;&nbsp; printf("\ncgets read %d characters: \"%s\"\n", buffer[1],
p);
<BR>&nbsp;&nbsp; printf("The returned pointer is %p, buffer[0] is at %p\n",
p, &amp;buffer);
<BR>&nbsp;&nbsp; return 0;
<BR>}
<BR>&nbsp;
<BR>&nbsp;
<BR>&nbsp;

<P>函数名: chdir
<BR>功&nbsp; 能: 改变工作目录
<BR>用&nbsp; 法: int chdir(const char *path);
<BR>程序例:

<P>#include &lt;stdio.h>
<BR>#include &lt;stdlib.h>
<BR>#include &lt;dir.h>

<P>char old_dir[MAXDIR];
<BR>char new_dir[MAXDIR];

<P>int main(void)
<BR>{
<BR>&nbsp;&nbsp; if (getcurdir(0, old_dir))
<BR>&nbsp;&nbsp; {
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; perror("getcurdir()");
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; exit(1);
<BR>&nbsp;&nbsp; }
<BR>&nbsp;&nbsp; printf("Current directory is: \\%s\n", old_dir);

<P>&nbsp;&nbsp; if (chdir("\\"))
<BR>&nbsp;&nbsp; {
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; perror("chdir()");
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; exit(1);
<BR>&nbsp;&nbsp; }

<P>&nbsp;&nbsp; if (getcurdir(0, new_dir))
<BR>&nbsp;&nbsp; {
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; perror("getcurdir()");
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; exit(1);
<BR>&nbsp;&nbsp; }
<BR>&nbsp;&nbsp; printf("Current directory is now: \\%s\n", new_dir);

<P>&nbsp;&nbsp; printf("\nChanging back to orignal directory: \\%s\n",
old_dir);
<BR>&nbsp;&nbsp; if (chdir(old_dir))
<BR>&nbsp;&nbsp; {
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; perror("chdir()");
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; exit(1);
<BR>&nbsp;&nbsp; }

<P>&nbsp;&nbsp; return 0;
<BR>}
<BR>&nbsp;
<BR>&nbsp;

<P>函数名: _chmod, chmod
<BR>功&nbsp; 能: 改变文件的访问方式
<BR>用&nbsp; 法: int chmod(const char *filename, int permiss);
<BR>程序例:

<P>#include &lt;sys\stat.h>
<BR>#include &lt;stdio.h>
<BR>#include &lt;io.h>

<P>void make_read_only(char *filename);

<P>int main(void)
<BR>{
<BR>&nbsp;&nbsp; make_read_only("NOTEXIST.FIL");
<BR>&nbsp;&nbsp; make_read_only("MYFILE.FIL");
<BR>&nbsp;&nbsp; return 0;
<BR>}

<P>void make_read_only(char *filename)
<BR>{
<BR>&nbsp;&nbsp; int stat;

<P>&nbsp;&nbsp; stat = chmod(filename, S_IREAD);
<BR>&nbsp;&nbsp; if (stat)
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf("Couldn't make %s read-only\n",
filename);
<BR>&nbsp;&nbsp; else
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf("Made %s read-only\n", filename);
<BR>}
<BR>&nbsp;
<BR>&nbsp;
<BR>&nbsp;

<P>函数名: chsize
<BR>功&nbsp; 能: 改变文件大小
<BR>用&nbsp; 法: int chsize(int handle, long size);
<BR>程序例:

<P>#include &lt;string.h>
<BR>#include &lt;fcntl.h>
<BR>#include &lt;io.h>

<P>int main(void)
<BR>{
<BR>&nbsp;&nbsp; int handle;
<BR>&nbsp;&nbsp; char buf[11] = "0123456789";

<P>&nbsp;&nbsp; /* create text file containing 10 bytes */
<BR>&nbsp;&nbsp; handle = open("DUMMY.FIL", O_CREAT);
<BR>&nbsp;&nbsp; write(handle, buf, strlen(buf));

<P>&nbsp;&nbsp; /* truncate the file to 5 bytes in size */
<BR>&nbsp;&nbsp; chsize(handle, 5);

<P>&nbsp;&nbsp; /* close the file */
<BR>&nbsp;&nbsp; close(handle);
<BR>&nbsp;&nbsp; return 0;
<BR>}
<BR>&nbsp;
<BR>&nbsp;

<P>函数名: circle
<BR>功&nbsp; 能: 在给定半径以(x, y)为圆心画圆
<BR>用&nbsp; 法: void far circle(int x, int y, int radius);
<BR>程序例:

<P>#include &lt;graphics.h>
<BR>#include &lt;stdlib.h>
<BR>#include &lt;stdio.h>
<BR>#include &lt;conio.h>

<P>int main(void)
<BR>{
<BR>&nbsp;&nbsp; /* request auto detection */
<BR>&nbsp;&nbsp; int gdriver = DETECT, gmode, errorcode;
<BR>&nbsp;&nbsp; int midx, midy;
<BR>&nbsp;&nbsp; int radius = 100;

<P>&nbsp;&nbsp; /* initialize graphics and local variables */
<BR>&nbsp;&nbsp; initgraph(&amp;gdriver, &amp;gmode, "");

<P>&nbsp;&nbsp; /* read result of initialization */
<BR>&nbsp;&nbsp; errorcode = graphresult();
<BR>&nbsp;&nbsp; if (errorcode != grOk)&nbsp; /* an error occurred */
<BR>&nbsp;&nbsp; {
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf("Graphics error: %s\n", grapherrormsg(errorcode));
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf("Press any key to halt:");
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; getch();
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; exit(1); /* terminate with an error
code */
<BR>&nbsp;&nbsp; }

<P>&nbsp;&nbsp; midx = getmaxx() / 2;
<BR>&nbsp;&nbsp; midy = getmaxy() / 2;
<BR>&nbsp;&nbsp; setcolor(getmaxcolor());

<P>&nbsp;&nbsp; /* draw the circle */
<BR>&nbsp;&nbsp; circle(midx, midy, radius);

<P>&nbsp;&nbsp; /* clean up */
<BR>&nbsp;&nbsp; getch();
<BR>&nbsp;&nbsp; closegraph();
<BR>&nbsp;&nbsp; return 0;
<BR>}
<BR>&nbsp;
<BR>&nbsp;
<BR>&nbsp;

<P>函数名: cleardevice
<BR>功&nbsp; 能: 清除图形屏幕
<BR>用&nbsp; 法: void far cleardevice(void);
<BR>程序例:

<P>#include &lt;graphics.h>
<BR>#include &lt;stdlib.h>
<BR>#include &lt;stdio.h>
<BR>#include &lt;conio.h>

<P>int main(void)
<BR>{
<BR>&nbsp;&nbsp; /* request auto detection */
<BR>&nbsp;&nbsp; int gdriver = DETECT, gmode, errorcode;
<BR>&nbsp;&nbsp; int midx, midy;

<P>&nbsp;&nbsp; /* initialize graphics and local variables */
<BR>&nbsp;&nbsp; initgraph(&amp;gdriver, &amp;gmode, "");

<P>&nbsp;&nbsp; /* read result of initialization */
<BR>&nbsp;&nbsp; errorcode = graphresult();
<BR>&nbsp;&nbsp; if (errorcode != grOk)&nbsp; /* an error occurred */
<BR>&nbsp;&nbsp; {
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf("Graphics error: %s\n", grapherrormsg(errorcode));
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf("Press any key to halt:");
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; getch();
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; exit(1); /* terminate with an error
code */
<BR>&nbsp;&nbsp; }

<P>&nbsp;&nbsp; midx = getmaxx() / 2;
<BR>&nbsp;&nbsp; midy = getmaxy() / 2;
<BR>&nbsp;&nbsp; setcolor(getmaxcolor());

<P>&nbsp;&nbsp; /* for centering screen messages */
<BR>&nbsp;&nbsp; settextjustify(CENTER_TEXT, CENTER_TEXT);

<P>&nbsp;&nbsp; /* output a message to the screen */
<BR>&nbsp;&nbsp; outtextxy(midx, midy, "press any key to clear the screen:");

<P>&nbsp;&nbsp; /* wait for a key */
<BR>&nbsp;&nbsp; getch();

<P>&nbsp;&nbsp; /* clear the screen */
<BR>&nbsp;&nbsp; cleardevice();

<P>&nbsp;&nbsp; /* output another message */
<BR>&nbsp;&nbsp; outtextxy(midx, midy, "press any key to quit:");

<P>&nbsp;&nbsp; /* clean up */
<BR>&nbsp;&nbsp; getch();
<BR>&nbsp;&nbsp; closegraph();
<BR>&nbsp;&nbsp; return 0;
<BR>}
<BR>&nbsp;
<BR>&nbsp;
<BR>&nbsp;

<P>函数名: clearerr
<BR>功&nbsp; 能: 复位错误标志
<BR>用&nbsp; 法:void clearerr(FILE *stream);
<BR>程序例:

<P>#include &lt;stdio.h>

<P>int main(void)
<BR>{
<BR>&nbsp;&nbsp; FILE *fp;
<BR>&nbsp;&nbsp; char ch;

<P>&nbsp;&nbsp; /* open a file for writing */
<BR>&nbsp;&nbsp; fp = fopen("DUMMY.FIL", "w");

<P>&nbsp;&nbsp; /* force an error condition by attempting to read */
<BR>&nbsp;&nbsp; ch = fgetc(fp);
<BR>&nbsp;&nbsp; printf("%c\n",ch);

<P>&nbsp;&nbsp; if (ferror(fp))
<BR>&nbsp;&nbsp; {
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* display an error message */
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf("Error reading from DUMMY.FIL\n");

<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* reset the error and EOF indicators
*/
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; clearerr(fp);
<BR>&nbsp;&nbsp; }

<P>&nbsp;&nbsp; fclose(fp);
<BR>&nbsp;&nbsp; return 0;
<BR>}
<BR>&nbsp;
<BR>&nbsp;
<BR>&nbsp;

<P>函数名: clearviewport
<BR>功&nbsp; 能: 清除图形视区
<BR>用&nbsp; 法: void far clearviewport(void);
<BR>程序例:

<P>#include &lt;graphics.h>
<BR>#include &lt;stdlib.h>
<BR>#include &lt;stdio.h>
<BR>#include &lt;conio.h>

<P>#define CLIP_ON 1&nbsp;&nbsp; /* activates clipping in viewport */

<P>int main(void)
<BR>{
<BR>&nbsp;&nbsp; /* request auto detection */
<BR>&nbsp;&nbsp; int gdriver = DETECT, gmode, errorcode;
<BR>&nbsp;&nbsp; int ht;

<P>&nbsp;&nbsp; /* initialize graphics and local variables */
<BR>&nbsp;&nbsp; initgraph(&amp;gdriver, &amp;gmode, "");

<P>&nbsp;&nbsp; /* read result of initialization */
<BR>&nbsp;&nbsp; errorcode = graphresult();
<BR>&nbsp;&nbsp; if (errorcode != grOk)&nbsp; /* an error occurred */
<BR>&nbsp;&nbsp; {
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf("Graphics error: %s\n", grapherrormsg(errorcode));
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf("Press any key to halt:");
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; getch();
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; exit(1); /* terminate with an error
code */
<BR>&nbsp;&nbsp; }

<P>&nbsp;&nbsp; setcolor(getmaxcolor());
<BR>&nbsp;&nbsp; ht = textheight("W");

<P>&nbsp;&nbsp; /* message in default full-screen viewport */
<BR>&nbsp;&nbsp; outtextxy(0, 0, "* &lt;-- (0, 0) in default viewport");

<P>&nbsp;&nbsp; /* create a smaller viewport */
<BR>&nbsp;&nbsp; setviewport(50, 50, getmaxx()-50, getmaxy()-50, CLIP_ON);

<P>&nbsp;&nbsp; /* display some messages */
<BR>&nbsp;&nbsp; outtextxy(0, 0, "* &lt;-- (0, 0) in smaller viewport");
<BR>&nbsp;&nbsp; outtextxy(0, 2*ht, "Press any key to clear viewport:");

<P>&nbsp;&nbsp; /* wait for a key */
<BR>&nbsp;&nbsp; getch();

<P>&nbsp;&nbsp; /* clear the viewport */
<BR>&nbsp;&nbsp; clearviewport();

<P>&nbsp;&nbsp; /* output another message */
<BR>&nbsp;&nbsp; outtextxy(0, 0, "Press any key to quit:");

<P>&nbsp;&nbsp; /* clean up */
<BR>&nbsp;&nbsp; getch();
<BR>&nbsp;&nbsp; closegraph();
<BR>&nbsp;&nbsp; return 0;
<BR>}
<BR>&nbsp;
<BR>&nbsp;
<BR>&nbsp;

<P>函数名: _close, close
<BR>功&nbsp; 能: 关闭文件句柄
<BR>用&nbsp; 法: int close(int handle);
<BR>程序例:

<P>#include &lt;string.h>
<BR>#include &lt;stdio.h>
<BR>#include &lt;fcntl.h>
<BR>#include &lt;io.h>

<P>main()
<BR>{
<BR>&nbsp;&nbsp; int handle;
<BR>&nbsp;&nbsp; char buf[11] = "0123456789";

<P>&nbsp;&nbsp; /* create a file containing 10 bytes */
<BR>&nbsp;&nbsp; handle = open("NEW.FIL", O_CREAT);
<BR>&nbsp;&nbsp; if (handle > -1)
<BR>&nbsp;&nbsp; {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -