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

📄 王大刚--c语言编程宝典--c.htm

📁 初学者的良师益友。其中包括C的全部教程。
💻 HTM
📖 第 1 页 / 共 2 页
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0038)http://www.hjflying.8u8.com/cl/022.htm -->
<HTML><HEAD><TITLE>王大刚-->C语言编程宝典-->C</TITLE>
<META http-equiv=Content-Type content="text/html; charset=GB2312">
<META content="王大刚 C语言编程宝典 C" name=keywords>
<META content="王大刚 - C语言编程宝典 - C" name=description>
<STYLE>#page {
	LEFT: 0px; POSITION: absolute; TOP: 0px
}
.tt3 {
	FONT: 9pt/12pt "宋体"
}
.tt2 {
	FONT: 12pt/15pt "宋体"
}
A {
	TEXT-DECORATION: none
}
A:hover {
	COLOR: blue; TEXT-DECORATION: underline
}
</STYLE>

<META content="MSHTML 6.00.2600.0" name=GENERATOR></HEAD>
<BODY text=#000000 vLink=#006699 aLink=#9900ff link=#006699 bgColor=#ffffff 
leftMargin=3 topMargin=3 marginwidth="3" marginheight="3">
<TABLE cellSpacing=0 cellPadding=10 width="100%" border=0>
  <TBODY>
  <TR>
    <TD class=tt3 vAlign=top width="8%" bgColor=#e0e0e0><STRONG><A 
      href="http://www.hjflying.8u8.com/cl/023.htm">后一页</A><BR><A 
      href="http://www.hjflying.8u8.com/cl/021.htm">前一页</A><BR><A 
      href="http://www.hjflying.8u8.com/cl/index.html">回目录</A><BR><A 
      href="http://www.hjflying.8u8.com/index.htm">回首页</A><BR></STRONG></TD>
    <TD class=tt2 width="84%" bgColor=#f5f8f8>
      <CENTER><B><FONT style="FONT-SIZE: 16.5pt" face=楷体_GB2312 
      color=#ff6666>C</FONT></B></CENTER>
      <HR width="94%" color=#ee9b73 SIZE=1>

      <P>函数名: cabs <BR>功&nbsp; 能: 计算复数的绝对值 <BR>用&nbsp; 法: double cabs(struct 
      complex z); <BR>程序例: <BR>
      <P>#include &lt;stdio.h&gt; <BR>#include &lt;math.h&gt; <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&gt; <BR>#include &lt;alloc.h&gt; <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&gt; <BR>#include &lt;stdio.h&gt; <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&gt; <BR>#include &lt;conio.h&gt; <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&gt; <BR>#include &lt;stdlib.h&gt; <BR>#include 
      &lt;dir.h&gt; <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&gt; <BR>#include &lt;stdio.h&gt; <BR>#include 
      &lt;io.h&gt; <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&gt; <BR>#include &lt;fcntl.h&gt; <BR>#include 
      &lt;io.h&gt; <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&gt; <BR>#include &lt;stdlib.h&gt; <BR>#include 
      &lt;stdio.h&gt; <BR>#include &lt;conio.h&gt; <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&gt; <BR>#include &lt;stdlib.h&gt; <BR>#include 
      &lt;stdio.h&gt; <BR>#include &lt;conio.h&gt; <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&gt; <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&gt; <BR>#include &lt;stdlib.h&gt; <BR>#include 
      &lt;stdio.h&gt; <BR>#include &lt;conio.h&gt; <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>

⌨️ 快捷键说明

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