C語言中有哪些預定義函式?


函式大致分為兩種型別,如下所示:

  • 預定義函式
  • 使用者定義函式

預定義(或)庫函式

  • 這些函式已在系統庫中定義。

  • 程式設計師將重用系統庫中已有的程式碼來編寫無錯誤的程式碼。

  • 但要使用庫函式,使用者必須瞭解函式的語法。

例如:

  • 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 次方……

示例

以下是一個使用預定義函式的 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

更新於: 2021年3月15日

9K+ 瀏覽量

開啟你的職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.