C语言.docx

上传人:b****6 文档编号:12101617 上传时间:2023-06-04 格式:DOCX 页数:39 大小:24.02KB
下载 相关 举报
C语言.docx_第1页
第1页 / 共39页
C语言.docx_第2页
第2页 / 共39页
C语言.docx_第3页
第3页 / 共39页
C语言.docx_第4页
第4页 / 共39页
C语言.docx_第5页
第5页 / 共39页
C语言.docx_第6页
第6页 / 共39页
C语言.docx_第7页
第7页 / 共39页
C语言.docx_第8页
第8页 / 共39页
C语言.docx_第9页
第9页 / 共39页
C语言.docx_第10页
第10页 / 共39页
C语言.docx_第11页
第11页 / 共39页
C语言.docx_第12页
第12页 / 共39页
C语言.docx_第13页
第13页 / 共39页
C语言.docx_第14页
第14页 / 共39页
C语言.docx_第15页
第15页 / 共39页
C语言.docx_第16页
第16页 / 共39页
C语言.docx_第17页
第17页 / 共39页
C语言.docx_第18页
第18页 / 共39页
C语言.docx_第19页
第19页 / 共39页
C语言.docx_第20页
第20页 / 共39页
亲,该文档总共39页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

C语言.docx

《C语言.docx》由会员分享,可在线阅读,更多相关《C语言.docx(39页珍藏版)》请在冰点文库上搜索。

C语言.docx

C语言

前面我们说的都是无参数无返回值的函数,实际程序中,我们经常使用到带参数有返回值的函数。

一、函数参数传递

    1.形式参数和实际参数

        函数的调用值把一些表达式作为参数传递给函数。

函数定义中的参数是形式参数,函数的调用者提供给函数的参数叫实际参数。

