📜  C 库函数

📅  最后修改于: 2022-05-13 01:54:47.686000             🧑  作者: Mango

C 库函数

C 中的标准函数库是一个巨大的子库库,每个子库都包含多个函数的代码。为了利用这些库,通过使用头文件链接更广泛的库中的每个库。这些函数的定义存在于它们各自的头文件中。为了使用这些函数,我们必须在程序中包含头文件。下面是一些带有描述的头文件:

S No.Header FilesDescription
1It checks the value of an expression that we expect to be true under normal circumstances.
If the expression is a nonzero value, the assert macro does nothing.
2 A set of functions for manipulating complex numbers.
3Defines macro constants specifying the implementation-specific properties of the
floating-point library.
4These limits specify that a variable cannot store any value beyond these limits, for example-
An unsigned character can store up to a maximum value of 255.
5The math.h header defines various mathematical functions and one macro. All the Functions 
in this library take double as an argument and return double as the result.
6The stdio.h header defines three variable types, several macros, and various
function for performing input and output.
7Defines date and time handling functions.
8Strings are defined as an array of characters. The difference between a character array 
and a string is that a string is terminated with a special character ‘\0’.

实现:让我们用一个 C 程序来讨论基本库的实现:

1. stdio.h 该库用于使用printf()函数,程序中应包含头文件。下面是实现上述方法的 C 程序:

C
// C program to implement
// the above approach
#include 
 
// Driver code
int main()
{
    printf("GEEKS FOR GEEKS");
    return 0;
}


C
// C program to implement
// the above approach
#include 
#include 
 
// Driver code
int main()
{
    double number, squareRoot;
 
    number = 12.5;
 
    // Computing the square root
    squareRoot = sqrt(number);
 
    printf("Square root of %.2lf =  %.2lf",
           number, squareRoot);
    return 0;
}


C
// C program to implement
// the above approach
#include 
#include 
 
// Driver code
int main()
{
    double base, power, result;
    base = 10.0;
    power = 2.0;
 
    // Calculate the result
    result = pow(base, power);
    printf("%.1lf^%.1lf = %.2lf",
           base, power, result);
    return 0;
}


C
// C program to implement
// the above approach
#include 
#include 
 
// Driver code
int main()
{
    double x;
    double result;
    x = 2.3;
    result = sin(x);
    printf("sin(%.2lf) = %.2lf\n",
           x, result);
    x = -2.3;
    result = sin(x);
    printf("sin(%.2lf) = %.2lf\n",
           x, result);
    x = 0;
    result = sin(x);
    printf("sin(%.2lf) = %.2lf\n",
           x, result);
    return 0;
}


C
// C program to implement
// the above approach
#include 
#include 
#define PI 3.141592654
 
// Driver code
int main()
{
    double arg = 30, result;
 
    // Converting to radian
    arg = (arg * PI) / 180;
 
    result = cos(arg);
    printf("cos of %.2lf radian = %.2lf",
           arg, result);
    return 0;
}


C
// C program to implement
// the above approach
#include 
#include 
 
// Driver code
int main()
{
    double x;
    double result;
    x = 2.3;
    result = tan(x);
    printf("tan(%.2lf) = %.2lf\n",
           x, result);
    x = -2.3;
    result = tan(x);
    printf("tan(%.2lf) = %.2lf\n",
           x, result);
    return 0;
}


C
// C program to implement
// the above approach
#include 
#include 
 
// Driver code
int main()
{
    double num = 5.6, result;
    result = log(num);
    printf("log(%.1f) = %.2f",
           num, result);
    return 0;
}


C
// C program to implement
// the above approach
#include 
#include 
 
// Driver code
int main()
{
    printf("Maximum value of float = %.10e\n",
           FLT_MAX);
    printf("Minimum value of float = %.10e\n",
           FLT_MIN);
}


C
// C program to implement
// the above approach
#include 
#include 
 
// Driver code
int main()
{
    printf("Number of bits in a byte %d\n",
           CHAR_BIT);
    printf("Minimum value of SIGNED CHAR = %d\n",
           SCHAR_MIN);
    printf("Maximum value of SIGNED CHAR = %d\n",
           SCHAR_MAX);
    printf("Maximum value of UNSIGNED CHAR = %d\n",
           UCHAR_MAX);
    printf("Minimum value of SHORT INT = %d\n",
           SHRT_MIN);
    printf("Maximum value of SHORT INT = %d\n",
           SHRT_MAX);
    printf("Minimum value of INT = %d\n",
           INT_MIN);
    printf("Maximum value of INT = %d\n",
           INT_MAX);
    printf("Minimum value of CHAR = %d\n",
           CHAR_MIN);
    printf("Maximum value of CHAR = %d\n",
           CHAR_MAX);
    printf("Minimum value of LONG = %ld\n",
           LONG_MIN);
    printf("Maximum value of LONG = %ld\n",
           LONG_MAX);
    return (0);
}


