📜  C / C ++中用户定义函数与库函数之间的区别

📅  最后修改于: 2021-05-31 23:53:59             🧑  作者: 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++基础课程以及有关语言和STL的C++ STL课程。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”