?? fm.htm
字號:
<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>fm</TITLE>
</HEAD>
<BODY>
main()主函數
<P> 每一C 程序都必須有一main()函數, 可以根據自己的愛好把它放在程序的某
<BR>個地方。有些程序員把它放在最前面, 而另一些程序員把它放在最后面, 無論放
<BR>在哪個地方, 以下幾點說明都是適合的。
<BR> 1. main() 參數
<BR> 在Turbo C2.0啟動過程中, 傳遞main()函數三個參數:
argc, argv和env。
<BR> * argc: 整數, 為傳給main()的命令行參數個數。
<BR> * argv: 字符串數組。
<BR>
在DOS 3.X 版本中, argv[0] 為程序運行的全路徑名; 對DOS 3.0
<BR>
以下的版本, argv[0]為空串("") 。
<BR>
argv[1] 為在DOS命令行中執行程序名后的第一個字符串;
<BR>
argv[2] 為執行程序名后的第二個字符串;
<BR>
...
<BR>
argv[argc]為NULL。
<BR> *env: 安符串數組。env[] 的每一個元素都包含ENVVAR=value形式的字符
<BR>串。其中ENVVAR為環境變量如PATH或87。value 為ENVVAR的對應值如C:\DOS,
C:
<BR>\TURBOC(對于PATH) 或YES(對于87)。
<BR> Turbo C2.0啟動時總是把這三個參數傳遞給main()函數,
可以在用戶程序中
<BR>說明(或不說明)它們, 如果說明了部分(或全部)參數, 它們就成為main()子程序
<BR>的局部變量。
<BR> 請注意: 一旦想說明這些參數, 則必須按argc, argv,
env 的順序, 如以下
<BR>的例子:
<BR> main()
<BR> main(int argc)
<BR> main(int argc, char *argv[])
<BR> main(int argc, char *argv[], char *env[])
<BR> 其中第二種情況是合法的, 但不常見, 因為在程序中很少有只用argc,
而不
<BR>用argv[]的情況。
<BR> 以下提供一樣例程序EXAMPLE.EXE, 演示如何在main()函數中使用三個參數:
<BR> /*program name EXAMPLE.EXE*/
<BR> #include <stdio.h>
<BR> #include <stdlib.h>
<BR> main(int argc, char *argv[], char *env[])
<BR> {
<BR> int i;
<BR> printf("These
are the %d command- line arguments passed to
<BR>
main:\n\n", argc);
<BR> for(i=0; i<=argc;
i++)
<BR>
printf("argv[%d]:%s\n", i, argv[i]);
<BR> printf("\nThe
environment string(s)on this system are:\n\n");
<BR> for(i=0; env[i]!=NULL;
i++)
<BR>
printf(" env[%d]:%s\n", i, env[i]);
<BR> }
<BR> 如果在DOS 提示符下, 按以下方式運行EXAMPLE.EXE:
<BR> C:\example first_argument "argument with blanks"
3 4 "last but
<BR>one" stop!
<BR> 注意: 可以用雙引號括起內含空格的參數, 如本例中的:
" argument
<BR>with blanks"和"Last but one")。
<BR> 結果是這樣的:
<BR> The value of argc is 7
<BR> These are the 7 command-linearguments passed
to main:
<BR> argv[0]:C:\TURBO\EXAMPLE.EXE
<BR> argv[1]:first_argument
<BR> argv[2]:argument with blanks
<BR> argv[3]:3
<BR> argv[4]:4
<BR> argv[5]:last but one
<BR> argv[6]:stop!
<BR> argv[7]:(NULL)
<BR> The environment string(s) on this system are:
<BR> env[0]: COMSPEC=C:\COMMAND.COM
<BR> env[1]: PROMPT=$P$G
/*視具體設置而定*/
<BR> env[2]: PATH=C:\DOS;C:\TC
/*視具體設置而定*/
<BR>
<BR> 應該提醒的是: 傳送main() 函數的命令行參數的最大長度為128
個字符 (包
<BR>括參數間的空格), 這是由DOS 限制的。
<BR>
<P>函數名: matherr
<BR>功 能: 用戶可修改的數學錯誤處理程序
<BR>用 法: int matherr(struct exception *e);
<BR>程序例:
<P>/* This is a user-defined matherr function that prevents
<BR> any error messages from being printed. */
<P>#include<math.h>
<P>int matherr(struct exception *a)
<BR>{
<BR> return 1;
<BR>}
<BR>
<BR>
<BR>
<P>函數名: memccpy
<BR>功 能: 從源source中拷貝n個字節到目標destin中
<BR>用 法: void *memccpy(void *destin, void *source, unsigned char
ch,
<BR> unsigned n);
<BR>程序例:
<P>#include <string.h>
<BR>#include <stdio.h>
<P>int main(void)
<BR>{
<BR> char *src = "This is the source string";
<BR> char dest[50];
<BR> char *ptr;
<P> ptr = memccpy(dest, src, 'c', strlen(src));
<P> if (ptr)
<BR> {
<BR> *ptr = '\0';
<BR> printf("The character was found:
%s\n", dest);
<BR> }
<BR> else
<BR> printf("The character wasn't found\n");
<BR> return 0;
<BR>}
<BR>
<BR>
<P>函數名: malloc
<BR>功 能: 內存分配函數
<BR>用 法: void *malloc(unsigned size);
<BR>程序例:
<P>#include <stdio.h>
<BR>#include <string.h>
<BR>#include <alloc.h>
<BR>#include <process.h>
<P>int main(void)
<BR>{
<BR> char *str;
<P> /* allocate memory for string */
<BR> /* This will generate an error when compiling */
<BR> /* with C++, use the new operator instead. */
<BR> if ((str = malloc(10)) == NULL)
<BR> {
<BR> printf("Not enough memory to allocate
buffer\n");
<BR> exit(1); /* terminate program
if out of memory */
<BR> }
<P> /* copy "Hello" into string */
<BR> strcpy(str, "Hello");
<P> /* display string */
<BR> printf("String is %s\n", str);
<P> /* free memory */
<BR> free(str);
<P> return 0;
<BR>}
<BR>
<BR>
<BR>
<P>函數名: memchr
<BR>功 能: 在數組的前n個字節中搜索字符
<BR>用 法: void *memchr(void *s, char ch, unsigned n);
<BR>程序例:
<P>#include <string.h>
<BR>#include <stdio.h>
<P>int main(void)
<BR>{
<BR> char str[17];
<BR> char *ptr;
<P> strcpy(str, "This is a string");
<BR> ptr = memchr(str, 'r', strlen(str));
<BR> if (ptr)
<BR> printf("The character 'r' is at position:
%d\n", ptr - str);
<BR> else
<BR> printf("The character was not found\n");
<BR> return 0;
<BR>}
<BR>
<P>函數名: memcpy
<BR>功 能: 從源source中拷貝n個字節到目標destin中
<BR>用 法: void *memcpy(void *destin, void *source, unsigned n);
<BR>程序例:
<P>#include <stdio.h>
<BR>#include <string.h>
<BR>int main(void)
<BR>{
<BR> char src[] = "******************************";
<BR> char dest[] = "abcdefghijlkmnopqrstuvwxyz0123456709";
<BR> char *ptr;
<BR> printf("destination before memcpy: %s\n", dest);
<BR> ptr = memcpy(dest, src, strlen(src));
<BR> if (ptr)
<BR> printf("destination after memcpy:
%s\n", dest);
<BR> else
<BR> printf("memcpy failed\n");
<BR> return 0;
<BR>}
<BR>
<BR>
<P>函數名: memicmp
<BR>功 能: 比較兩個串s1和s2的前n個字節, 忽略大小寫
<BR>用 法: int memicmp(void *s1, void *s2, unsigned n);
<BR>程序例:
<P>#include <stdio.h>
<BR>#include <string.h>
<P>int main(void)
<BR>{
<BR> char *buf1 = "ABCDE123";
<BR> char *buf2 = "abcde456";
<BR> int stat;
<BR> stat = memicmp(buf1, buf2, 5);
<BR> printf("The strings to position 5 are ");
<BR> if (stat)
<BR> printf("not ");
<BR> printf("the same\n");
<BR> return 0;
<BR>}
<BR>
<BR>
<P>函數名: memmove
<BR>功 能: 移動一塊字節
<BR>用 法: void *memmove(void *destin, void *source, unsigned n);
<BR>程序例:
<P>#include <string.h>
<BR>#include <stdio.h>
<P>int main(void)
<BR>{
<BR> char *dest = "abcdefghijklmnopqrstuvwxyz0123456789";
<BR> char *src = "******************************";
<BR> printf("destination prior to memmove: %s\n", dest);
<BR> memmove(dest, src, 26);
<BR> printf("destination after memmove: %s\n",
dest);
<BR> return 0;
<BR>}
<BR>
<BR>
<BR>
<P>函數名: memset
<BR>功 能: 設置s中的所有字節為ch, s數組的大小由n給定
<BR>用 法: void *memset(void *s, char ch, unsigned n);
<BR>程序例:
<P>#include <string.h>
<BR>#include <stdio.h>
<BR>#include <mem.h>
<P>int main(void)
<BR>{
<BR> char buffer[] = "Hello world\n";
<P> printf("Buffer before memset: %s\n", buffer);
<BR> memset(buffer, '*', strlen(buffer) - 1);
<BR> printf("Buffer after memset: %s\n", buffer);
<BR> return 0;
<BR>}
<BR>
<BR>
<P>函數名: mkdir
<BR>功 能: 建立一個目錄
<BR>用 法: int mkdir(char *pathname);
<BR>程序例:
<P>#include <stdio.h>
<BR>#include <conio.h>
<BR>#include <process.h>
<BR>#include <dir.h>
<P>int main(void)
<BR>{
<BR> int status;
<P> clrscr();
<BR> status = mkdir("asdfjklm");
<BR> (!status) ? (printf("Directory created\n")) :
<BR>
(printf("Unable to create directory\n"));
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -