当前位置: 首页 > 图文教程 > 开发语言 > C/C++ > C/C++:小编谈C语言函数那些事(5)

C/C++
2009年编程开发语言的使用率
C++对象模型笔记:dynamic binding
cstl -- c语言编写通用数据结构和常用算法库(模仿SGI STL)
子串匹配:不回溯算法
C++ Builder 访问 USB 口的方法
C++中二维数组的动态分配
数组和指针在编译的时候的区别
如何利用doxygen生成pdf文档
有关C++析构函数的异常(Exceptions in Destructors)
C++模板学习之函数对象之谓词
5月编程语言排行榜:D语言风采不再
一个C++类实现文件全盘搜索
C语言编程宝典之一 读书笔记
C语言嵌入式系统编程修炼(内存操作)
C++内存管理
带头结点的双循环链表
有关VA_LIST的用法
C++标准库简介(转)
一个栈类的实现(链栈)
C 引用与指针的比较

C/C++:小编谈C语言函数那些事(5)


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-10-17   浏览: 58 ::
收藏到网摘: n/a

C程序是由一组或是变量或是函数的外部对象组成的。 函数是一个自我包含的完成一定相关功能的执行代码段。下面小编和大家分享下C语言中的函数。

1. ellipse函数

Ellipse函数的功能是画一椭圆,其用法为:void far ellipse(int x, int y, int stangle, int endangle, int xradius, int yradius); 程序例子如下:

#include <graphics.h>

#include <stdlib.h>

#include <stdio.h>

#include <conio.h>

int main(void)

{

   /* request auto detection */

   int gdriver = DETECT, gmode, errorcode;

   int midx, midy;

   int stangle = 0, endangle = 360;

   int xradius = 100, yradius = 50;

   /* initialize graphics, local variables */

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

   /* read result of initialization */

   errorcode = graphresult();

   if (errorcode != grOk)

   /* an error occurred */

   {

      printf("Graphics error: %s\n",

      grapherrormsg(errorcode));

      printf("Press any key to halt:");

      getch();

      exit(1);

   /* terminate with an error code */

   }

   midx = getmaxx() / 2;

   midy = getmaxy() / 2;

   setcolor(getmaxcolor());

   /* draw ellipse */

   ellipse(midx, midy, stangle, endangle,

    xradius, yradius);

   /* clean up */

   getch();

   closegraph();

   return 0;

}

2. eof函数

Eof函数的功能是检测文件结束,其用法int eof(int *handle); 程序例子如下:

#include <sys\stat.h>

#include <string.h>

#include <stdio.h>

#include <fcntl.h>

#include <io.h>

int main(void)

{

   int handle;

   char msg[] = "This is a test";

   char ch;

   /* create a file */

   handle = open("DUMMY.FIL",

   O_CREAT | O_RDWR,

   S_IREAD | S_IWRITE);

   /* write some data to the file */

   write(handle, msg, strlen(msg));

   /* seek to the beginning of the file */

   lseek(handle, 0L, SEEK_SET);

   /*

      reads chars from the file until hit EOF

   */

   do

   {

      read(handle, &ch, 1);

      printf("%c", ch);

   } while (!eof(handle));

   close(handle);

   return 0;

}

3. exec...函数

Exec函数的功能是装入并运行其它程序的函数,其用法为:

int execl(char *pathname, char *arg0, arg1, ..., argn, NULL);

int execle(char *pathname, char *arg0, arg1, ..., argn, NULL,char *envp[]);

int execlp(char *pathname, char *arg0, arg1, .., NULL);

int execple(char *pathname, char *arg0, arg1, ..., NULL,char *envp[]);

int execv(char *pathname, char *argv[]);

int execve(char *pathname, char *argv[], char *envp[]);

int execvp(char *pathname, char *argv[]);

int execvpe(char *pathname, char *argv[], char *envp[]);

程序例子如下:

/* execv example */

#include <process.h>

#include <stdio.h>

#include <errno.h>

void main(int argc, char *argv[])

{

   int i;

   printf("Command line arguments:\n");

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

      printf("[%2d] : %s\n", i, argv[i]);

   printf("About to exec child with arg1 arg2 ...\n");

   execv("CHILD.EXE", argv);

   perror("exec error");

   exit(1);

}

 

 

4.       exp函数

exp函数功能是指数函数,其用法为:double exp(double x);  程序例子如下:

#include <stdio.h>

#include <math.h>

int main(void)

{

   double result;

   double x = 4.0;

   result = exp(x);

   printf("'e' raised to the power \

   of %lf (e ^ %lf) = %lf\n",

   x, x, result);

   return 0;

}