”工欲善其事,必先利其器。“—孔子《论语.录灵公》
首页 > 编程 > C、必要的图书馆

C、必要的图书馆

发布于2024-11-01
浏览:226

C, Essential Libraries

stdio.h

The stdio.h library in C provides functionalities for input and output operations. Here are some of the important functions provided by stdio.h with examples:

printf

  • Prints formatted output to the standard output (stdout).
  • Syntax: int printf(const char *format, ...)
#include 

int main() {
    printf("Hello, World!\n");  // Output: Hello, World!
    printf("Number: %d\n", 10); // Output: Number: 10
    return 0;
}

scanf

  • Reads formatted input from the standard input (stdin).
  • Syntax: int scanf(const char *format, ...)
#include 

int main() {
    int num;
    printf("Enter a number: ");
    scanf("%d", &num);
    printf("You entered: %d\n", num);
    return 0;
}

gets

  • Reads a line from stdin into the buffer pointed to by s until a newline character or EOF is encountered.
  • Syntax: char *gets(char *s)
#include 

int main() {
    char str[100];
    printf("Enter a string: ");
    gets(str);
    printf("You entered: %s\n", str);
    return 0;
}

fgets

  • Reads a line from the specified stream and stores it into the string pointed to by s. Reading stops after an n-1 characters or a newline.
  • Syntax: char *fgets(char *s, int n, FILE *stream)
#include 

int main() {
    char str[100];
    printf("Enter a string: ");
    fgets(str, 100, stdin);
    printf("You entered: %s\n", str);
    return 0;
}

putchar

  • Writes a character to the standard output (stdout).
  • Syntax: int putchar(int char)
#include 

int main() {
    putchar('A');  // Output: A
    putchar('\n');
    return 0;
}

getchar

  • Reads the next character from the standard input (stdin).
  • Syntax: int getchar(void)
#include 

int main() {
    int c;
    printf("Enter a character: ");
    c = getchar();
    printf("You entered: %c\n", c);
    return 0;
}

puts

  • Writes a string to the standard output (stdout) followed by a newline character.
  • Syntax: int puts(const char *s)
#include 

int main() {
    puts("Hello, World!");  // Output: Hello, World!
    return 0;
}

fputs

  • Writes a string to the specified stream.
  • Syntax: int fputs(const char *s, FILE *stream)
#include 

int main() {
    fputs("Hello, World!\n", stdout);  // Output: Hello, World!
    return 0;
}

stdlib.h

The stdlib.h library in C provides various utility functions for performing general-purpose operations, including memory allocation, process control, conversions, and searching/sorting. Here are some of the important functions provided by stdlib.h with examples:

malloc

  • Allocates a block of memory of a specified size.
  • Syntax: void *malloc(size_t size)
#include 
#include 