在函数调用之前,实际参数的值将被拷贝到这些形式参数中。

    2.参数传递

        先看一个例子:

            void a(int);         /*注意函数声明的形式*/

            main()

            {

                int num;

                scanf(\"%d\",&num);

                a(num);           /*注意调用形式*/

            }

            void a(int num_back)     /*注意定义形式*/

            {

                printf(\"%d\\n\",num_back);

            }

        

        在主函数中,先定义一个变量,然后输入一个值,在a()这个函数中输出。

当程序运行a(num);这一步时,把num的值赋值给num_back,在运行程序过程中,把实际参数的值传给形式参数,这就是函数参数的传递。

        形参和实参可能不只一个,如果多于一个时,函数声明、调用、定义的形式都要一一对应,不仅个数要对应,参数的数据类型也要对应。

  

            void a(int,float);

            main()

            {

                int num1;

                float num2;

                scanf(\"%d\",&num1);

                scanf(\"%f\",&num2);

                a(num1,num2);

            }

            void a(int num1_back,float num2_back)

            {

                printf(\"%d,%f\\n\",num1_back,num2_back);

            }

        

        上面的例子中,函数有两个参数,一个是整型,一个是浮点型,那么在声明、调用、定义的时候,不仅个数要一样,类型也要对应。

如果不对应,有可能使的编译错误,即使没错误,也有可能让数据传递过程中出现错误。

        再看一个例子:

            void a(int);

            main()

            {

                int num;

                scanf(\"%d\",&num);

                a(num);

            }

            void a(int num)

            {

                printf(\"%d\\n\",num);

            }

        看上面的例子,形式参数和实际参数的标识符都是num,程序把实际参数num的值传递给形式参数num。

有些人可能就不明白了,既然两个都是num,为什么还要传递呢?

干脆这样不就行了吗:

分类函数,所在函数库为ctype.h

  intisalpha(intch)若ch是字母('A'-'Z','a'-'z') 返回非0值,否则返回0

  intisalnum(intch)若ch是字母('A'-'Z','a'-'z')或数字('0'-'9') 返回非0值,否则返回0

  intisascii(intch)若ch是字符(ASCII码中的0-127)返回非0值,否则返回0

  intiscntrl(intch)若ch是作废字符(0x7F)或普通控制字符(0x00-0x1F) 返回非0值,否则返回0

  intisdigit(intch)若ch是数字('0'-'9')返回非0值,否则返回0

  intisgraph(intch)若ch是可打印字符(不含空格)(0x21-0x7E)返回非0值,否则返回0

  intislower(intch)若ch是小写字母('a'-'z')返回非0值,否则返回0

  intisprint(intch)若ch是可打印字符(含空格)(0x20-0x7E)返回非0值,否则返回0

  intispunct(intch)若ch是标点字符(0x00-0x1F)返回非0值,否则返回0

  intisspace(intch)若ch是空格(''),水平制表符('\t'),回车符('\r'),走纸换行('\f'),垂直制表符('\v'),换行符('\n')返回非0值,否则返回0

  intisupper(intch)若ch是大写字母('A'-'Z')返回非0值,否则返回0

  intisxdigit(intch)若ch是16进制数('0'-'9','A'-'F','a'-'f')返回非0值,否则返回0

  inttolower(intch)若ch是大写字母('A'-'Z')返回相应的小写字母('a'-'z')

  inttoupper(intch)若ch是小写字母('a'-'z')返回相应的大写字母('A'-'Z')

函数名:

abort

  功能:

异常终止一个进程

  用法:

voidabort(void);

  程序例:

  #include

#include

intmain(void)

{

printf("Callingabort()\n");

abort();

return0;/*Thisisneverreached*/

}

  函数名:

abs

  功能:

求整数的绝对值

  用法:

intabs(inti);

  程序例:

  #include

#include

intmain(void)

{

intnumber=-1234;

printf("number:

%dabsolutevalue:

%d\n",number,abs(number));

return0;

}

  函数名:

absread,abswirte

  功能:

绝对磁盘扇区读、写数据

  用法:

intabsread(intdrive,intnsects,intsectno,void*buffer);

  intabswrite(intdrive,intnsects,intsectno,void*buffer);

  程序例:

  /*absreadexample*/

#include

#include

#include

#include

intmain(void)

{

inti,strt,ch_out,sector;

charbuf[512];

printf("InsertadisketteintodriveAandpressanykey\n");

getch();

sector=0;

if(absread(0,1,sector,&buf)!

=0)

{

perror("Diskproblem");

exit

(1);

}

printf("ReadOK\n");

strt=3;

for(i=0;i<80;i++)

{

ch_out=buf[strt+i];

putchar(ch_out);

}

printf("\n");

return(0);

}

  函数名:

access

  功能:

确定文件的访问权限

  用法:

intaccess(constchar*filename,intamode);

函数名:

bar

  功能:

画一个二维条形图

  用法:

voidfarbar(intleft,inttop,intright,intbottom);

  程序例:

  #include

#include

#include

#include

intmain(void)

{

/*requestautodetection*/

intgdriver=DETECT,gmode,errorcode;

intmidx,midy,i;

/*initializegraphicsandlocalvariables*/

initgraph(&gdriver,&gmode,"");

/*readresultofinitialization*/

errorcode=graphresult();

if(errorcode!

=grOk)/*anerroroccurred*/

{

printf("Graphicserror:

%s\n",grapherrormsg(errorcode));

printf("Pressanykeytohalt:

");

getch();

exit

(1);/*terminatewithanerrorcode*/

}

midx=getmaxx()/2;

midy=getmaxy()/2;

/*loopthroughthefillpatterns*/

for(i=SOLID_FILL;i

{

/*setthefillstyle*/

setfillstyle(i,getmaxcolor());

/*drawthebar*/

bar(midx-50,midy-50,midx+50,

midy+50);

getch();

}

/*cleanup*/

closegraph();

return0;

}

  函数名:

bar3d

  功能:

画一个三维条形图

  用法:

voidfarbar3d(intleft,inttop,intright,intbottom,

  intdepth,inttopflag);

  程序例:

  #include

#include

#include

#include

intmain(void)

{

/*requestautodetection*/

intgdriver=DETECT,gmode,errorcode;

intmidx,midy,i;

/*initializegraphics,localvariables*/

initgraph(&gdriver,&gmode,"");

/*readresultofinitialization*/

errorcode=graphresult();

if(errorcode!

=grOk)/*anerroroccurred*/

{

printf("Graphicserror:

%s\n",grapherrormsg(errorcode));

printf("Pressanykeytohalt:

");

getch();

exit

(1);/*terminatewitherrorcode*/

}

midx=getmaxx()/2;

midy=getmaxy()/2;

/*loopthroughthefillpatterns*/

for(i=EMPTY_FILL;i

{

/*setthefillstyle*/

setfillstyle(i,getmaxcolor());

/*drawthe3-dbar*/

bar3d(midx-50,midy-50,midx+50,midy+50,10,1);

getch();

}

/*cleanup*/

closegraph();

return0;

}

函数名:

cabs

  功能:

计算复数的绝对值

  用法:

doublecabs(structcomplexz);

  程序例:

  #include

#include

intmain(void)

{

structcomplexz;

doubleval;

z.x=2.0;

z.y=1.0;

val=cabs(z);

printf("Theabsolutevalueof%.2lfi%.2lfjis%.2lf",z.x,z.y,val);

return0;

}

  函数名:

calloc

  功能:

分配主存储器

  用法:

void*calloc(size_tnelem,size_telsize);

  程序例:

  #include

#include

intmain(void)

{

char*str=NULL;

/*allocatememoryforstring*/

str=calloc(10,sizeof(char));

/*copy"Hello"intostring*/

strcpy(str,"Hello");

/*displaystring*/

printf("Stringis%s\n",str);

/*freememory*/

free(str);

return0;

}

  函数名:

ceil

  功能:

向上舍入

  用法:

doubleceil(doublex);

  程序例:

  #include

#include

intmain(void)

{

doublenumber=123.54;

doubledown,up;

down=floor(number);

up=ceil(number);

printf("originalnumber%5.2lf\n",number);

printf("numberroundeddown%5.2lf\n",down);

printf("numberroundedup%5.2lf\n",up);

return0;

}

  函数名:

cgets

  功能:

从控制台读字符串

  用法:

char*cgets(char*str);

  程序例:

  #include

#include

intmain(void)

{

charbuffer[83];

char*p;

/*There'sspacefor80charactersplustheNULLterminator*/

buffer[0]=81;

printf("Inputsomechars:

");

p=cgets(buffer);

printf("\ncgetsread%dcharacters:

\"%s\"\n",buffer[1],p);

printf("Thereturnedpointeris%p,buffer[0]isat%p\n",p,&buffer);

/*Leaveroomfor5charactersplustheNULLterminator*/

buffer[0]=6;

printf("Inputsomechars:

");

p=cgets(buffer);

printf("\ncgetsread%dcharacters:

\"%s\"\n",buffer[1],p);

printf("Thereturnedpointeris%p,buffer[0]isat%p\n",p,&buffer);

return0;

}

函数名:

delay

  功能:

将程序的执行暂停一段时间(毫秒)

  用法:

voiddelay(unsignedmilliseconds);

  程序例:

  /*Emitsa440-Hztonefor500milliseconds*/

#include

intmain(void)

{

sound(440);

delay(500);

nosound();

return0;

}

  函数名:

delline

  功能:

在文本窗口中删去一行

  用法:

voiddelline(void);

  程序例:

  #include

intmain(void)

{

clrscr();

cprintf("ThefunctionDELLINEdeletes\

thelinecontainingthe\r\n");

cprintf("cursorandmovesalllines\

belowitonelineup.\r\n");

cprintf("DELLINEoperateswithinthe\

currentlyactivetext\r\n");

cprintf("window.Pressanykeyto\

continue...");

gotoxy(1,2);/*Movethecursortothe

secondlineandfirstcolumn*/

getch();

delline();

getch();

return0;

}

  函数名:

detectgraph

  功能:

通过检测硬件确定图形驱动程序和模式

  用法:

voidfardetectgraph(intfar*graphdriver,intfar*graphmode);

  程序例:

  #include

#include

#include

#include

/*namesofthevariouscardssupported*/

char*dname[]={"requestsdetection",

"aCGA",

"anMCGA",

"anEGA",

"a64KEGA",

"amonochromeEGA",

"anIBM8514",

"aHerculesmonochrome",

"anAT&T6300PC",

"aVGA",

"anIBM3270PC"

};

intmain(void)

{

/*returnsdetectedhardwareinfo.*/

intgdriver,gmode,errorcode;

/*detectgraphicshardwareavailable*/

detectgraph(&gdriver,&gmode);

/*readresultofdetectgraphcall*/

errorcode=graphresult();

if(errorcode!

=grOk)/*anerror

occurred*/

{

printf("Graphicserror:

%s\n",\

grapherrormsg(errorcode));

printf("Pressanykeytohalt:

");

getch();

exit

(1);/*terminatewithanerror

code*/

}

/*displaytheinformationdetected*/

clrscr();

printf("Youhave%svideodisplay\

card.\n",dname[gdriver]);

printf("Pressanykeytohalt:

");

getch();

return0;

}

函数名:

ecvt

  功能:

把一个浮点数转换为字符串

  用法:

charecvt(doublevalue,intndigit,int*decpt,int*sign);

  程序例:

  #include

#include

#include

intmain(void)

{

char*string;

doublevalue;

intdec,sign;

intndig=10;

clrscr();

value=9.876;

string=ecvt(value,ndig,&dec,&sign);

printf("string=%sdec=%d\

sign=%d\n",string,dec,sign);

value=-123.45;

ndig=15;

string=ecvt(value,ndig,&dec,&sign);

printf("string=%sdec=%dsign=%d\n",

string,dec,sign);

value=0.6789e5;/*scientific

notation*/

ndig=5;

string=ecvt(value,ndig,&dec,&sign);

printf("string=%sdec=%d\

sign=%d\n",string,dec,sign);

return0;

}

  函数名:

ellipse

  功能:

画一椭圆

  用法:

voidfarellipse(intx,inty,intstangle,intendangle,

  intxradius,intyradius);

  程序例:

  #include

#include

#include

#include

intmain(void)

{

/*requestautodetection*/

intgdriver=DETECT,gmode,errorcode;

intmidx,midy;

intstangle=0,endangle=360;

intxradius=100,yradius=50;

/*initializegraphics,localvariables*/

initgraph(&gdriver,&gmode,"");

/*readresult

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 自然科学 > 物理

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2