程序员之家 >> 文章 >> 开发语言 >> C/C++
C/C++——小编谈C语言函数那些事(53)
作者:娜   来源:程序员之家   发布者:admin
时间:2009-11-12 09:43:51   点击:2694

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

 

1.  div函数

div函数的功能是将两个整数相除, 返回商和余数,其用法为:div_t (int number, int denom);程序实例如下:

#include <stdlib.h>

#include <stdio.h>

div_t x;

int main(void)

{

   x = div(10,3);

   printf("10 div 3 = %d remainder %d\n", x.quot, x.rem);

   return 0;

}

 2. dup函数

dup函数的功能是复制一个文件句柄,其用法为int dup(int handle);程序实例代码如下:

#include <string.h>

#include <stdio.h>

#include <conio.h>

#include <io.h>

void flush(FILE *stream);

int main(void)

{

   FILE *fp;

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

   fp = fopen("DUMMY.FIL", "w");

   fwrite(msg, strlen(msg), 1, fp);

   clrscr();

   printf("Press any key to flush \

   DUMMY.FIL:");

   getch();

   flush(fp);

   printf("\nFile was flushed, Press any \

   key to quit:");

   getch();

   return 0;

}

void flush(FILE *stream)

{

   int duphandle;

   fflush(stream);

   duphandle = dup(fileno(stream));

      DOS buffer */

   close(duphandle);

}

 

3.  atan2函数

atan2函数的功能是计算Y/X的反正切值, 其用法为double atan2(double y, double x);程序实例代码如下:

#include <stdio.h>

#include <math.h>

int main(void)

{

   double result;

   double x = 90.0, y = 45.0;

   result = atan2(y, x);

   printf("The arc tangent ratio of %lf is %lf\n", (y / x), result);

   return 0;

}

 

4. gety函数

gety函数的功能是返回当前图形位置的y坐标,其用法为:int far gety(void);程序实例代码如下:

#include <graphics.h>

#include <stdlib.h>

#include <stdio.h>

#include <conio.h>

int main(void)

{

   int gdriver = DETECT, gmode, errorcode;

   char msg[80];

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

   errorcode = graphresult();

   if (errorcode != grOk) 

   {

      printf("Graphics error: %s\n", grapherrormsg(errorcode));

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

      getch();

      exit(1);

   }

   moveto(getmaxx() / 2, getmaxy() / 2);

   sprintf(msg, "<-(%d, %d) is the here.", getx(), gety());

   outtext(msg);

   getch();

   closegraph();

   return 0;

}

转载请注明出处: 程序员之家   http://www.sunxin.org

 

最新文章
点击排行