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

📄 022.htm

📁 一个好的讲DSP中C语言编程的电子书
💻 HTM
📖 第 1 页 / 共 2 页
字号:
<HTML><HEAD><meta http-equiv="Content-Type" content="text/html; charset=GB2312"><TITLE>王大刚-->C语言编程宝典-->C</TITLE>
<META NAME="keywords" CONTENT="王大刚 C语言编程宝典 C">
<META NAME="description" CONTENT="王大刚 - C语言编程宝典 - C">

<style>
<!--
#page {position:absolute; z-index:0; left:0px; top:0px}
.tt3 {font: 9pt/12pt "宋体"}
.tt2 {font: 12pt/15pt "宋体"}
a {text-decoration:none}
a:hover {color: blue;text-decoration:underline}
-->
</style>
</HEAD>
<body text="#000000" aLink=#9900ff link=#006699 vLink=#006699 bgcolor="#FFFFFF" leftmargin="3" topmargin="3" marginheight="3" marginwidth="3">
<TABLE WIDTH="100%" CELLPADDING=10 CELLSPACING=0 BORDER=0>
<TR>
<TD CLASS="tt3" VALIGN="top" width="8%"  bgcolor="#e0e0e0"><strong><A HREF="023.htm">后一页</A><BR>
<A HREF="021.htm">前一页</A><BR>