C
// C program to implement
// the above approach
#include 
#include 
#define SIZE 256
 
// Driver code
int main(void)
{
    char buffer[SIZE];
    time_t curtime;
    struct tm* loctime;
 
    // Get the current time.
    curtime = time(NULL);
 
    // Convert it to local time
    // representation.
    loctime = localtime(&curtime);
 
    // Print out the date and time
    // in the standard format.
    fputs(asctime(loctime), stdout);
 
    // Print it out
    strftime(buffer, SIZE,
             "Today is %A, %B %d.\n",
             loctime);
 
    fputs(buffer, stdout);
    strftime(buffer, SIZE,
             "The time is %I:%M %p.\n",
             loctime);
    fputs(buffer, stdout);
    return 0;
}


C
// C program to implement
// the above approach
#include 
#include 
 
// Driver code
int main()
{
    char str1[100] = "Geeks ",
         str2[100] = " For Geeks";
 
    // Concatenates str1 and str2
    strcat(str1, str2);
 
    // Resultant string is stored
    // in str1
    puts(str1);
 
    return 0;
}


C
// C program to implement
// the above approach
#include 
#include 
 
// Driver code
int main()
{
    char str1[] = "Geeks",
         str2[] = "gEeks",
         str3[] = "Geeks";
    int result;
 
    // Comparing strings str1
    // and str2
    result = strcmp(str1, str2);
 
    printf("strcmp(str1, str2) = %d\n",
           result);
 
    // Comparing strings str1 and str3
    result = strcmp(str1, str3);
 
    printf("strcmp(str1, str3) = %d\n",
           result);
    return 0;
}


C
// C program to implement
// the above approach
#include 
#include 
 
// Driver code
int main()
{
    char str1[20] = "Geeks For Geeks";
    char str2[20];
 
    // Copying str1 to str2
    strcpy(str2, str1);
 
    puts(str2);
    return 0;
}


C
// C program to implement
// the above approach
#include 
#include 
 
// Driver code
int main()
{
    char a[20] = "Program";
    char b[20] = { "Geeks for Geeks" };
    printf("Length of string a = %zu \n",
           strlen(a));
    printf("Length of string b = %zu \n",
           strlen(b));
    return 0;
}


C
// C program to implement
// the above approach
#include 
#include 
 
// Driver code
int main(void)
{
    double real = 1.3,
           imag = 4.9;
    double complex z = CMPLX(real,
                             imag);
    double complex conj_f = conjf(z);
    printf("z = %.1f% + .1fi\n",
           creal(conj_f),
           cimag(conj_f));
}


C++
// C program to implement
// the above approach
#include 
#include 
 
// Driver code
int main()
{
    int x = 7;
 
    // Some big code in between
    // and let's say x is accidentally
    // changed to 9
    x = 9;
 
    // Programmer assumes x to be 7
    // in rest of the code
    assert(x == 7);
 
    // Rest of the code
 
    return 0;
}


输出
GEEKS FOR GEEKS

注意:如果使用 printf()函数而不包含头文件 ,则会显示错误。



2. math.h 要执行任何与数学相关的操作,必须包含 math.h 头文件。

示例 1: sqrt()

句法-

double sqrt(double x)

下面是计算任意数平方根的 C 程序:

C

// C program to implement
// the above approach
#include 
#include 
 
// Driver code
int main()
{
    double number, squareRoot;
 
    number = 12.5;
 
    // Computing the square root
    squareRoot = sqrt(number);
 
    printf("Square root of %.2lf =  %.2lf",
           number, squareRoot);
    return 0;
}
输出
Square root of 12.50 =  3.54

示例 2- pow()

句法:

double pow(double x, double y)

下面是计算任意数幂的 C 程序:



C

// C program to implement
// the above approach
#include 
#include 
 
// Driver code
int main()
{
    double base, power, result;
    base = 10.0;
    power = 2.0;
 
    // Calculate the result
    result = pow(base, power);
    printf("%.1lf^%.1lf = %.2lf",
           base, power, result);
    return 0;
}
输出
10.0^2.0 = 100.00

示例 3- sin()

句法:

double sin(double x)

下面是计算参数正弦的 C 程序:

C

// C program to implement
// the above approach
#include 
#include 
 