int main() {
    int *arr;
    int n = 5;
    arr = (int *)malloc(n * sizeof(int));  // Allocates memory for 5 integers
    if (arr == NULL) {
        printf("Memory allocation failed\n");
        return 1;
    }

    for (int i = 0; i 



calloc

  • Allocates a block of memory for an array of elements, initializing all bytes to zero.
  • Syntax: void *calloc(size_t num, size_t size)
#include 
#include 

int main() {
    int *arr;
    int n = 5;
    arr = (int *)calloc(n, sizeof(int));  // Allocates memory for 5 integers and initializes to zero
    if (arr == NULL) {
        printf("Memory allocation failed\n");
        return 1;
    }

    for (int i = 0; i 



realloc

  • Changes the size of a previously allocated memory block.
  • Syntax: void *realloc(void *ptr, size_t size)
#include 
#include 

int main() {
    int *arr;
    int n = 5;
    arr = (int *)malloc(n * sizeof(int));  // Allocates memory for 5 integers
    if (arr == NULL) {
        printf("Memory allocation failed\n");
        return 1;
    }

    for (int i = 0; i 



free

  • Frees the previously allocated memory.
  • Syntax: void free(void *ptr)
#include 

int main() {
    int *arr = (int *)malloc(5 * sizeof(int));
    // ... use the allocated memory ...
    free(arr);  // Frees the allocated memory
    return 0;
}

exit

  • Terminates the program.
  • Syntax: void exit(int status)
#include 
#include 

int main() {
    printf("Exiting the program\n");
    exit(0);  // Exits the program with a status code of 0
    printf("This line will not be executed\n");
    return 0;
}

string.h

The string.h library in C provides functions for handling strings and performing various operations on them, such as copying, concatenation, comparison, and searching. Here are some of the important functions provided by string.h with examples:

strlen

  • Computes the length of a string.
  • Syntax: size_t strlen(const char *str)
#include 
#include 

int main() {
    char str[] = "Hello, world!";
    printf("Length of the string: %zu\n", strlen(str));  // Output: Length of the string: 13
    return 0;
}

strcpy

  • Copies a string to another.
  • Syntax: char *strcpy(char *dest, const char *src)
#include 
#include 

int main() {
    char src[] = "Hello, world!";
    char dest[50];
    strcpy(dest, src);
    printf("Copied string: %s\n", dest);  // Output: Copied string: Hello, world!
    return 0;
}

strncpy

  • Copies a specified number of characters from a source string to a destination string.
  • Syntax: char *strncpy(char *dest, const char *src, size_t n)
#include 
#include 

int main() {
    char src[] = "Hello, world!";
    char dest[50];
    strncpy(dest, src, 5);
    dest[5] = '\0';  // Null-terminate the destination string
    printf("Copied string: %s\n", dest);  // Output: Copied string: Hello
    return 0;
}

strcat

  • Appends a source string to a destination string.
  • Syntax: char *strcat(char *dest, const char *src)
#include 
#include 

int main() {
    char dest[50] = "Hello";
    char src[] = ", world!";
    strcat(dest, src);
    printf("Concatenated string: %s\n", dest);  // Output: Concatenated string: Hello, world!
    return 0;
}

strncat

  • Appends a specified number of characters from a source string to a destination string.
  • Syntax: char *strncat(char *dest, const char *src, size_t n)
#include 
#include 

int main() {
    char dest[50] = "Hello";
    char src[] = ", world!";
    strncat(dest, src, 7);
    printf("Concatenated string: %s\n", dest);  // Output: Concatenated string: Hello, world
    return 0;
}

strcmp

  • Compares two strings.
  • Syntax: int strcmp(const char *str1, const char *str2)
#include 
#include 

int main() {
    char str1[] = "Hello";
    char str2[] = "Hello";
    char str3[] = "World";
    printf("Comparison result: %d\n", strcmp(str1, str2));  // Output: Comparison result: 0
    printf("Comparison result: %d\n", strcmp(str1, str3));  // Output: Comparison result: -1 (or another negative value)
    return 0;
}

strncmp

  • Compares a specified number of characters of two strings.
  • Syntax: int strncmp(const char *str1, const char *str2, size_t n)
#include 
#include 

int main() {
    char str1[] = "Hello";
    char str2[] = "Helium";
    printf("Comparison result: %d\n", strncmp(str1, str2, 3));  // Output: Comparison result: 0
    printf("Comparison result: %d\n", strncmp(str1, str2, 5));  // Output: Comparison result: -1 (or another negative value)
    return 0;
}

strchr

  • Searches for the first occurrence of a character in a string.
  • Syntax: char *strchr(const char *str, int c)
#include 
#include 

int main() {
    char str[] = "Hello, world!";
    char *ptr = strchr(str, 'w');
    if (ptr != NULL) {
        printf("Character found: %s\n", ptr);  // Output: Character found: world!
    } else {
        printf("Character not found\n");
    }
    return 0;
}

strrchr

  • Searches for the last occurrence of a character in a string.
  • Syntax: char *strrchr(const char *str, int c)
#include 
#include 

int main() {
    char str[] = "Hello, world!";
    char *ptr = strrchr(str, 'o');
    if (ptr != NULL) {
        printf("Last occurrence of character found: %s\n", ptr);  // Output: Last occurrence of character found: orld!
    } else {
        printf("Character not found\n");
    }
    return 0;
}

strstr

  • Searches for the first occurrence of a substring in a string.
  • Syntax: char *strstr(const char *haystack, const char *needle)
#include 
#include 

int main() {
    char str[] = "Hello, world!";
    char *ptr = strstr(str, "world");
    if (ptr != NULL) {
        printf("Substring found: %s\n", ptr);  // Output: Substring found: world!
    } else {
        printf("Substring not found\n");
    }
    return 0;
}

ctype.h

The ctype.h library in C provides functions for character classification and conversion. These functions help to determine the type of a character (such as whether it is a digit, letter, whitespace, etc.) and to convert characters between different cases.

Here are some of the important functions provided by ctype.h with examples:

isalpha

  • Checks if the given character is an alphabetic letter.
  • Syntax: int isalpha(int c)
#include 
#include 

int main() {
    char ch = 'A';
    if (isalpha(ch)) {
        printf("%c is an alphabetic letter\n", ch);  // Output: A is an alphabetic letter
    } else {
        printf("%c is not an alphabetic letter\n", ch);
    }
    return 0;
}

isdigit

  • Checks if the given character is a digit.
  • Syntax: int isdigit(int c)
#include 
#include 

int main() {
    char ch = '9';
    if (isdigit(ch)) {
        printf("%c is a digit\n", ch);  // Output: 9 is a digit
    } else {
        printf("%c is not a digit\n", ch);
    }
    return 0;
}

isalnum

  • Checks if the given character is an alphanumeric character.
  • Syntax: int isalnum(int c)
#include 
#include 

int main() {
    char ch = 'a';
    if (isalnum(ch)) {
        printf("%c is an alphanumeric character\n", ch);  // Output: a is an alphanumeric character
    } else {
        printf("%c is not an alphanumeric character\n", ch);
    }
    return 0;
}

isspace

  • Checks if the given character is a whitespace character.
  • Syntax: int isspace(int c)
#include 
#include 

int main() {
    char ch = ' ';
    if (isspace(ch)) {
        printf("The character is a whitespace\n");  // Output: The character is a whitespace
    } else {
        printf("The character is not a whitespace\n");
    }
    return 0;
}

isupper

  • Checks if the given character is an uppercase letter.
  • Syntax: int isupper(int c)
#include 
#include 

int main() {
    char ch = 'Z';
    if (isupper(ch)) {
        printf("%c is an uppercase letter\n", ch);  // Output: Z is an uppercase letter
    } else {
        printf("%c is not an uppercase letter\n", ch);
    }
    return 0;
}

islower

  • Checks if the given character is a lowercase letter.
  • Syntax: int islower(int c)
#include 
#include 

int main() {
    char ch = 'z';
    if (islower(ch)) {
        printf("%c is a lowercase letter\n", ch);  // Output: z is a lowercase letter
    } else {
        printf("%c is not a lowercase letter\n", ch);
    }
    return 0;
}

toupper

  • Converts a given character to its uppercase equivalent if it is a lowercase letter.
  • Syntax: int toupper(int c)
#include 
#include 

int main() {
    char ch = 'a';
    char upper = toupper(ch);
    printf("Uppercase of %c is %c\n", ch, upper);  // Output: Uppercase of a is A
    return 0;
}

tolower

  • Converts a given character to its lowercase equivalent if it is an uppercase letter.
  • Syntax: int tolower(int c)
#include 
#include 

int main() {
    char ch = 'A';
    char lower = tolower(ch);
    printf("Lowercase of %c is %c\n", ch, lower);  // Output: Lowercase of A is a
    return 0;
}

math.h

The math.h library in C provides functions for mathematical computations. These functions allow operations like trigonometry, logarithms, exponentiation, and more. Here are some important functions provided by math.h with examples:

Trigonometric Functions

sin

  • Computes the sine of an angle (in radians).
  • Syntax: double sin(double x)
#include 
#include 

int main() {
    double angle = 0.5;
    double result = sin(angle);
    printf("sin(0.5) = %.4f\n", result);  // Output: sin(0.5) = 0.4794
    return 0;
}

cos

  • Computes the cosine of an angle (in radians).
  • Syntax: double cos(double x)
#include 
#include 

int main() {
    double angle = 0.5;
    double result = cos(angle);
    printf("cos(0.5) = %.4f\n", result);  // Output: cos(0.5) = 0.8776
    return 0;
}

tan

  • Computes the tangent of an angle (in radians).
  • Syntax: double tan(double x)
#include 
#include 

int main() {
    double angle = 0.5;
    double result = tan(angle);
    printf("tan(0.5) = %.4f\n", result);  // Output: tan(0.5) = 0.5463
    return 0;
}

Exponential and Logarithmic Functions

exp

  • Computes the base-e exponential function of x, e^x.
  • Syntax: double exp(double x)
#include 
#include 

int main() {
    double x = 2.0;
    double result = exp(x);
    printf("exp(2.0) = %.4f\n", result);  // Output: exp(2.0) = 7.3891
    return 0;
}

log

  • Computes the natural logarithm (base-e logarithm) of x.
  • Syntax: double log(double x)
#include 
#include 

int main() {
    double x = 10.0;
    double result = log(x);
    printf("log(10.0) = %.4f\n", result);  // Output: log(10.0) = 2.3026
    return 0;
}

pow

  • Computes x raised to the power of y (x^y).
  • Syntax: double pow(double x, double y)
#include 
#include 

int main() {
    double base = 2.0;
    double exponent = 3.0;
    double result = pow(base, exponent);
    printf("pow(2.0, 3.0) = %.4f\n", result);  // Output: pow(2.0, 3.0) = 8.0000
    return 0;
}

sqrt

  • Computes the square root of x.
  • Syntax: double sqrt(double x)
#include 
#include 

int main() {
    double x = 25.0;
    double result = sqrt(x);
    printf("sqrt(25.0) = %.4f\n", result);  // Output: sqrt(25.0) = 5.0000
    return 0;
}

Rounding and Remainder Functions

ceil

  • Computes the smallest integer value greater than or equal to x.
  • Syntax: double ceil(double x)
#include 
#include 

int main() {
    double x = 3.14;
    double result = ceil(x);
    printf("ceil(3.14) = %.4f\n", result);  // Output: ceil(3.14) = 4.0000
    return 0;
}

floor

  • Computes the largest integer value less than or equal to x.
  • Syntax: double floor(double x)
#include 
#include 

int main() {
    double x = 3.14;
    double result = floor(x);
    printf("floor(3.14) = %.4f\n", result);  // Output: floor(3.14) = 3.0000
    return 0;
}

round

  • Rounds x to the nearest integer value.
  • Syntax: double round(double x)
#include 
#include 

int main() {
    double x = 3.75;
    double result = round(x);
    printf("round(3.75) = %.4f\n", result);  // Output: round(3.75) = 4.0000
    return 0;
}
版本声明 本文转载于:https://dev.to/harshm03/c-essential-libraries-4hda如有侵犯,请联系[email protected]删除
最新教程 更多>
  • HTML格式标签
    HTML格式标签
    HTML 格式化元素 **HTML Formatting is a process of formatting text for better look and feel. HTML provides us ability to format text without us...
    编程 发布于2025-07-13
  • 版本5.6.5之前,使用current_timestamp与时间戳列的current_timestamp与时间戳列有什么限制?
    版本5.6.5之前,使用current_timestamp与时间戳列的current_timestamp与时间戳列有什么限制?
    在时间戳列上使用current_timestamp或MySQL版本中的current_timestamp或在5.6.5 此限制源于遗留实现的关注,这些限制需要对当前的_timestamp功能进行特定的实现。 创建表`foo`( `Productid` int(10)unsigned not n...
    编程 发布于2025-07-13
  • 如何将来自三个MySQL表的数据组合到新表中?
    如何将来自三个MySQL表的数据组合到新表中?
    mysql:从三个表和列的新表创建新表 答案:为了实现这一目标,您可以利用一个3-way Join。 选择p。*,d.content作为年龄 来自人为p的人 加入d.person_id = p.id上的d的详细信息 加入T.Id = d.detail_id的分类法 其中t.taxonomy =...
    编程 发布于2025-07-13
  • 如何将多种用户类型(学生,老师和管理员)重定向到Firebase应用中的各自活动?
    如何将多种用户类型(学生,老师和管理员)重定向到Firebase应用中的各自活动?
    Red: How to Redirect Multiple User Types to Respective ActivitiesUnderstanding the ProblemIn a Firebase-based voting app with three distinct user type...
    编程 发布于2025-07-13
  • Android如何向PHP服务器发送POST数据?
    Android如何向PHP服务器发送POST数据?
    在android apache httpclient(已弃用) httpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost(“ http://www.yoursite.com/script.p...
    编程 发布于2025-07-13
  • 如何从Python中的字符串中删除表情符号:固定常见错误的初学者指南?
    如何从Python中的字符串中删除表情符号:固定常见错误的初学者指南?
    从python import codecs import codecs import codecs 导入 text = codecs.decode('这狗\ u0001f602'.encode('utf-8'),'utf-8') 印刷(文字)#带有...
    编程 发布于2025-07-13
  • PHP阵列键值异常:了解07和08的好奇情况
    PHP阵列键值异常:了解07和08的好奇情况
    PHP数组键值问题,使用07&08 在给定数月的数组中,键值07和08呈现令人困惑的行为时,就会出现一个不寻常的问题。运行print_r($月)返回意外结果:键“ 07”丢失,而键“ 08”分配给了9月的值。此问题源于PHP对领先零的解释。当一个数字带有0(例如07或08)的前缀时,PHP将其...
    编程 发布于2025-07-13
  • 如何使用不同数量列的联合数据库表?
    如何使用不同数量列的联合数据库表?
    合并列数不同的表 当尝试合并列数不同的数据库表时,可能会遇到挑战。一种直接的方法是在列数较少的表中,为缺失的列追加空值。 例如,考虑两个表,表 A 和表 B,其中表 A 的列数多于表 B。为了合并这些表,同时处理表 B 中缺失的列,请按照以下步骤操作: 确定表 B 中缺失的列,并将它们添加到表的末...
    编程 发布于2025-07-13
  • Go web应用何时关闭数据库连接?
    Go web应用何时关闭数据库连接?
    在GO Web Applications中管理数据库连接很少,考虑以下简化的web应用程序代码:出现的问题:何时应在DB连接上调用Close()方法?,该特定方案将自动关闭程序时,该程序将在EXITS EXITS EXITS出现时自动关闭。但是,其他考虑因素可能保证手动处理。选项1:隐式关闭终止数...
    编程 发布于2025-07-13
  • 在UTF8 MySQL表中正确将Latin1字符转换为UTF8的方法
    在UTF8 MySQL表中正确将Latin1字符转换为UTF8的方法
    在UTF8表中将latin1字符转换为utf8 ,您遇到了一个问题,其中含义的字符(例如,“jáuòiñe”)在utf8 table tabled tablesset中被extect(例如,“致电。为了解决此问题,您正在尝试使用“ mb_convert_encoding”和“ iconv”转换受...
    编程 发布于2025-07-13
  • 编译器报错“usr/bin/ld: cannot find -l”解决方法
    编译器报错“usr/bin/ld: cannot find -l”解决方法
    错误:“ usr/bin/ld:找不到-l “ 此错误表明链接器在链接您的可执行文件时无法找到指定的库。为了解决此问题,我们将深入研究如何指定库路径并将链接引导到正确位置的详细信息。添加库搜索路径的一个可能的原因是,此错误是您的makefile中缺少库搜索路径。要解决它,您可以在链接器命令中添加...
    编程 发布于2025-07-13
  • 图片在Chrome中为何仍有边框?`border: none;`无效解决方案
    图片在Chrome中为何仍有边框?`border: none;`无效解决方案
    在chrome 在使用Chrome and IE9中的图像时遇到的一个频繁的问题是围绕图像的持续薄薄边框,尽管指定了图像,尽管指定了;和“边境:无;”在CSS中。要解决此问题,请考虑以下方法: Chrome具有忽略“ border:none; none;”的已知错误,风格。要解决此问题,请使用以下...
    编程 发布于2025-07-13
  • 如何使用Python理解有效地创建字典?
    如何使用Python理解有效地创建字典?
    在python中,词典综合提供了一种生成新词典的简洁方法。尽管它们与列表综合相似,但存在一些显着差异。与问题所暗示的不同,您无法为钥匙创建字典理解。您必须明确指定键和值。 For example:d = {n: n**2 for n in range(5)}This creates a dicti...
    编程 发布于2025-07-13
  • 为什么不````''{margin:0; }`始终删除CSS中的最高边距?
    为什么不````''{margin:0; }`始终删除CSS中的最高边距?
    在CSS 问题:不正确的代码: 全球范围将所有余量重置为零,如提供的代码所建议的,可能会导致意外的副作用。解决特定的保证金问题是更建议的。 例如,在提供的示例中,将以下代码添加到CSS中,将解决余量问题: body H1 { 保证金顶:-40px; } 此方法更精确,避免了由全局保证金重置引...
    编程 发布于2025-07-13
  • 为什么PHP的DateTime :: Modify('+1个月')会产生意外的结果?
    为什么PHP的DateTime :: Modify('+1个月')会产生意外的结果?
    使用php dateTime修改月份:发现预期的行为在使用PHP的DateTime类时,添加或减去几个月可能并不总是会产生预期的结果。正如文档所警告的那样,“当心”这些操作的“不像看起来那样直观。 ; $ date->修改('1个月'); //前进1个月 echo $ date->...
    编程 发布于2025-07-13

免责声明: 提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发到邮箱:[email protected] 我们会第一时间内为您处理。

Copyright© 2022 湘ICP备2022001581号-3