<A HREF="index.html">回目录</A><BR>
<A HREF="../../../../index.htm">回首页</A><BR>
</strong>
</TD>
<TD class="tt2" bgcolor="#F5F8F8" width="84%"><center><B><FONT style="FONT-SIZE: 16.5pt" COLOR="#FF6666" FACE="楷体_GB2312">C</FONT></B></center>
<hr color="#EE9B73" size="1" width="94%">
<P>函数名: cabs
<BR>功&nbsp; 能: 计算复数的绝对值
<BR>用&nbsp; 法: double cabs(struct complex z);
<BR>程序例:
<BR>
<P>#include &lt;stdio.h>
<BR>#include &lt;math.h>
<BR>
<P>int main(void)
<BR>{
<BR>&nbsp;&nbsp; struct complex z;
<BR>&nbsp;&nbsp; double val;
<BR>
<P>&nbsp;&nbsp; z.x = 2.0;
<BR>&nbsp;&nbsp; z.y = 1.0;
<BR>&nbsp;&nbsp; val = cabs(z);
<BR>
<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;
<BR>
<P>函数名: calloc
<BR>功&nbsp; 能: 分配主存储器
<BR>用&nbsp; 法: void *calloc(size_t nelem, size_t elsize);
<BR>程序例:
<BR>
<P>#include &lt;stdio.h>
<BR>#include &lt;alloc.h>
<BR>
<P>int main(void)
<BR>{
<BR>&nbsp;&nbsp; char *str = NULL;
<BR>
<P>&nbsp;&nbsp; /* allocate memory for string */
<BR>&nbsp;&nbsp; str = calloc(10, sizeof(char));
<BR>
<P>&nbsp;&nbsp; /* copy "Hello" into string */
<BR>&nbsp;&nbsp; strcpy(str, "Hello");
<BR>
<P>&nbsp;&nbsp; /* display string */
<BR>&nbsp;&nbsp; printf("String is %s\n", str);
<BR>
<P>&nbsp;&nbsp; /* free memory */
<BR>&nbsp;&nbsp; free(str);
<BR>
<P>&nbsp;&nbsp; return 0;
<BR>}
<BR>&nbsp;
<BR>&nbsp;
<BR>&nbsp;
<BR>
<P>函数名: ceil
<BR>功&nbsp; 能: 向上舍入
<BR>用&nbsp; 法: double ceil(double x);
<BR>程序例:
<BR>
<P>#include &lt;math.h>
<BR>#include &lt;stdio.h>
<BR>
<P>int main(void)
<BR>{
<BR>&nbsp;&nbsp; double number = 123.54;
<BR>&nbsp;&nbsp; double down, up;
<BR>
<P>&nbsp;&nbsp; down = floor(number);
<BR>&nbsp;&nbsp; up = ceil(number);
<BR>
<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);
<BR>
<P>&nbsp;&nbsp; return 0;
<BR>}
<BR>&nbsp;
<BR>&nbsp;
<BR>&nbsp;
<BR>
<P>函数名: cgets
<BR>功&nbsp; 能: 从控制台读字符串
<BR>用&nbsp; 法: char *cgets(char *str);
<BR>程序例:
<BR>
<P>#include &lt;stdio.h>
<BR>#include &lt;conio.h>
<BR>
<P>int main(void)
<BR>{
<BR>&nbsp;&nbsp; char buffer[83];
<BR>&nbsp;&nbsp; char *p;
<BR>
<P>&nbsp;&nbsp; /* There's space for 80 characters plus the NULL terminator
*/
<BR>&nbsp;&nbsp; buffer[0] = 81;
<BR>
<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>
<P>&nbsp;&nbsp; /* Leave room for 5 characters plus the NULL terminator
*/
<BR>&nbsp;&nbsp; buffer[0] = 6;
<BR>
<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;
<BR>
<P>函数名: chdir
<BR>功&nbsp; 能: 改变工作目录
<BR>用&nbsp; 法: int chdir(const char *path);
<BR>程序例:
<BR>
<P>#include &lt;stdio.h>
<BR>#include &lt;stdlib.h>
<BR>#include &lt;dir.h>
<BR>
<P>char old_dir[MAXDIR];
<BR>char new_dir[MAXDIR];
<BR>
<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);
<BR>
<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; }
<BR>
<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);
<BR>
<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; }
<BR>
<P>&nbsp;&nbsp; return 0;
<BR>}
<BR>&nbsp;
<BR>&nbsp;
<BR>
<P>函数名: _chmod, chmod
<BR>功&nbsp; 能: 改变文件的访问方式
<BR>用&nbsp; 法: int chmod(const char *filename, int permiss);
<BR>程序例:
<BR>
<P>#include &lt;sys\stat.h>
<BR>#include &lt;stdio.h>
<BR>#include &lt;io.h>
<BR>
<P>void make_read_only(char *filename);
<BR>
<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>}
<BR>
<P>void make_read_only(char *filename)
<BR>{
<BR>&nbsp;&nbsp; int stat;
<BR>
<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;
<BR>
<P>函数名: chsize
<BR>功&nbsp; 能: 改变文件大小
<BR>用&nbsp; 法: int chsize(int handle, long size);
<BR>程序例:
<BR>
<P>#include &lt;string.h>
<BR>#include &lt;fcntl.h>
<BR>#include &lt;io.h>
<BR>
<P>int main(void)
<BR>{
<BR>&nbsp;&nbsp; int handle;
<BR>&nbsp;&nbsp; char buf[11] = "0123456789";
<BR>
<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));
<BR>
<P>&nbsp;&nbsp; /* truncate the file to 5 bytes in size */
<BR>&nbsp;&nbsp; chsize(handle, 5);
<BR>
<P>&nbsp;&nbsp; /* close the file */
<BR>&nbsp;&nbsp; close(handle);
<BR>&nbsp;&nbsp; return 0;
<BR>}
<BR>&nbsp;
<BR>&nbsp;
<BR>
<P>函数名: circle
<BR>功&nbsp; 能: 在给定半径以(x, y)为圆心画圆
<BR>用&nbsp; 法: void far circle(int x, int y, int radius);
<BR>程序例:
<BR>
<P>#include &lt;graphics.h>
<BR>#include &lt;stdlib.h>
<BR>#include &lt;stdio.h>
<BR>#include &lt;conio.h>
<BR>
<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;
<BR>
<P>&nbsp;&nbsp; /* initialize graphics and local variables */
<BR>&nbsp;&nbsp; initgraph(&amp;gdriver, &amp;gmode, "");
<BR>
<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; }
<BR>
<P>&nbsp;&nbsp; midx = getmaxx() / 2;
<BR>&nbsp;&nbsp; midy = getmaxy() / 2;
<BR>&nbsp;&nbsp; setcolor(getmaxcolor());
<BR>
<P>&nbsp;&nbsp; /* draw the circle */
<BR>&nbsp;&nbsp; circle(midx, midy, radius);
<BR>
<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;
<BR>
<P>函数名: cleardevice
<BR>功&nbsp; 能: 清除图形屏幕
<BR>用&nbsp; 法: void far cleardevice(void);
<BR>程序例:
<BR>
<P>#include &lt;graphics.h>
<BR>#include &lt;stdlib.h>
<BR>#include &lt;stdio.h>
<BR>#include &lt;conio.h>
<BR>
<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>
<P>&nbsp;&nbsp; /* initialize graphics and local variables */
<BR>&nbsp;&nbsp; initgraph(&amp;gdriver, &amp;gmode, "");
<BR>
<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; }
<BR>
<P>&nbsp;&nbsp; midx = getmaxx() / 2;
<BR>&nbsp;&nbsp; midy = getmaxy() / 2;
<BR>&nbsp;&nbsp; setcolor(getmaxcolor());
<BR>
<P>&nbsp;&nbsp; /* for centering screen messages */
<BR>&nbsp;&nbsp; settextjustify(CENTER_TEXT, CENTER_TEXT);
<BR>
<P>&nbsp;&nbsp; /* output a message to the screen */
<BR>&nbsp;&nbsp; outtextxy(midx, midy, "press any key to clear the screen:");
<BR>
<P>&nbsp;&nbsp; /* wait for a key */
<BR>&nbsp;&nbsp; getch();
<BR>
<P>&nbsp;&nbsp; /* clear the screen */
<BR>&nbsp;&nbsp; cleardevice();
<BR>
<P>&nbsp;&nbsp; /* output another message */
<BR>&nbsp;&nbsp; outtextxy(midx, midy, "press any key to quit:");
<BR>
<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;
<BR>
<P>函数名: clearerr
<BR>功&nbsp; 能: 复位错误标志
<BR>用&nbsp; 法:void clearerr(FILE *stream);
<BR>程序例:
<BR>
<P>#include &lt;stdio.h>
<BR>
<P>int main(void)
<BR>{
<BR>&nbsp;&nbsp; FILE *fp;
<BR>&nbsp;&nbsp; char ch;
<BR>
<P>&nbsp;&nbsp; /* open a file for writing */
<BR>&nbsp;&nbsp; fp = fopen("DUMMY.FIL", "w");
<BR>
<P>&nbsp;&nbsp; /* force an error condition by attempting to read */
<BR>&nbsp;&nbsp; ch = fgetc(fp);
<BR>&nbsp;&nbsp; printf("%c\n",ch);
<BR>
<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");
<BR>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* reset the error and EOF indicators
*/
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; clearerr(fp);
<BR>&nbsp;&nbsp; }
<BR>
<P>&nbsp;&nbsp; fclose(fp);
<BR>&nbsp;&nbsp; return 0;
<BR>}
<BR>&nbsp;
<BR>&nbsp;
<BR>&nbsp;
<BR>
<P>函数名: clearviewport
<BR>功&nbsp; 能: 清除图形视区
<BR>用&nbsp; 法: void far clearviewport(void);
<BR>程序例:
<BR>
<P>#include &lt;graphics.h>
<BR>#include &lt;stdlib.h>
<BR>#include &lt;stdio.h>
<BR>#include &lt;conio.h>
<BR>
<P>#define CLIP_ON 1&nbsp;&nbsp; /* activates clipping in viewport */
<BR>
<P>int main(void)
<BR>{
<BR>&nbsp;&nbsp; /* request auto detection */
<BR>&nbsp;&nbsp; int gdriver = DETECT, gmode, errorcode;
<BR>&nbsp;&nbsp; int ht;
<BR>
<P>&nbsp;&nbsp; /* initialize graphics and local variables */
<BR>&nbsp;&nbsp; initgraph(&amp;gdriver, &amp;gmode, "");
<BR>
<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; }
<BR>
<P>&nbsp;&nbsp; setcolor(getmaxcolor());
<BR>&nbsp;&nbsp; ht = textheight("W");
<BR>
<P>&nbsp;&nbsp; /* message in default full-screen viewport */
<BR>&nbsp;&nbsp; outtextxy(0, 0, "* &lt;-- (0, 0) in default viewport");
<BR>
<P>&nbsp;&nbsp; /* create a smaller viewport */
<BR>&nbsp;&nbsp; setviewport(50, 50, getmaxx()-50, getmaxy()-50, CLIP_ON);
<BR>
<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:");
<BR>
<P>&nbsp;&nbsp; /* wait for a key */
<BR>&nbsp;&nbsp; getch();
<BR>
<P>&nbsp;&nbsp; /* clear the viewport */
<BR>&nbsp;&nbsp; clearviewport();
<BR>
<P>&nbsp;&nbsp; /* output another message */
<BR>&nbsp;&nbsp; outtextxy(0, 0, "Press any key to quit:");
<BR>
<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;
<BR>
<P>函数名: _close, close
<BR>功&nbsp; 能: 关闭文件句柄
<BR>用&nbsp; 法: int close(int handle);
<BR>程序例:
<BR>
<P>#include &lt;string.h>
<BR>#include &lt;stdio.h>
<BR>#include &lt;fcntl.h>
<BR>#include &lt;io.h>
<BR>
<P>main()
<BR>{
<BR>&nbsp;&nbsp; int handle;
<BR>&nbsp;&nbsp; char buf[11] = "0123456789";

⌨️ 快捷键说明

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