C, 필수 라이브러리
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 중국어 웹사이트의 기타 관련 기사를 참조하세요!

핫 AI 도구

Undresser.AI Undress
사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover
사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool
무료로 이미지를 벗다

Clothoff.io
AI 옷 제거제

Video Face Swap
완전히 무료인 AI 얼굴 교환 도구를 사용하여 모든 비디오의 얼굴을 쉽게 바꾸세요!

인기 기사

뜨거운 도구

메모장++7.3.1
사용하기 쉬운 무료 코드 편집기

SublimeText3 중국어 버전
중국어 버전, 사용하기 매우 쉽습니다.

스튜디오 13.0.1 보내기
강력한 PHP 통합 개발 환경

드림위버 CS6
시각적 웹 개발 도구

SublimeText3 Mac 버전
신 수준의 코드 편집 소프트웨어(SublimeText3)

C#과 C의 역사와 진화는 독특하며 미래의 전망도 다릅니다. 1.C는 1983 년 Bjarnestroustrup에 의해 발명되어 객체 지향 프로그래밍을 C 언어에 소개했습니다. Evolution 프로세스에는 자동 키워드 소개 및 Lambda Expressions 소개 C 11, C 20 도입 개념 및 코 루틴과 같은 여러 표준화가 포함되며 향후 성능 및 시스템 수준 프로그래밍에 중점을 둘 것입니다. 2.C#은 2000 년 Microsoft에 의해 출시되었으며 C와 Java의 장점을 결합하여 진화는 단순성과 생산성에 중점을 둡니다. 예를 들어, C#2.0은 제네릭과 C#5.0 도입 된 비동기 프로그래밍을 소개했으며, 이는 향후 개발자의 생산성 및 클라우드 컴퓨팅에 중점을 둘 것입니다.

C# 및 C 및 개발자 경험의 학습 곡선에는 상당한 차이가 있습니다. 1) C#의 학습 곡선은 비교적 평평하며 빠른 개발 및 기업 수준의 응용 프로그램에 적합합니다. 2) C의 학습 곡선은 가파르고 고성능 및 저수준 제어 시나리오에 적합합니다.

C에서 정적 분석의 적용에는 주로 메모리 관리 문제 발견, 코드 로직 오류 확인 및 코드 보안 개선이 포함됩니다. 1) 정적 분석은 메모리 누출, 이중 릴리스 및 초기화되지 않은 포인터와 같은 문제를 식별 할 수 있습니다. 2) 사용하지 않은 변수, 데드 코드 및 논리적 모순을 감지 할 수 있습니다. 3) Coverity와 같은 정적 분석 도구는 버퍼 오버플로, 정수 오버플로 및 안전하지 않은 API 호출을 감지하여 코드 보안을 개선 할 수 있습니다.

C는 XML과 타사 라이브러리 (예 : TinyXML, Pugixml, Xerces-C)와 상호 작용합니다. 1) 라이브러리를 사용하여 XML 파일을 구문 분석하고 C- 처리 가능한 데이터 구조로 변환하십시오. 2) XML을 생성 할 때 C 데이터 구조를 XML 형식으로 변환하십시오. 3) 실제 애플리케이션에서 XML은 종종 구성 파일 및 데이터 교환에 사용되어 개발 효율성을 향상시킵니다.

C에서 Chrono 라이브러리를 사용하면 시간과 시간 간격을보다 정확하게 제어 할 수 있습니다. 이 도서관의 매력을 탐구합시다. C의 크로노 라이브러리는 표준 라이브러리의 일부로 시간과 시간 간격을 다루는 현대적인 방법을 제공합니다. 시간과 C 시간으로 고통받는 프로그래머에게는 Chrono가 의심 할 여지없이 혜택입니다. 코드의 가독성과 유지 가능성을 향상시킬뿐만 아니라 더 높은 정확도와 유연성을 제공합니다. 기본부터 시작합시다. Chrono 라이브러리에는 주로 다음 주요 구성 요소가 포함됩니다. std :: Chrono :: System_Clock : 현재 시간을 얻는 데 사용되는 시스템 클럭을 나타냅니다. STD :: 크론

C의 미래는 병렬 컴퓨팅, 보안, 모듈화 및 AI/기계 학습에 중점을 둘 것입니다. 1) 병렬 컴퓨팅은 코 루틴과 같은 기능을 통해 향상 될 것입니다. 2)보다 엄격한 유형 검사 및 메모리 관리 메커니즘을 통해 보안이 향상 될 것입니다. 3) 변조는 코드 구성 및 편집을 단순화합니다. 4) AI 및 머신 러닝은 C가 수치 컴퓨팅 및 GPU 프로그래밍 지원과 같은 새로운 요구에 적응하도록 촉구합니다.

c is nontdying; it'sevolving.1) c COMINGDUETOITSTIONTIVENICICICICINICE INPERFORMICALEPPLICATION.2) thelugageIscontinuousUllyUpdated, witcentfeatureslikemodulesandCoroutinestoimproveusActionalance.3) despitechallen

C#은 자동 쓰레기 수집 메커니즘을 사용하는 반면 C는 수동 메모리 관리를 사용합니다. 1. C#의 쓰레기 수집기는 메모리 누출 위험을 줄이기 위해 메모리를 자동으로 관리하지만 성능 저하로 이어질 수 있습니다. 2.C는 유연한 메모리 제어를 제공하며, 미세 관리가 필요한 애플리케이션에 적합하지만 메모리 누출을 피하기 위해주의해서 처리해야합니다.
