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

其他书籍

开发平台:

CHM

  1. main()主函数 
  2.     每一C 程序都必须有一main()函数, 可以根据自己的爱好把它放在程序的某个地方。有些程序员把它放在最前面, 而另一些程序员把它放在最后面, 无论放在哪个地方, 以下几点说明都是适合的。
  3.     1. main() 参数
  4.     在Turbo C2.0启动过程中, 传递main()函数三个参数: argc, argv和env。
  5.      * argc:  整数, 为传给main()的命令行参数个数。
  6.      * argv:  字符串数组。
  7.               在DOS 3.X 版本中, argv[0] 为程序运行的全路径名; 对DOS 3.0              以下的版本, argv[0]为空串("") 。
  8.               argv[1] 为在DOS命令行中执行程序名后的第一个字符串;
  9.               argv[2] 为执行程序名后的第二个字符串;
  10.               ...
  11.               argv[argc]为NULL。
  12.      *env:  安符串数组。env[] 的每一个元素都包含ENVVAR=value形式的字符串。其中ENVVAR为环境变量如PATH或87。value 为ENVVAR的对应值如C:DOS, C:TURBOC(对于PATH) 或YES(对于87)。
  13.     Turbo C2.0启动时总是把这三个参数传递给main()函数, 可以在用户程序中说明(或不说明)它们, 如果说明了部分(或全部)参数, 它们就成为main()子程序的局部变量。
  14.     请注意: 一旦想说明这些参数, 则必须按argc, argv, env 的顺序, 如以下的例子:
  15.      main()
  16.      main(int argc)
  17.      main(int argc, char *argv[])
  18.      main(int argc, char *argv[], char *env[])
  19.     其中第二种情况是合法的, 但不常见, 因为在程序中很少有只用argc, 而不用argv[]的情况。
  20.     以下提供一样例程序EXAMPLE.EXE,  演示如何在main()函数中使用三个参数:
  21.      /*program name EXAMPLE.EXE*/
  22.      #include <stdio.h>
  23.      #include <stdlib.h>
  24.      main(int argc, char *argv[], char *env[])
  25.      {
  26.           int i;
  27.           printf("These are the %d  command- line  arguments passed  to
  28.                   main:nn", argc);
  29.           for(i=0; i<=argc; i++)
  30.             printf("argv[%d]:%sn", i, argv[i]);
  31.           printf("nThe environment string(s)on this system are:nn");
  32.           for(i=0; env[i]!=NULL; i++)
  33.                printf(" env[%d]:%sn", i, env[i]);
  34.      }
  35.     如果在DOS 提示符下, 按以下方式运行EXAMPLE.EXE:
  36.     C:example first_argument "argument with blanks"  3  4  "last  butone" stop!
  37.     注意: 可以用双引号括起内含空格的参数, 如本例中的:   "  argumentwith blanks"和"Last but one")。
  38.     结果是这样的:
  39.      The value of argc is 7
  40.      These are the 7 command-linearguments passed to main:
  41.      argv[0]:C:TURBOEXAMPLE.EXE
  42.      argv[1]:first_argument
  43.      argv[2]:argument with blanks
  44.      argv[3]:3
  45.      argv[4]:4
  46.      argv[5]:last but one
  47.      argv[6]:stop!
  48.      argv[7]:(NULL)
  49.      The environment string(s) on this system are:
  50.      env[0]: COMSPEC=C:COMMAND.COM
  51.      env[1]: PROMPT=$P$G            /*视具体设置而定*/
  52.      env[2]: PATH=C:DOS;C:TC      /*视具体设置而定*/
  53.  
  54.      应该提醒的是: 传送main() 函数的命令行参数的最大长度为128 个字符 (包括参数间的空格),  这是由DOS 限制的。
  55.  
  56. 函数名: matherr
  57. 功  能: 用户可修改的数学错误处理程序
  58. 用  法: int matherr(struct exception *e);
  59. 程序例:
  60. /* This is a user-defined matherr function that prevents
  61.    any error messages from being printed. */
  62. #include<math.h>
  63. int matherr(struct exception *a)
  64. {
  65.    return 1;
  66. }
  67.  
  68.  
  69.  
  70. 函数名: memccpy
  71. 功  能: 从源source中拷贝n个字节到目标destin中
  72. 用  法: void *memccpy(void *destin, void *source, unsigned char ch,   unsigned n);
  73. 程序例:
  74. #include <string.h>
  75. #include <stdio.h>
  76. int main(void)
  77. {
  78.    char *src = "This is the source string";
  79.    char dest[50];
  80.    char *ptr;
  81.    ptr = memccpy(dest, src, 'c', strlen(src));
  82.    if (ptr)
  83.    {
  84.       *ptr = '';
  85.       printf("The character was found:  %sn", dest);
  86.    }
  87.    else
  88.       printf("The character wasn't foundn");
  89.    return 0;
  90. }
  91.  
  92.  
  93. 函数名: malloc
  94. 功  能: 内存分配函数
  95. 用  法: void *malloc(unsigned size);
  96. 程序例:
  97. #include <stdio.h>
  98. #include <string.h>
  99. #include <alloc.h>
  100. #include <process.h>
  101. int main(void)
  102. {
  103.    char *str;
  104.    /* allocate memory for string */
  105.    /* This will generate an error when compiling */
  106.    /* with C++, use the new operator instead. */
  107.    if ((str = malloc(10)) == NULL)
  108.    {
  109.       printf("Not enough memory to allocate buffern");
  110.       exit(1);  /* terminate program if out of memory */
  111.    }
  112.    /* copy "Hello" into string */
  113.    strcpy(str, "Hello");
  114.    /* display string */
  115.    printf("String is %sn", str);
  116.    /* free memory */
  117.    free(str);
  118.    return 0;
  119. }
  120.  
  121.  
  122.  
  123. 函数名: memchr
  124. 功  能: 在数组的前n个字节中搜索字符
  125. 用  法: void *memchr(void *s, char ch, unsigned n);
  126. 程序例:
  127. #include <string.h>
  128. #include <stdio.h>
  129. int main(void)
  130. {
  131.    char str[17];
  132.    char *ptr;
  133.    strcpy(str, "This is a string");
  134.    ptr = memchr(str, 'r', strlen(str));
  135.    if (ptr)
  136.       printf("The character 'r' is at position: %dn", ptr - str);
  137.    else
  138.       printf("The character was not foundn");
  139.    return 0;
  140. }
  141.  
  142. 函数名: memcpy
  143. 功  能: 从源source中拷贝n个字节到目标destin中
  144. 用  法: void *memcpy(void *destin, void *source, unsigned n);
  145. 程序例:
  146. #include <stdio.h>
  147. #include <string.h>
  148. int main(void)
  149. {
  150.    char src[] = "******************************";
  151.    char dest[] = "abcdefghijlkmnopqrstuvwxyz0123456709";
  152.    char *ptr;
  153.    printf("destination before memcpy: %sn", dest);
  154.    ptr = memcpy(dest, src, strlen(src));
  155.    if (ptr)
  156.       printf("destination after memcpy:  %sn", dest);
  157.    else
  158.       printf("memcpy failedn");
  159.    return 0;
  160. }
  161.  
  162.  
  163. 函数名: memicmp
  164. 功  能: 比较两个串s1和s2的前n个字节, 忽略大小写
  165. 用  法: int memicmp(void *s1, void *s2, unsigned n);
  166. 程序例:
  167. #include <stdio.h>
  168. #include <string.h>
  169. int main(void)
  170. {
  171.    char *buf1 = "ABCDE123";
  172.    char *buf2 = "abcde456";
  173.    int stat;
  174.    stat = memicmp(buf1, buf2, 5);
  175.    printf("The strings to position 5 are ");
  176.    if (stat)
  177.       printf("not ");
  178.    printf("the samen");
  179.    return 0;
  180. }
  181.  
  182.  
  183. 函数名: memmove
  184. 功  能: 移动一块字节
  185. 用  法: void *memmove(void *destin, void *source, unsigned n);
  186. 程序例:
  187. #include <string.h>
  188. #include <stdio.h>
  189. int main(void)
  190. {
  191.   char *dest = "abcdefghijklmnopqrstuvwxyz0123456789";
  192.   char *src = "******************************";
  193.   printf("destination prior to memmove: %sn", dest);
  194.   memmove(dest, src, 26);
  195.   printf("destination after memmove:    %sn", dest);
  196.   return 0;
  197. }
  198.  
  199.  
  200.  
  201. 函数名: memset
  202. 功  能: 设置s中的所有字节为ch, s数组的大小由n给定
  203. 用  法: void *memset(void *s, char ch, unsigned n);
  204. 程序例:
  205. #include <string.h>
  206. #include <stdio.h>
  207. #include <mem.h>
  208. int main(void)
  209. {
  210.    char buffer[] = "Hello worldn";
  211.    printf("Buffer before memset: %sn", buffer);
  212.    memset(buffer, '*', strlen(buffer) - 1);
  213.    printf("Buffer after memset:  %sn", buffer);
  214.    return 0;
  215. }
  216.  
  217.  
  218. 函数名: mkdir
  219. 功  能: 建立一个目录
  220. 用  法: int mkdir(char *pathname);
  221. 程序例:
  222. #include <stdio.h>
  223. #include <conio.h>
  224. #include <process.h>
  225. #include <dir.h>
  226. int main(void)
  227. {
  228.   int status;
  229.    clrscr();
  230.    status = mkdir("asdfjklm");
  231.    (!status) ? (printf("Directory createdn")) :
  232.                (printf("Unable to create directoryn"));
  233.    getch();
  234.    system("dir");
  235.    getch();
  236.    status = rmdir("asdfjklm");
  237.    (!status) ? (printf("Directory deletedn")) :
  238.   (perror("Unable to delete directory"));
  239.    return 0;
  240. }
  241.  
  242.  
  243.  
  244. 函数名: mktemp
  245. 功  能: 建立唯一的文件名
  246. 用  法: char *mktemp(char *template);
  247. 程序例:
  248. #include <dir.h>
  249. #include <stdio.h>
  250. int main(void)
  251. {
  252.    /* fname defines the template for the
  253.      temporary file.  */
  254.    char *fname = "TXXXXXX", *ptr;
  255.    ptr = mktemp(fname);
  256.    printf("%sn",ptr);
  257.    return 0;
  258. }
  259.  
  260.  
  261. 函数名: MK_FP
  262. 功  能: 设置一个远指针
  263. 用  法: void far *MK_FP(unsigned seg, unsigned off);
  264. 程序例:
  265. #include <dos.h>
  266. #include <graphics.h>
  267. int main(void)
  268. {
  269.    int gd, gm, i;
  270.    unsigned int far *screen;
  271.    detectgraph(&gd, &gm);
  272.    if (gd == HERCMONO)
  273.        screen = MK_FP(0xB000, 0);
  274.    else
  275.        screen = MK_FP(0xB800, 0);
  276.    for (i=0; i<26; i++)
  277.       screen[i] = 0x0700 + ('a' + i);
  278.    return 0;
  279. }
  280.  
  281.  
  282. 函数名: modf
  283. 功  能: 把数分为指数和尾数
  284. 用  法: double modf(double value, double *iptr);
  285. 程序例:
  286. #include <math.h>
  287. #include <stdio.h>
  288. int main(void)
  289. {
  290.    double fraction, integer;
  291.    double number = 100000.567;
  292.    fraction = modf(number, &integer);
  293.    printf("The whole and fractional parts of %lf are %lf and %lfn",
  294.           number, integer, fraction);
  295.    return 0;
  296. }
  297.  
  298.  
  299. 函数名: movedata
  300. 功  能: 拷贝字节
  301. 用  法: void movedata(int segsrc, int offsrc, int segdest,
  302.   int offdest, unsigned numbytes);
  303. 程序例:
  304. #include <mem.h>
  305. #define MONO_BASE 0xB000
  306. /* saves the contents of the monochrome screen in buffer */
  307. void save_mono_screen(char near *buffer)
  308. {
  309.    movedata(MONO_BASE, 0, _DS, (unsigned)buffer, 80*25*2);
  310. }
  311. int main(void)
  312. {
  313.    char buf[80*25*2];
  314.    save_mono_screen(buf);
  315. }
  316.  
  317.  
  318. 函数名: moverel
  319. 功  能: 将当前位置(CP)移动一相对距离
  320. 用  法: void far moverel(int dx, int dy);
  321. 程序例:
  322. #include <graphics.h>
  323. #include <stdlib.h>
  324. #include <stdio.h>
  325. #include <conio.h>
  326. int main(void)
  327. {
  328.    /* request auto detection */
  329.    int gdriver = DETECT, gmode, errorcode;
  330.    char msg[80];
  331.    /* initialize graphics and local variables */
  332.    initgraph(&gdriver, &gmode, "");
  333.    /* read result of initialization */
  334.    errorcode = graphresult();
  335.    if (errorcode != grOk)  /* an error occurred */
  336.    {
  337.       printf("Graphics error: %sn", grapherrormsg(errorcode));
  338.       printf("Press any key to halt:");
  339.       getch();
  340.       exit(1); /* terminate with an error code */
  341.    }
  342.    /* move the C.P. to location (20, 30) */
  343.    moveto(20, 30);
  344.    /* plot a pixel at the C.P. */
  345.    putpixel(getx(), gety(), getmaxcolor());
  346.    /* create and output a message at (20, 30) */
  347.    sprintf(msg, " (%d, %d)", getx(), gety());
  348.    outtextxy(20, 30, msg);
  349.    /* move to a point a relative distance */
  350.    /* away from the current value of C.P. */
  351.    moverel(100, 100);
  352.    /* plot a pixel at the C.P. */
  353.    putpixel(getx(), gety(), getmaxcolor());
  354.    /* create and output a message at C.P. */
  355.    sprintf(msg, " (%d, %d)", getx(), gety());
  356.    outtext(msg);
  357.    /* clean up */
  358.    getch();
  359.    closegraph();
  360.    return 0;
  361. }
  362.  
  363.  
  364. 函数名: movetext
  365. 功  能: 将屏幕文本从一个矩形区域拷贝到另一个矩形区域
  366. 用  法: int movetext(int left, int top, int right, int bottom,
  367.   int newleft, int newtop);
  368. 程序例:
  369. #include <conio.h>
  370. #include <string.h>
  371. int main(void)
  372. {
  373.    char *str = "This is a test string";
  374.    clrscr();
  375.    cputs(str);
  376.    getch();
  377.    movetext(1, 1, strlen(str), 2, 10, 10);
  378.    getch();
  379.    return 0;
  380. }
  381.  
  382.  
  383. 函数名: moveto
  384. 功  能: 将CP移到(x, y)
  385. 用  法: void far moveto(int x, int y);
  386. 程序例:
  387. #include <graphics.h>
  388. #include <stdlib.h>
  389. #include <stdio.h>
  390. #include <conio.h>
  391. int main(void)
  392. {
  393.    /* request auto detection */
  394.    int gdriver = DETECT, gmode, errorcode;
  395.    char msg[80];
  396.    /* initialize graphics and local variables */
  397.    initgraph(&gdriver, &gmode, "");
  398.    /* read result of initialization */
  399.    errorcode = graphresult();
  400.    if (errorcode != grOk)  /* an error occurred */
  401.    {
  402.       printf("Graphics error: %sn", grapherrormsg(errorcode));
  403.       printf("Press any key to halt:");
  404.       getch();
  405.       exit(1); /* terminate with an error code */
  406.    }
  407.    /* move the C.P. to location (20, 30) */
  408.    moveto(20, 30);
  409.    /* plot a pixel at the C.P. */
  410.    putpixel(getx(), gety(), getmaxcolor());
  411.    /* create and output a message at (20, 30) */
  412.    sprintf(msg, " (%d, %d)", getx(), gety());
  413.    outtextxy(20, 30, msg);
  414.    /* move to (100, 100) */
  415.    moveto(100, 100);
  416.    /* plot a pixel at the C.P. */
  417.    putpixel(getx(), gety(), getmaxcolor());
  418.    /* create and output a message at C.P. */
  419.    sprintf(msg, " (%d, %d)", getx(), gety());
  420.    outtext(msg);
  421.    /* clean up */
  422.    getch();
  423.    closegraph();
  424.    return 0;
  425. }
  426.  
  427.  
  428. 函数名: movemem
  429. 功  能: 移动一块字节
  430. 用  法: void movemem(void *source, void *destin, unsigned len);
  431. 程序例:
  432. #include <mem.h>
  433. #include <alloc.h>
  434. #include <stdio.h>
  435. #include <string.h>
  436. int main(void)
  437. {
  438.    char *source = "Borland International";
  439.    char *destination;
  440.    int length;
  441.    length = strlen(source);
  442.    destination = malloc(length + 1);
  443.    movmem(source,destination,length);
  444.    printf("%sn",destination);
  445.    return 0;
  446. }
  447.  
  448.  
  449. 函数名: normvideo
  450. 功  能: 选择正常亮度字符
  451. 用  法: void normvideo(void);
  452. 程序例:
  453. #include <conio.h>
  454. int main(void)
  455. {
  456.    normvideo();
  457.    cprintf("NORMAL Intensity Textrn");
  458.    return 0;
  459. }
  460.  
  461.  
  462. 函数名: nosound
  463. 功  能: 关闭PC扬声器
  464. 用  法: void nosound(void);
  465. 程序例:
  466. /* Emits a 7-Hz tone for 10 seconds.
  467.      True story: 7 Hz is the resonant frequency of a chicken's skull cavity.
  468.      This was determined empirically in Australia, where a new factory
  469.      generating 7-Hz tones was located too close to a chicken ranch:
  470.      When the factory started up, all the chickens died.
  471.      Your PC may not be able to emit a 7-Hz tone.
  472. */
  473. int main(void)
  474. {
  475.    sound(7);
  476.    delay(10000);
  477.    nosound();
  478. }