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
#include <stdio.h> int main() { printf("Hello, World!\n"); // Output: Hello, World! printf("Number: %d\n", 10); // Output: Number: 10 return 0; }
scanf
#include <stdio.h> int main() { int num; printf("Enter a number: "); scanf("%d", &num); printf("You entered: %d\n", num); return 0; }
gets
#include <stdio.h> int main() { char str[100]; printf("Enter a string: "); gets(str); printf("You entered: %s\n", str); return 0; }
fgets
#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
#include <stdio.h> int main() { putchar('A'); // Output: A putchar('\n'); return 0; }
getchar
#include <stdio.h> int main() { int c; printf("Enter a character: "); c = getchar(); printf("You entered: %c\n", c); return 0; }
puts
#include <stdio.h> int main() { puts("Hello, World!"); // Output: Hello, World! return 0; }
fputs
#include <stdio.h> int main() { fputs("Hello, World!\n", stdout); // Output: Hello, World! return 0; }
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
#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
#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
#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
#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
#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; }
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
#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
#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
#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
#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
#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
#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
#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
#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
#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
#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; }
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
#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
#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
#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
#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
#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
#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
#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
#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; }
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
#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
#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
#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
#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
#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
#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
#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
#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
#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
#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中文网其他相关文章!