首页 后端开发 C++ C、必要的图书馆

C、必要的图书馆

Jul 18, 2024 pm 10:47 PM

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 <stdio.h>

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 <stdio.h>

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 <stdio.h>

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 <stdio.h>

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 <stdio.h>

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 <stdio.h>

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 <stdio.h>

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 <stdio.h>

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 <stdio.h>
#include <stdlib.h>

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 < n; i++) {
        arr[i] = i + 1;
    }

    for (int i = 0; i < n; i++) {
        printf("%d ", arr[i]);  // Output: 1 2 3 4 5
    }

    free(arr);  // Frees the allocated memory
    return 0;
}
登录后复制

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 <stdio.h>
#include <stdlib.h>

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 < n; i++) {
        printf("%d ", arr[i]);  // Output: 0 0 0 0 0
    }

    free(arr);  // Frees the allocated memory
    return 0;
}
登录后复制

realloc

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

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 < n; i++) {
        arr[i] = i + 1;
    }

    n = 10;  // Resize the array to hold 10 integers
    arr = (int *)realloc(arr, n * sizeof(int));
    if (arr == NULL) {
        printf("Memory reallocation failed\n");
        return 1;
    }

    for (int i = 5; i < n; i++) {
        arr[i] = i + 1;
    }

    for (int i = 0; i < n; i++) {
        printf("%d ", arr[i]);  // Output: 1 2 3 4 5 6 7 8 9 10
    }

    free(arr);  // Frees the allocated memory
    return 0;
}
登录后复制

free

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

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 <stdio.h>
#include <stdlib.h>

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 <stdio.h>
#include <string.h>

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 <stdio.h>
#include <string.h>

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 <stdio.h>
#include <string.h>

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 <stdio.h>
#include <string.h>

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 <stdio.h>
#include <string.h>

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 <stdio.h>
#include <string.h>

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 <stdio.h>
#include <string.h>

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 <stdio.h>
#include <string.h>

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 <stdio.h>
#include <string.h>

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 <stdio.h>
#include <string.h>

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 <stdio.h>
#include <ctype.h>

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 <stdio.h>
#include <ctype.h>

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 <stdio.h>
#include <ctype.h>

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 <stdio.h>
#include <ctype.h>

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 <stdio.h>
#include <ctype.h>

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 <stdio.h>
#include <ctype.h>

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 <stdio.h>
#include <ctype.h>

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 <stdio.h>
#include <ctype.h>

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 <stdio.h>
#include <math.h>

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 <stdio.h>
#include <math.h>

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 <stdio.h>
#include <math.h>

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 <stdio.h>
#include <math.h>

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 <stdio.h>
#include <math.h>

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 <stdio.h>
#include <math.h>

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 <stdio.h>
#include <math.h>

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 <stdio.h>
#include <math.h>

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 <stdio.h>
#include <math.h>

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 <stdio.h>
#include <math.h>

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;
}
登录后复制

以上是C、必要的图书馆的详细内容。更多信息请关注PHP中文网其他相关文章!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

<🎜>:泡泡胶模拟器无穷大 - 如何获取和使用皇家钥匙
4 周前 By 尊渡假赌尊渡假赌尊渡假赌
北端:融合系统,解释
4 周前 By 尊渡假赌尊渡假赌尊渡假赌
Mandragora:巫婆树的耳语 - 如何解锁抓钩
3 周前 By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

热门话题

Java教程
1673
14
CakePHP 教程
1429
52
Laravel 教程
1333
25
PHP教程
1278
29
C# 教程
1257
24
C#与C:历史,进化和未来前景 C#与C:历史,进化和未来前景 Apr 19, 2025 am 12:07 AM

C#和C 的历史与演变各有特色,未来前景也不同。1.C 由BjarneStroustrup在1983年发明,旨在将面向对象编程引入C语言,其演变历程包括多次标准化,如C 11引入auto关键字和lambda表达式,C 20引入概念和协程,未来将专注于性能和系统级编程。2.C#由微软在2000年发布,结合C 和Java的优点,其演变注重简洁性和生产力,如C#2.0引入泛型,C#5.0引入异步编程,未来将专注于开发者的生产力和云计算。

C#vs. C:学习曲线和开发人员的经验 C#vs. C:学习曲线和开发人员的经验 Apr 18, 2025 am 12:13 AM

C#和C 的学习曲线和开发者体验有显着差异。 1)C#的学习曲线较平缓,适合快速开发和企业级应用。 2)C 的学习曲线较陡峭,适用于高性能和低级控制的场景。

什么是C  中的静态分析? 什么是C 中的静态分析? Apr 28, 2025 pm 09:09 PM

静态分析在C 中的应用主要包括发现内存管理问题、检查代码逻辑错误和提高代码安全性。1)静态分析可以识别内存泄漏、双重释放和未初始化指针等问题。2)它能检测未使用变量、死代码和逻辑矛盾。3)静态分析工具如Coverity能发现缓冲区溢出、整数溢出和不安全API调用,提升代码安全性。

C和XML:探索关系和支持 C和XML:探索关系和支持 Apr 21, 2025 am 12:02 AM

C 通过第三方库(如TinyXML、Pugixml、Xerces-C )与XML交互。1)使用库解析XML文件,将其转换为C 可处理的数据结构。2)生成XML时,将C 数据结构转换为XML格式。3)在实际应用中,XML常用于配置文件和数据交换,提升开发效率。

C  中的chrono库如何使用? C 中的chrono库如何使用? Apr 28, 2025 pm 10:18 PM

使用C 中的chrono库可以让你更加精确地控制时间和时间间隔,让我们来探讨一下这个库的魅力所在吧。C 的chrono库是标准库的一部分,它提供了一种现代化的方式来处理时间和时间间隔。对于那些曾经饱受time.h和ctime折磨的程序员来说,chrono无疑是一个福音。它不仅提高了代码的可读性和可维护性,还提供了更高的精度和灵活性。让我们从基础开始,chrono库主要包括以下几个关键组件:std::chrono::system_clock:表示系统时钟,用于获取当前时间。std::chron

C的未来:改编和创新 C的未来:改编和创新 Apr 27, 2025 am 12:25 AM

C 的未来将专注于并行计算、安全性、模块化和AI/机器学习领域:1)并行计算将通过协程等特性得到增强;2)安全性将通过更严格的类型检查和内存管理机制提升;3)模块化将简化代码组织和编译;4)AI和机器学习将促使C 适应新需求,如数值计算和GPU编程支持。

C:死亡还是简单地发展? C:死亡还是简单地发展? Apr 24, 2025 am 12:13 AM

1)c relevantduetoItsAverity and效率和效果临界。2)theLanguageIsconTinuellyUped,withc 20introducingFeaturesFeaturesLikeTuresLikeSlikeModeLeslikeMeSandIntIneStoImproutiMimproutimprouteverusabilityandperformance.3)

C#vs. C:内存管理和垃圾收集 C#vs. C:内存管理和垃圾收集 Apr 15, 2025 am 12:16 AM

C#使用自动垃圾回收机制,而C 采用手动内存管理。1.C#的垃圾回收器自动管理内存,减少内存泄漏风险,但可能导致性能下降。2.C 提供灵活的内存控制,适合需要精细管理的应用,但需谨慎处理以避免内存泄漏。

See all articles