c.txt
资源名称:C.rar [点击查看]
上传用户:harvey99
上传日期:2020-01-11
资源大小:938k
文件大小:15k
源码类别:

其他书籍

开发平台:

CHM

  1. 函数名: cabs
  2. 功  能: 计算复数的绝对值
  3. 用  法: double cabs(struct complex z);
  4. 程序例: 
  5. #include <stdio.h>
  6. #include <math.h>
  7. int main(void)
  8. {
  9.    struct complex z;
  10.    double val;
  11.    z.x = 2.0;
  12.    z.y = 1.0;
  13.    val = cabs(z);
  14.    printf("The absolute value of %.2lfi %.2lfj is %.2lf", z.x, z.y, val);
  15.    return 0;
  16. }
  17.  
  18.  
  19.  
  20. 函数名: calloc
  21. 功  能: 分配主存储器
  22. 用  法: void *calloc(size_t nelem, size_t elsize);
  23. 程序例:
  24. #include <stdio.h>
  25. #include <alloc.h>
  26. int main(void)
  27. {
  28.    char *str = NULL;
  29.    /* allocate memory for string */
  30.    str = calloc(10, sizeof(char));
  31.    /* copy "Hello" into string */
  32.    strcpy(str, "Hello");
  33.    /* display string */
  34.    printf("String is %sn", str);
  35.    /* free memory */
  36.    free(str);
  37.    return 0;
  38. }
  39.  
  40.  
  41.  
  42. 函数名: ceil
  43. 功  能: 向上舍入
  44. 用  法: double ceil(double x);
  45. 程序例:
  46. #include <math.h>
  47. #include <stdio.h>
  48. int main(void)
  49. {
  50.    double number = 123.54;
  51.    double down, up;
  52.    down = floor(number);
  53.    up = ceil(number);
  54.    printf("original number     %5.2lfn", number);
  55.    printf("number rounded down %5.2lfn", down);
  56.    printf("number rounded up   %5.2lfn", up);
  57.    return 0;
  58. }
  59.  
  60.  
  61.  
  62. 函数名: cgets
  63. 功  能: 从控制台读字符串
  64. 用  法: char *cgets(char *str);
  65. 程序例:
  66. #include <stdio.h>
  67. #include <conio.h>
  68. int main(void)
  69. {
  70.    char buffer[83];
  71.    char *p;
  72.    /* There's space for 80 characters plus the NULL terminator */
  73.    buffer[0] = 81;
  74.    printf("Input some chars:");
  75.    p = cgets(buffer);
  76.    printf("ncgets read %d characters: "%s"n", buffer[1], p);
  77.    printf("The returned pointer is %p, buffer[0] is at %pn", p, &buffer);
  78.    /* Leave room for 5 characters plus the NULL terminator */
  79.    buffer[0] = 6;
  80.    printf("Input some chars:");
  81.    p = cgets(buffer);
  82.    printf("ncgets read %d characters: "%s"n", buffer[1], p);
  83.    printf("The returned pointer is %p, buffer[0] is at %pn", p, &buffer);
  84.    return 0;
  85. }
  86.  
  87.  
  88.  
  89. 函数名: chdir
  90. 功  能: 改变工作目录
  91. 用  法: int chdir(const char *path);
  92. 程序例:
  93. #include <stdio.h>
  94. #include <stdlib.h>
  95. #include <dir.h>
  96. char old_dir[MAXDIR];
  97. char new_dir[MAXDIR];
  98. int main(void)
  99. {
  100.    if (getcurdir(0, old_dir))
  101.    {
  102.       perror("getcurdir()");
  103.       exit(1);
  104.    }
  105.    printf("Current directory is: \%sn", old_dir);
  106.    if (chdir("\"))
  107.    {
  108.       perror("chdir()");
  109.       exit(1);
  110.    }
  111.    if (getcurdir(0, new_dir))
  112.    {
  113.       perror("getcurdir()");
  114.       exit(1);
  115.    }
  116.    printf("Current directory is now: \%sn", new_dir);
  117.    printf("nChanging back to orignal directory: \%sn", old_dir);
  118.    if (chdir(old_dir))
  119.    {
  120.       perror("chdir()");
  121.       exit(1);
  122.    }
  123.    return 0;
  124. }
  125.  
  126.  
  127. 函数名: _chmod, chmod
  128. 功  能: 改变文件的访问方式
  129. 用  法: int chmod(const char *filename, int permiss);
  130. 程序例:
  131. #include <sysstat.h>
  132. #include <stdio.h>
  133. #include <io.h>
  134. void make_read_only(char *filename);
  135. int main(void)
  136. {
  137.    make_read_only("NOTEXIST.FIL");
  138.    make_read_only("MYFILE.FIL");
  139.    return 0;
  140. }
  141. void make_read_only(char *filename)
  142. {
  143.    int stat;
  144.    stat = chmod(filename, S_IREAD);
  145.    if (stat)
  146.       printf("Couldn't make %s read-onlyn", filename);
  147.    else
  148.       printf("Made %s read-onlyn", filename);
  149. }
  150.  
  151.  
  152.  
  153. 函数名: chsize
  154. 功  能: 改变文件大小
  155. 用  法: int chsize(int handle, long size);
  156. 程序例:
  157. #include <string.h>
  158. #include <fcntl.h>
  159. #include <io.h>
  160. int main(void)
  161. {
  162.    int handle;
  163.    char buf[11] = "0123456789";
  164.    /* create text file containing 10 bytes */
  165.    handle = open("DUMMY.FIL", O_CREAT);
  166.    write(handle, buf, strlen(buf));
  167.    /* truncate the file to 5 bytes in size */
  168.    chsize(handle, 5);
  169.    /* close the file */
  170.    close(handle);
  171.    return 0;
  172. }
  173.  
  174.  
  175. 函数名: circle
  176. 功  能: 在给定半径以(x, y)为圆心画圆
  177. 用  法: void far circle(int x, int y, int radius);
  178. 程序例:
  179. #include <graphics.h>
  180. #include <stdlib.h>
  181. #include <stdio.h>
  182. #include <conio.h>
  183. int main(void)
  184. {
  185.    /* request auto detection */
  186.    int gdriver = DETECT, gmode, errorcode;
  187.    int midx, midy;
  188.    int radius = 100;
  189.    /* initialize graphics and local variables */
  190.    initgraph(&gdriver, &gmode, "");
  191.    /* read result of initialization */
  192.    errorcode = graphresult();
  193.    if (errorcode != grOk)  /* an error occurred */
  194.    {
  195.       printf("Graphics error: %sn", grapherrormsg(errorcode));
  196.       printf("Press any key to halt:");
  197.       getch();
  198.       exit(1); /* terminate with an error code */
  199.    }
  200.    midx = getmaxx() / 2;
  201.    midy = getmaxy() / 2;
  202.    setcolor(getmaxcolor());
  203.    /* draw the circle */
  204.    circle(midx, midy, radius);
  205.    /* clean up */
  206.    getch();
  207.    closegraph();
  208.    return 0;
  209. }
  210.  
  211.  
  212.  
  213. 函数名: cleardevice
  214. 功  能: 清除图形屏幕
  215. 用  法: void far cleardevice(void);
  216. 程序例:
  217. #include <graphics.h>
  218. #include <stdlib.h>
  219. #include <stdio.h>
  220. #include <conio.h>
  221. int main(void)
  222. {
  223.    /* request auto detection */
  224.    int gdriver = DETECT, gmode, errorcode;
  225.    int midx, midy;
  226.    /* initialize graphics and local variables */
  227.    initgraph(&gdriver, &gmode, "");
  228.    /* read result of initialization */
  229.    errorcode = graphresult();
  230.    if (errorcode != grOk)  /* an error occurred */
  231.    {
  232.       printf("Graphics error: %sn", grapherrormsg(errorcode));
  233.       printf("Press any key to halt:");
  234.       getch();
  235.       exit(1); /* terminate with an error code */
  236.    }
  237.    midx = getmaxx() / 2;
  238.    midy = getmaxy() / 2;
  239.    setcolor(getmaxcolor());
  240.    /* for centering screen messages */
  241.    settextjustify(CENTER_TEXT, CENTER_TEXT);
  242.    /* output a message to the screen */
  243.    outtextxy(midx, midy, "press any key to clear the screen:");
  244.    /* wait for a key */
  245.    getch();
  246.    /* clear the screen */
  247.    cleardevice();
  248.    /* output another message */
  249.    outtextxy(midx, midy, "press any key to quit:");
  250.    /* clean up */
  251.    getch();
  252.    closegraph();
  253.    return 0;
  254. }
  255.  
  256.  
  257.  
  258. 函数名: clearerr
  259. 功  能: 复位错误标志
  260. 用  法:void clearerr(FILE *stream);
  261. 程序例:
  262. #include <stdio.h>
  263. int main(void)
  264. {
  265.    FILE *fp;
  266.    char ch;
  267.    /* open a file for writing */
  268.    fp = fopen("DUMMY.FIL", "w");
  269.    /* force an error condition by attempting to read */
  270.    ch = fgetc(fp);
  271.    printf("%cn",ch);
  272.    if (ferror(fp))
  273.    {
  274.       /* display an error message */
  275.       printf("Error reading from DUMMY.FILn");
  276.       /* reset the error and EOF indicators */
  277.       clearerr(fp);
  278.    }
  279.    fclose(fp);
  280.    return 0;
  281. }
  282.  
  283.  
  284.  
  285. 函数名: clearviewport
  286. 功  能: 清除图形视区
  287. 用  法: void far clearviewport(void);
  288. 程序例:
  289. #include <graphics.h>
  290. #include <stdlib.h>
  291. #include <stdio.h>
  292. #include <conio.h>
  293. #define CLIP_ON 1   /* activates clipping in viewport */
  294. int main(void)
  295. {
  296.    /* request auto detection */
  297.    int gdriver = DETECT, gmode, errorcode;
  298.    int ht;
  299.    /* initialize graphics and local variables */
  300.    initgraph(&gdriver, &gmode, "");
  301.    /* read result of initialization */
  302.    errorcode = graphresult();
  303.    if (errorcode != grOk)  /* an error occurred */
  304.    {
  305.       printf("Graphics error: %sn", grapherrormsg(errorcode));
  306.       printf("Press any key to halt:");
  307.       getch();
  308.       exit(1); /* terminate with an error code */
  309.    }
  310.    setcolor(getmaxcolor());
  311.    ht = textheight("W");
  312.    /* message in default full-screen viewport */
  313.    outtextxy(0, 0, "* <-- (0, 0) in default viewport");
  314.    /* create a smaller viewport */
  315.    setviewport(50, 50, getmaxx()-50, getmaxy()-50, CLIP_ON);
  316.    /* display some messages */
  317.    outtextxy(0, 0, "* <-- (0, 0) in smaller viewport");
  318.    outtextxy(0, 2*ht, "Press any key to clear viewport:");
  319.    /* wait for a key */
  320.    getch();
  321.    /* clear the viewport */
  322.    clearviewport();
  323.    /* output another message */
  324.    outtextxy(0, 0, "Press any key to quit:");
  325.    /* clean up */
  326.    getch();
  327.    closegraph();
  328.    return 0;
  329. }
  330.  
  331.  
  332.  
  333. 函数名: _close, close
  334. 功  能: 关闭文件句柄
  335. 用  法: int close(int handle);
  336. 程序例:
  337. #include <string.h>
  338. #include <stdio.h>
  339. #include <fcntl.h>
  340. #include <io.h>
  341. main()
  342. {
  343.    int handle;
  344.    char buf[11] = "0123456789";
  345.    /* create a file containing 10 bytes */
  346.    handle = open("NEW.FIL", O_CREAT);
  347.    if (handle > -1)
  348.    {
  349.        write(handle, buf, strlen(buf));
  350.        /* close the file */
  351.        close(handle);
  352.    }
  353.    else
  354.    {
  355.        printf("Error opening filen");
  356.    }
  357.    return 0;
  358. }
  359.  
  360.  
  361.  
  362. 函数名: clock
  363. 功  能: 确定处理器时间
  364. 用  法: clock_t clock(void);
  365. 程序例:
  366. #include <time.h>
  367. #include <stdio.h>
  368. #include <dos.h>
  369. int main(void)
  370. {
  371.    clock_t start, end;
  372.    start = clock();
  373.    delay(2000);
  374.    end = clock();
  375.    printf("The time was: %fn", (end - start) / CLK_TCK);
  376.    return 0;
  377. }
  378.  
  379.  
  380.  
  381. 函数名: closegraph
  382. 功  能: 关闭图形系统
  383. 用  法: void far closegraph(void);
  384. 程序例:
  385. #include <graphics.h>
  386. #include <stdlib.h>
  387. #include <stdio.h>
  388. #include <conio.h>
  389. int main(void)
  390. {
  391.    /* request auto detection */
  392.    int gdriver = DETECT, gmode, errorcode;
  393.    int x, y;
  394.    /* initialize graphics mode */
  395.    initgraph(&gdriver, &gmode, "");
  396.    /* read result of initialization */
  397.    errorcode = graphresult();
  398.    if (errorcode != grOk)  /* an error
  399.       occurred */
  400.    {
  401.       printf("Graphics error: %sn", grapherrormsg(errorcode));
  402.       printf("Press any key to halt:");
  403.       getch();
  404.       exit(1); /* terminate with an error code */
  405.    }
  406.    x = getmaxx() / 2;
  407.    y = getmaxy() / 2;
  408.    /* output a message */
  409.    settextjustify(CENTER_TEXT, CENTER_TEXT);
  410.    outtextxy(x, y, "Press a key to close the graphics system:");
  411.    /* wait for a key */
  412.    getch();
  413.    /* closes down the graphics system */
  414.    closegraph();
  415.    printf("We're now back in text mode.n");
  416.    printf("Press any key to halt:");
  417.    getch();
  418.    return 0;
  419. }
  420.  
  421.  
  422.  
  423. 函数名: clreol
  424. 功  能: 在文本窗口中清除字符到行末
  425. 用  法: void clreol(void);
  426. 程序例:
  427. #include <conio.h>
  428. int main(void)
  429. {
  430.    clrscr();
  431.    cprintf("The function CLREOL clears all characters from thern");
  432.    cprintf("cursor position to the end of the line within thern");
  433.    cprintf("current text window, without moving the cursor.rn");
  434.    cprintf("Press any key to continue . . .");
  435.    gotoxy(14, 4);
  436.    getch();
  437.    clreol();
  438.    getch();
  439.    return 0;
  440. }
  441.  
  442.  
  443.  
  444. 函数名: clrscr
  445. 功  能: 清除文本模式窗口
  446. 用  法: void clrscr(void);
  447. 程序例:
  448. #include <conio.h>
  449. int main(void)
  450. {
  451.    int i;
  452.    clrscr();
  453.    for (i = 0; i < 20; i++)
  454.       cprintf("%drn", i);
  455.    cprintf("rnPress any key to clear screen");
  456.    getch();
  457.    clrscr();
  458.    cprintf("The screen has been cleared!");
  459.    getch();
  460.    return 0;
  461. }
  462.  
  463.  
  464.  
  465. 函数名: coreleft
  466. 功  能: 返回未使用内存的大小
  467. 用  法: unsigned coreleft(void);
  468. 程序例:
  469. #include <stdio.h>
  470. #include <alloc.h>
  471. int main(void)
  472. {
  473.    printf("The difference between the highest allocated block andn");
  474.    printf("the top of the heap is: %lu bytesn", (unsigned long) coreleft());
  475.    return 0;
  476. }
  477.  
  478. 函数名: cos
  479. 功  能: 余弦函数
  480. 用  法: double cos(double x);
  481. 程序例:
  482. #include <stdio.h>
  483. #include <math.h>
  484. int main(void)
  485. {
  486.    double result;
  487.    double x = 0.5;
  488.    result = cos(x);
  489.    printf("The cosine of %lf is %lfn", x, result);
  490.    return 0;
  491. }
  492.  
  493.  
  494.  
  495. 函数名: cosh
  496. 功  能: 双曲余弦函数
  497. 用  法: dluble cosh(double x);
  498. 程序例:
  499. #include <stdio.h>
  500. #include <math.h>
  501. int main(void)
  502. {
  503.    double result;
  504.    double x = 0.5;
  505.    result = cosh(x);
  506.    printf("The hyperboic cosine of %lf is %lfn", x, result);
  507.    return 0;
  508. }
  509.  
  510.  
  511.  
  512. 函数名: country
  513. 功  能: 返回与国家有关的信息
  514. 用  法: struct COUNTRY *country(int countrycode, struct country *country);
  515. 程序例:
  516. #include <dos.h>
  517. #include <stdio.h>
  518. #define USA 0
  519. int main(void)
  520. {
  521.    struct COUNTRY country_info;
  522.    country(USA, &country_info);
  523.    printf("The currency symbol for the USA is: %sn",
  524.            country_info.co_curr);
  525.    return 0;
  526. }
  527.  
  528.  
  529.  
  530. 函数名: cprintf
  531. 功  能: 送格式化输出至屏幕
  532. 用  法: int cprintf(const char *format[, argument, ...]);
  533. 程序例:
  534. #include <conio.h>
  535. int main(void)
  536. {
  537.    /* clear the screen */
  538.    clrscr();
  539.    /* create a text window */
  540.    window(10, 10, 80, 25);
  541.    /* output some text in the window */
  542.    cprintf("Hello worldrn");
  543.    /* wait for a key */
  544.    getch();
  545.    return 0;
  546. }
  547.  
  548.  
  549.  
  550. 函数名: cputs
  551. 功  能: 写字符到屏幕
  552. 用  法: void cputs(const char *string);
  553. 程序例:
  554. #include <conio.h>
  555. int main(void)
  556. {
  557.    /* clear the screen */
  558.    clrscr();
  559.    /* create a text window */
  560.    window(10, 10, 80, 25);
  561.    /* output some text in the window */
  562.    cputs("This is within the windowrn");
  563.    /* wait for a key */
  564.    getch();
  565.    return 0;
  566. }
  567.  
  568.  
  569.  
  570. 函数名: _creat  creat
  571. 功  能: 创建一个新文件或重写一个已存在的文件
  572. 用  法: int creat (const char *filename, int permiss);
  573. 程序例:
  574. #include <sysstat.h>
  575. #include <string.h>
  576. #include <fcntl.h>
  577. #include <io.h>
  578. int main(void)
  579. {
  580.    int handle;
  581.    char buf[11] = "0123456789";
  582.    /* change the default file mode from text to binary */
  583.    _fmode = O_BINARY;
  584.    /* create a binary file for reading and writing */
  585.    handle = creat("DUMMY.FIL", S_IREAD | S_IWRITE);
  586.    /* write 10 bytes to the file */
  587.    write(handle, buf, strlen(buf));
  588.    /* close the file */
  589.    close(handle);
  590.    return 0;
  591. }
  592.  
  593. 函数名: creatnew
  594. 功  能: 创建一个新文件
  595. 用  法: int creatnew(const char *filename, int attrib);
  596. 程序例:
  597. #include <string.h>
  598. #include <stdio.h>
  599. #include <errno.h>
  600. #include <dos.h>
  601. #include <io.h>
  602. int main(void)
  603. {
  604.    int handle;
  605.    char buf[11] = "0123456789";
  606.    /* attempt to create a file that doesn't already exist */
  607.    handle = creatnew("DUMMY.FIL", 0);
  608.    if (handle == -1)
  609.       printf("DUMMY.FIL already exists.n");
  610.    else
  611.    {
  612.       printf("DUMMY.FIL successfully created.n");
  613.       write(handle, buf, strlen(buf));
  614.       close(handle);
  615.    }
  616.    return 0;
  617. }
  618.  
  619.  
  620.  
  621. 函数名: creattemp
  622. 功  能: 创建一个新文件或重写一个已存在的文件
  623. 用  法: int creattemp(const char *filename, int attrib);
  624. 程序例:
  625. #include <string.h>
  626. #include <stdio.h>
  627. #include <io.h>
  628. int main(void)
  629. {
  630.    int handle;
  631.    char pathname[128];
  632.    strcpy(pathname, "\");
  633.    /* create a unique file in the root directory */
  634.    handle = creattemp(pathname, 0);
  635.    printf("%s was the unique file created.n", pathname);
  636.    close(handle);
  637.    return 0;
  638. }
  639.  
  640.  
  641.  
  642. 函数名: cscanf
  643. 功  能: 从控制台执行格式化输入
  644. 用  法: int cscanf(char *format[,argument, ...]);
  645. 程序例:
  646. #include <conio.h>
  647. int main(void)
  648. {
  649.    char string[80];
  650.    /* clear the screen */
  651.    clrscr();
  652.    /* Prompt the user for input */
  653.    cprintf("Enter a string with no spaces:");
  654.    /* read the input */
  655.    cscanf("%s", string);
  656.    /* display what was read */
  657.    cprintf("rnThe string entered is: %s", string);
  658.    return 0;
  659. }
  660.  
  661.  
  662.  
  663. 函数名: ctime
  664. 功  能: 把日期和时间转换为字符串
  665. 用  法: char *ctime(const time_t *time);
  666. 程序例:
  667. #include <stdio.h>
  668. #include <time.h>
  669. int main(void)
  670. {
  671.    time_t t;
  672.    time(&t);
  673.    printf("Today's date and time: %sn", ctime(&t));
  674.    return 0;
  675. }
  676.  
  677.  
  678.  
  679. 函数名: ctrlbrk
  680. 功  能: 设置Ctrl-Break处理程序
  681. 用  法: void ctrlbrk(*fptr)(void);
  682. 程序例:
  683. #include <stdio.h>
  684. #include <dos.h>
  685. #define ABORT 0
  686. int c_break(void)
  687. {
  688.    printf("Control-Break pressed.  Program aborting ...n");
  689.    return (ABORT);
  690. }
  691. int main(void)
  692. {
  693.    ctrlbrk(c_break);
  694.    for(;;)
  695.    {
  696.       printf("Looping... Press <Ctrl-Break> to quit:n");
  697.    }
  698.    return 0;
  699. }