// Driver code
int main()
{
    double x;
    double result;
    x = 2.3;
    result = sin(x);
    printf("sin(%.2lf) = %.2lf\n",
           x, result);
    x = -2.3;
    result = sin(x);
    printf("sin(%.2lf) = %.2lf\n",
           x, result);
    x = 0;
    result = sin(x);
    printf("sin(%.2lf) = %.2lf\n",
           x, result);
    return 0;
}
输出
sin(2.30) = 0.75
sin(-2.30) = -0.75
sin(0.00) = 0.00

示例 4- cos()

句法:

double cos(double x);

下面是计算参数余弦的 C 程序:

C

// C program to implement
// the above approach
#include 
#include 
#define PI 3.141592654
 
// Driver code
int main()
{
    double arg = 30, result;
 
    // Converting to radian
    arg = (arg * PI) / 180;
 
    result = cos(arg);
    printf("cos of %.2lf radian = %.2lf",
           arg, result);
    return 0;
}
输出
cos of 0.52 radian = 0.87

示例 5- tan()



句法:

double tan(double x);

下面是计算参数正切的 C 程序:

C

// C program to implement
// the above approach
#include 
#include 
 
// Driver code
int main()
{
    double x;
    double result;
    x = 2.3;
    result = tan(x);
    printf("tan(%.2lf) = %.2lf\n",
           x, result);
    x = -2.3;
    result = tan(x);
    printf("tan(%.2lf) = %.2lf\n",
           x, result);
    return 0;
}
输出
tan(2.30) = -1.12
tan(-2.30) = 1.12

示例 6- log()

句法-

double log( double arg );

下面是计算参数的自然对数的 C 程序 -

C

// C program to implement
// the above approach
#include 
#include 
 
// Driver code
int main()
{
    double num = 5.6, result;
    result = log(num);
    printf("log(%.1f) = %.2f",
           num, result);
    return 0;
}
输出
log(5.6) = 1.72

3. float.h C 标准库的 float.h 头文件包含一组与浮点值相关的各种平台相关常量。下面是实现上述方法的 C 程序 -

C

// C program to implement
// the above approach
#include 
#include 
 
// Driver code
int main()
{
    printf("Maximum value of float = %.10e\n",
           FLT_MAX);
    printf("Minimum value of float = %.10e\n",
           FLT_MIN);
}
输出
Maximum value of float = 3.4028234664e+38
Minimum value of float = 1.1754943508e-38

4. limits.h中该头limits.h中确定的各种变量类型的各种属性。此标头中定义的宏限制了各种变量类型(如 char、int 和 long)的值。下面是实现上述方法的 C 程序 -



C

// C program to implement
// the above approach
#include 
#include 
 
// Driver code
int main()
{
    printf("Number of bits in a byte %d\n",
           CHAR_BIT);
    printf("Minimum value of SIGNED CHAR = %d\n",
           SCHAR_MIN);
    printf("Maximum value of SIGNED CHAR = %d\n",
           SCHAR_MAX);
    printf("Maximum value of UNSIGNED CHAR = %d\n",
           UCHAR_MAX);
    printf("Minimum value of SHORT INT = %d\n",
           SHRT_MIN);
    printf("Maximum value of SHORT INT = %d\n",
           SHRT_MAX);
    printf("Minimum value of INT = %d\n",
           INT_MIN);
    printf("Maximum value of INT = %d\n",
           INT_MAX);
    printf("Minimum value of CHAR = %d\n",
           CHAR_MIN);
    printf("Maximum value of CHAR = %d\n",
           CHAR_MAX);
    printf("Minimum value of LONG = %ld\n",
           LONG_MIN);
    printf("Maximum value of LONG = %ld\n",
           LONG_MAX);
    return (0);
}
输出
Number of bits in a byte 8
Minimum value of SIGNED CHAR = -128
Maximum value of SIGNED CHAR = 127
Maximum value of UNSIGNED CHAR = 255
Minimum value of SHORT INT = -32768
Maximum value of SHORT INT = 32767
Minimum value of INT = -2147483648
Maximum value of INT = 2147483647
Minimum value of CHAR = -128
Maximum value of CHAR = 127
Minimum value of LONG = -9223372036854775808
Maximum value of LONG = 9223372036854775807

5. time.h 这个头文件定义了日期和时间函数。下面是实现 time() 和 localtime() 函数的 C 程序-

C

// C program to implement
// the above approach
#include 
#include 
#define SIZE 256
 
