C is a powerful programming language that offers a wide range of built-in functions to simplify programming tasks and provide ready-to-use functionality. These built-in functions are part of the standard library and cover various areas such as input/output, mathematical operations, string manipulation, and more. In this article, we will explore some of the important and commonly used built-in functions in C.
1. <cctype>:
This library provides functions for character classification and manipulation.
Function | Purpose | Example |
---|---|---|
isalnum() | Checks if a character is alphanumeric. | isalnum('A') returns true. |
isalpha() | Checks if a character is alphabetic. | isalpha('7') returns false. |
isdigit() | Checks if a character is a decimal digit. | isdigit('3') returns true. |
islower() | Checks if a character is lowercase. | islower('x') returns true. |
isupper() | Checks if a character is uppercase. | isupper('Z') returns true. |
isspace() | Checks if a character is whitespace. | isspace(' ') returns true. |
tolower() | Converts a character to lowercase. | tolower('H') returns 'h'. |
toupper() | Converts a character to uppercase. | toupper('e') returns 'E'. |
2. <math.h>:
This library provides mathematical functions and constants.
Function | Purpose | Example |
---|---|---|
sqrt() | Calculates the square root of a number. | sqrt(25) returns 5. |
pow() | Calculates the power of a number. | pow(2, 3) returns 8. |
abs() | Returns the absolute value of a number. | abs(-10) returns 10. |
sin() | Calculates the sine of an angle (in radians). | sin(M_PI / 2) returns 1. |
cos() | Calculates the cosine of an angle (in radians). | cos(0) returns 1. |
tan() | Calculates the tangent of an angle (in radians). | tan(M_PI / 4) returns 1. |
3. <string.h>:
This library provides functions for string manipulation and memory operations.
Function | Purpose | Example |
---|---|---|
strlen() | Returns the length of a C-style string. | strlen("Hello") returns 5. |
strcpy() | Copies a C-style string to another. | char dest[10]; strcpy(dest, "Hello"); |
strcat() | Concatenates two C-style strings. | char str1[10] = "Hello"; char str2[10] = "World"; strcat(str1, str2); |
strcmp() | Compares two C-style strings lexicographically. | strcmp("apple", "banana") returns a negative value. |
4. <string>:
This library provides the string
class for more convenient string manipulation compared to C-style strings.
Member Function | Purpose | Example |
---|---|---|
length() | Returns the length of the string. | string str = "Hello"; str.length() returns 5. |
substr() | Returns a substring of the string. | string str = "Hello World"; str.substr(6, 5) returns "World". |
find() | Finds the first occurrence of a substring within the string. | string str = "Hello World"; str.find("World") returns 6. |
replace() | Replaces a portion of the string with another string. | string str = "Hello World"; str.replace(6, 5, "Universe") replaces "World" with "Universe". |
operator+ | Concatenates two strings. | string str1 = "Hello"; string str2 = "World"; string result = str1 + str2; |
operator== | Compares two strings for equality. | string str1 = "Hello"; string str2 = "World"; if (str1 == str2) ... |
size() | Returns the size of the string (same as length()). | string str = "Hello"; str.size() returns 5. |
5. <cstdlib>:
This library provides general-purpose functions.
Function/Macro | Purpose | Example |
---|---|---|
atoi() | Converts a string to an integer. | int num = atoi("123"); |
atof() | Converts a string to a floating-point number. | float num = atof("3.14"); |
rand() | Generates a random integer. | int randomNum = rand(); |
srand() | Seeds the random number generator. | srand(time(NULL)); |
exit() | Terminates the program. | exit(0); |
6. <ctime>:
This library provides functions for working with time and dates.
Function/Structure | Purpose | Example |
---|---|---|
time() | Obtains the current calendar time. | time_t currentTime = time(NULL); |
ctime() | Converts a time value to a string representation. | time_t currentTime = time(NULL); char* timeString = ctime(¤tTime); |
difftime() | Calculates the difference in seconds between two time values. | time_t startTime = time(NULL); // Perform some operations... time_t endTime = time(NULL); double duration = difftime(endTime, startTime); |
7. <algorithm>:
This library provides functions for performing operations on sequences of elements.
Function | Purpose | Example |
---|---|---|
sort() | Sorts a sequence of elements. | int arr[] = {3, 1, 4, 1, 5}; sort(arr, arr + 5); |
binary_search() | Checks if an element exists in a sorted sequence. | int arr[] = {1, 2, 3, 4, 5}; bool exists = binary_search(arr, arr + 5, 3); |
find() | Finds the first occurrence of an element in a sequence. | vector<int> vec = {10, 20, 30, 40, 50}; auto it = find(vec.begin(), vec.end(), 30); |
8. <stdlib.h>:
This library provides functions for memory allocation and other utility functions.
Function | Purpose | Example |
---|---|---|
malloc() | Allocates a block of memory. | int* ptr = (int*)malloc(5 * sizeof(int)); |
calloc() | Allocates a block of memory and initializes it to zero. | int* ptr = (int*)calloc(5, sizeof(int)); |
realloc() | Changes the size of a previously allocated block of memory. | int* ptr = (int*)malloc(5 * sizeof(int)); ptr = (int*)realloc(ptr, 10 * sizeof(int)); |
free() | Frees a block of memory previously allocated by malloc , calloc , or realloc . |
free(ptr); |