函数大致分为两类,如下: -
- 预定义函数
- 用户定义函数
预定义(或)库函数
这些函数已在系统库中定义。
- < p>程序员将重用系统库中已有的代码来编写无错误的代码。
但是要使用库函数,用户必须了解函数的语法。
示例 -
- sqrt() 函数在 math.h 库中可用它的用法是 -
y= sqrt (x) x number must be positive eg: y = sqrt (25) then ‘y’ = 5
- printf ( ) 存在于 stdio.h 库中。
- clrscr ( ) 存在于 conio.h 库中。
示例
下面给出的是预定义函数 sqrt、printf、conio 的 C 程序 -
#include<stdio.h> #include<conio.h> #include<math.h> main ( ){ int x,y; clrscr ( ); printf ("enter a positive number"); scanf (" %d", &x) y = sqrt(x); printf("squareroot = %d", y); getch(); }
输出
您将看到以下输出 -
Enter a positive number 25 Squareroot = 5
考虑一些更预定义的函数 -
- Cbrt(x):x 的立方根
- Log(x):x 底的自然对数e
- Ceils(x):将x四舍五入为不小于x的较小整数
- Pow(x,y):x的y次方……...
- Pow(x,y):x的y次幂…… ul>
示例
以下是使用预定义函数的 C 程序 -
#include<stdio.h> #include<math.h> main ( ){ int x,y,z,n,k,p,r,q; printf ("enter x and n values:"); scanf (" %d%d", &x,&y) y=cbrt(x); z=exp(x); k=log(x); p=ceil(x); q=pow(x,r); printf("cuberoot = %d", y); printf("exponent value = %d",z); printf("logarithmic value = %d", k); printf("ceil value = %d", p); printf("power = %d", q); getch(); }
输出
输出如下 -
enter x and n values:9 2 cuberoot = 2 exponent value = 8103 logarithmic value = 2 ceil value = 9 power = 81