// Driver code
int main(void)
{
    char buffer[SIZE];
    time_t curtime;
    struct tm* loctime;
 
    // Get the current time.
    curtime = time(NULL);
 
    // Convert it to local time
    // representation.
    loctime = localtime(&curtime);
 
    // Print out the date and time
    // in the standard format.
    fputs(asctime(loctime), stdout);
 
    // Print it out
    strftime(buffer, SIZE,
             "Today is %A, %B %d.\n",
             loctime);
 
    fputs(buffer, stdout);
    strftime(buffer, SIZE,
             "The time is %I:%M %p.\n",
             loctime);
    fputs(buffer, stdout);
    return 0;
}
输出
Sun May 30 17:27:47 2021
Today is Sunday, May 30.
The time is 05:27 PM.

6. 字符串.h 为了使用字符串函数,需要在程序中包含字符串.h 头文件。

示例 1: strcat() 在 C 编程中, strcat() 函数用于连接(连接)两个字符串。此函数连接目标字符串和源字符串,并将结果存储在目标字符串。

句法-

char *strcat(char *destination, const char *source)

下面是实现 strcat() 的 C 程序:

C

// C program to implement
// the above approach
#include 
#include 
 
// Driver code
int main()
{
    char str1[100] = "Geeks ",
         str2[100] = " For Geeks";
 
    // Concatenates str1 and str2
    strcat(str1, str2);
 
    // Resultant string is stored
    // in str1
    puts(str1);
 
    return 0;
}
输出
Geeks  For Geeks

示例 2- strcmp() 它比较两个字符串。如果返回值为 0,则字符串相等;如果返回值为非零,则字符串不相等。

句法:



int strcmp (const char* str1, const char* str2);

下面是实现 strcmp() 的 C 程序:

C

// C program to implement
// the above approach
#include 
#include 
 
// Driver code
int main()
{
    char str1[] = "Geeks",
         str2[] = "gEeks",
         str3[] = "Geeks";
    int result;
 
    // Comparing strings str1
    // and str2
    result = strcmp(str1, str2);
 
    printf("strcmp(str1, str2) = %d\n",
           result);
 
    // Comparing strings str1 and str3
    result = strcmp(str1, str3);
 
    printf("strcmp(str1, str3) = %d\n",
           result);
    return 0;
}
输出
strcmp(str1, str2) = -32
strcmp(str1, str3) = 0

示例 3 – strcpy() strcpy()函数将源指向的字符串复制到目标。

句法:

char* strcpy(char* destination, const char* source);

下面是实现 strcpy() 的 C 程序:

C

// C program to implement
// the above approach
#include 
#include 
 
// Driver code
int main()
{
    char str1[20] = "Geeks For Geeks";
    char str2[20];
 
    // Copying str1 to str2
    strcpy(str2, str1);
 
    puts(str2);
    return 0;
}
输出
Geeks For Geeks

示例 4 – strlen() 此函数计算给定字符串的长度。

句法:

int strlen(char a[]);

下面是实现 strlen() 的 C 程序:

C

// C program to implement
// the above approach
#include 
#include 
 
// Driver code
int main()
{
    char a[20] = "Program";
    char b[20] = { "Geeks for Geeks" };
    printf("Length of string a = %zu \n",
           strlen(a));
    printf("Length of string b = %zu \n",
           strlen(b));
    return 0;
}
输出
Length of string a = 7 
Length of string b = 15 

7. complex.h 该头文件中的函数用于对复数进行各种操作。复数是具有实部和虚部的复数。

下面是实现复数共轭的 C 程序 -

C

// C program to implement
// the above approach
#include 
#include 
 
// Driver code
int main(void)
{
    double real = 1.3,
           imag = 4.9;
    double complex z = CMPLX(real,
                             imag);
    double complex conj_f = conjf(z);
    printf("z = %.1f% + .1fi\n",
           creal(conj_f),
           cimag(conj_f));
}

输出:

z = 1.3 - 4.9i

8. assert.h 断言是用于测试程序员所做假设的语句。例如,我们可以使用断言来检查 malloc() 返回的指针是否为 NULL。

句法-

void assert(int expression); 

C++

// C program to implement
// the above approach
#include 
#include 
 
// Driver code
int main()
{
    int x = 7;
 
    // Some big code in between
    // and let's say x is accidentally
    // changed to 9
    x = 9;
 
    // Programmer assumes x to be 7
    // in rest of the code
    assert(x == 7);
 
    // Rest of the code
 
    return 0;
}

输出

Assertion failed: x==7, file test.cpp, line 13 
This application has requested the Runtime to terminate it in an unusual 
way. Please contact the application's support team for more information.
想要从精选的视频和练习题中学习,请查看C 基础到高级C 基础课程