📜  C/C++中用户定义函数和库函数的区别

📅  最后修改于: 2021-09-14 01:59:47             🧑  作者: Mango

库函数:
这些函数是内置函数,即它们是在 C 库中预定义的。它们用于执行最常见的操作,如计算、更新等。一些库函数是printf、scanf、sqrt 等。要在程序中使用此函数,用户必须使用与程序中相应函数相关联的关联头文件

例如:
如果用户必须使用输入流打印数据或扫描数据,那么我们必须在 C 程序中使用 printf() 和 scanf() 函数,在 C++ 程序中使用 cin 和 cout 函数。要使用这些函数,用户必须在 C 程序中包含#include预处理器指令,在 C++ 程序中包含#include预处理器指令。

C
// C program to illustrate inbuilt function
#include 
  
// Driver Code
int main()
{
  
    // Print Statement
    printf("GeeksforGeeks!");
  
    return 0;
}


C++
// C++ program to illustrate inbuilt function
#include 
using namespace std;
  
// Driver Code
int main()
{
  
    // Print Statement
    cout << "GeeksforGeeks!";
  
    return 0;
}


C
// C program to illustrate user-defined function
#include 
  
// Function Call to find the sum of a and b
void findSum(int a, int b)
{
  
    // Print the sum
    printf("Sum is: %d", a + b);
}
  
// Driver Code
int main()
{
    // Given two numbers
    int a = 3, b = 5;
  
    // Function Call
    findSum(a, b);
    return 0;
}


C++
// C++ program to illustrate inbuilt function
#include 
using namespace std;
  
// Function Call to find the sum of a and b
void findSum(int a, int b)
{
  
    // Print the sum
    cout << "Sum is: " << a + b;
}
  
// Driver Code
int main()
{
    // Given two numbers
    int a = 3, b = 5;
  
    // Function Call
    findSum(a, b);
    return 0;
}


输出:
GeeksforGeeks!

用户定义函数:
这些函数是由用户在编写任何程序时设计的,因为对于每项任务,我们都没有预定义定义的函数库。为了根据用户要求执行,用户必须自己开发一些功能,这些功能称为用户自定义功能。对于此类函数,用户必须定义函数正确定义

例如:如果我们要执行两个数字的加法,那么下面是使用用户定义函数说明两个数字相加的程序:

C

// C program to illustrate user-defined function
#include 
  
// Function Call to find the sum of a and b
void findSum(int a, int b)
{
  
    // Print the sum
    printf("Sum is: %d", a + b);
}
  
// Driver Code
int main()
{
    // Given two numbers
    int a = 3, b = 5;
  
    // Function Call
    findSum(a, b);
    return 0;
}

C++

// C++ program to illustrate inbuilt function
#include 
using namespace std;
  
// Function Call to find the sum of a and b
void findSum(int a, int b)
{
  
    // Print the sum
    cout << "Sum is: " << a + b;
}
  
// Driver Code
int main()
{
    // Given two numbers
    int a = 3, b = 5;
  
    // Function Call
    findSum(a, b);
    return 0;
}
输出:
Sum is: 8

表格表示来说明库和用户定义函数之间的区别:

User-defined Functions Library Functions
These functions are not predefined in the Compiler. These functions are predefined in the compiler of C language.
These function are created by user as per their own requirement. These functions are not created by user as their own.
User-defined functions are not stored in library file. Library Functions are stored in special library file.
There is no such kind of requirement to add the particular library. In this if the user wants to use a particular library function then the user have to add the particular library of that function in header file of the program.
Execution of the program begins from the user-define function. Execution of the program does not begin from the library function.
Example: sum(), fact(),…etc. Example: printf(), scanf(), sqrt(),…etc.
想要从精选的视频和练习题中学习,请查看C++ 基础课程,从基础到高级 C++ 和C++ STL 课程,了解语言和 STL。要完成从学习语言到 DS Algo 等的准备工作,请参阅完整的面试准备课程