📄 王大刚--c语言编程宝典--m.htm
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0038)http://www.hjflying.8u8.com/cl/031.htm -->
<HTML><HEAD><TITLE>王大刚-->C语言编程宝典-->M</TITLE>
<META http-equiv=Content-Type content="text/html; charset=GB2312">
<META content="王大刚 C语言编程宝典 M" name=keywords>
<META content="王大刚 - C语言编程宝典 - M" 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/032.htm">后一页</A><BR><A
href="http://www.hjflying.8u8.com/cl/030.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>M</FONT></B></CENTER>
<HR width="94%" color=#ee9b73 SIZE=1>
<BR>main()主函数 <BR>
<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> <BR>
<P>函数名: matherr <BR>功 能: 用户可修改的数学错误处理程序 <BR>用 法: int
matherr(struct exception *e); <BR>程序例: <BR>
<P>/* This is a user-defined matherr function that prevents
<BR> any error messages from being printed. */ <BR>
<P>#include<math.h> <BR>
<P>int matherr(struct exception *a) <BR>{ <BR> return 1; <BR>}
<BR> <BR> <BR> <BR>
<P>函数名: memccpy <BR>功 能: 从源source中拷贝n个字节到目标destin中 <BR>用 法:
void *memccpy(void *destin, void *source, unsigned char ch,
<BR> unsigned n); <BR>程序例: <BR>
<P>#include <string.h> <BR>#include <stdio.h> <BR>
<P>int main(void) <BR>{ <BR> char *src = "This is the source
string"; <BR> char dest[50]; <BR> char *ptr; <BR>
<P> ptr = memccpy(dest, src, 'c', strlen(src)); <BR>
<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> <BR>
<P>函数名: malloc <BR>功 能: 内存分配函数 <BR>用 法: void *malloc(unsigned
size); <BR>程序例: <BR>
<P>#include <stdio.h> <BR>#include <string.h> <BR>#include
<alloc.h> <BR>#include <process.h> <BR>
<P>int main(void) <BR>{ <BR> char *str; <BR>
<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> }
<BR>
<P> /* copy "Hello" into string */ <BR>
strcpy(str, "Hello"); <BR>
<P> /* display string */ <BR> printf("String is
%s\n", str); <BR>
<P> /* free memory */ <BR> free(str); <BR>
<P> return 0; <BR>} <BR> <BR> <BR> <BR>
<P>函数名: memchr <BR>功 能: 在数组的前n个字节中搜索字符 <BR>用 法: void
*memchr(void *s, char ch, unsigned n); <BR>程序例: <BR>
<P>#include <string.h> <BR>#include <stdio.h> <BR>
<P>int main(void) <BR>{ <BR> char str[17]; <BR>
char *ptr; <BR>
<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> <BR>
<P>函数名: memcpy <BR>功 能: 从源source中拷贝n个字节到目标destin中 <BR>用 法:
void *memcpy(void *destin, void *source, unsigned n); <BR>程序例: <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> <BR>
<P>函数名: memicmp <BR>功 能: 比较两个串s1和s2的前n个字节, 忽略大小写 <BR>用 法: int
memicmp(void *s1, void *s2, unsigned n); <BR>程序例: <BR>
<P>#include <stdio.h> <BR>#include <string.h> <BR>
<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> <BR>
<P>函数名: memmove <BR>功 能: 移动一块字节 <BR>用 法: void *memmove(void
*destin, void *source, unsigned n); <BR>程序例: <BR>
<P>#include <string.h> <BR>#include <stdio.h> <BR>
<P>int main(void) <BR>{ <BR> char *dest =
"abcdefghijklmnopqrstuvwxyz0123456789"; <BR> char *src =
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -