Common Functions in C/C++: A Comprehensive Guide

Common Functions in C/C++: A Comprehensive Guide

In C/C++ courses and projects, the application of functions is crucial. Here, we have gathered and organized some commonly used functions in C/C++. We hope you can apply them frequently to gain proficiency.

1. Character Processing Functions

This category of functions is used for processing individual characters, including character type testing and case conversion.

Header File: ctype.h

int isalpha(int ch) returns a non-zero value if ch is a letter (‘A’-‘Z’, ‘a’-‘z’), otherwise returns 0.

int isalnum(int ch) returns a non-zero value if ch is a letter (‘A’-‘Z’, ‘a’-‘z’) or a digit (‘0’-‘9’), otherwise returns 0.

int isascii(int ch) returns a non-zero value if ch is a character (ASCII code 0-127), otherwise returns 0.

int iscntrl(int ch) returns a non-zero value if ch is a control character (0x00-0x1F) or the delete character (0x7F), otherwise returns 0.

int isdigit(int ch) returns a non-zero value if ch is a digit (‘0’-‘9’), otherwise returns 0.

int isgraph(int ch) returns a non-zero value if ch is a printable character (excluding space) (0x21-0x7E), otherwise returns 0.

int islower(int ch) returns a non-zero value if ch is a lowercase letter (‘a’-‘z’), otherwise returns 0.

int isprint(int ch) returns a non-zero value if ch is a printable character (including space) (0x20-0x7E), otherwise returns 0.

int ispunct(int ch) returns a non-zero value if ch is a punctuation character (0x00-0x1F), otherwise returns 0.

int isspace(int ch) returns a non-zero value if ch is a space (‘ ‘), horizontal tab (‘\t’), carriage return (‘\r’), form feed (‘\f’), vertical tab (‘\v’), or newline (‘\n’), otherwise returns 0.

int isupper(int ch) returns a non-zero value if ch is an uppercase letter (‘A’-‘Z’), otherwise returns 0.

int isxdigit(int ch) returns a non-zero value if ch is a hexadecimal digit (‘0’-‘9’, ‘A’-‘F’, ‘a’-‘f’), otherwise returns 0.

int tolower(int ch) returns the corresponding lowercase letter (‘a’-‘z’) if ch is an uppercase letter (‘A’-‘Z’).

int toupper(int ch) returns the corresponding uppercase letter (‘A’-‘Z’) if ch is a lowercase letter (‘a’-‘z’).

2. Mathematical Functions

This category provides various mathematical computation functions.

Header File: math.h

int abs(int i) returns the absolute value of the integer parameter i.

double cabs(struct complex znum) returns the absolute value of the complex number znum.

double fabs(double x) returns the absolute value of the double parameter x.

long labs(long n) returns the absolute value of the long integer parameter n.

double exp(double x) returns the value of the exponential function e^x.

double frexp(double value, int *eptr) returns the value of x in value = x * 2^n, with n stored in eptr.

double ldexp(double value, int exp) returns the value of value * 2^exp.

double log(double x) returns the value of log_e(x).

double log10(double x) returns the value of log_10(x).

double pow(double x, double y) returns the value of x^y.

double pow10(int p) returns the value of 10^p.

double sqrt(double x) returns the square root of x.

double acos(double x) returns the inverse cosine cos^-1(x), where x is in radians.

double asin(double x) returns the inverse sine sin^-1(x), where x is in radians.

double atan(double x) returns the inverse tangent tan^-1(x), where x is in radians.

double atan2(double y, double x) returns the inverse tangent tan^-1(y/x), where y and x are in radians.

double cos(double x) returns the cosine cos(x), where x is in radians.

double sin(double x) returns the sine sin(x), where x is in radians.

double tan(double x) returns the tangent tan(x), where x is in radians.

double cosh(double x) returns the hyperbolic cosine cosh(x), where x is in radians.

double sinh(double x) returns the hyperbolic sine sinh(x), where x is in radians.

double tanh(double x) returns the hyperbolic tangent tanh(x), where x is in radians.

double hypot(double x, double y) returns the length of the hypotenuse of a right triangle (z), where x and y are the lengths of the legs, z^2 = x^2 + y^2.

double ceil(double x) returns the smallest integer greater than or equal to x.

double floor(double x) returns the largest integer less than or equal to x.

void srand(unsigned seed) initializes the random number generator.

int rand() generates a random number and returns it.

double modf(double value, double *iptr) decomposes the double number value into its fractional and integral parts.

double fmod(double x, double y) returns the remainder of x/y.

3. String Processing Functions

This category of functions is used for operations such as merging and comparing strings.

Header File: string.h

char stpcpy(char *dest, const char *src) copies the string src to dest.

char strcat(char *dest, const char *src) appends the string src to the end of dest.

char strchr(const char *s, int c) locates and returns the position of the first occurrence of character c in string s.

int strcmp(const char *s1, const char *s2) compares the strings s1 and s2, returning s1 – s2.

char strcpy(char *dest, const char *src) copies the string src to dest.

size_t strcspn(const char *s1, const char *s2) scans s1 and returns the number of characters that are in both s1 and s2.

char strdup(const char *s) duplicates the string s into newly allocated memory.

int stricmp(const char *s1, const char *s2) compares strings s1 and s2, returning s1 – s2.

size_t strlen(const char *s) returns the length of the string s.

char strlwr(char *s) converts all uppercase letters in string s to lowercase and returns the modified string.

char strncat(char *dest, const char *src, size_t maxlen) appends at most maxlen characters from string src to string dest.

int strncmp(const char *s1, const char *s2, size_t maxlen) compares the first maxlen characters of strings s1 and s2.

char strncpy(char *dest, const char *src, size_t maxlen) copies at most maxlen characters from src to dest.

int strnicmp(const char *s1, const char *s2, size_t maxlen) compares the first maxlen characters of strings s1 and s2.

char strnset(char *s, int ch, size_t n) sets the first n characters of string s to ch.

char strpbrk(const char *s1, const char *s2) scans string s1 and returns the number of characters that are in both s1 and s2.

char strrchr(const char *s, int c) scans for the last occurrence of character c in string s.

char strrev(char *s) reverses the order of characters in string s and returns the modified string.

char strset(char *s, int ch) sets all characters in string s to the specified character ch.

size_t strspn(const char *s1, const char *s2) scans string s1 and returns the number of characters that are in both s1 and s2.

char strstr(const char *s1, const char *s2) scans string s2 and returns the position of the first occurrence of s1.

char strtok(char *s1, const char *s2) tokenizes string s1 using the delimiters defined in string s2.

Common Functions in C/C++: A Comprehensive Guide

Nanjing University of Science and Technology Undergraduate Academic Guidance Center

WeChat Official Account: NUST Learning Space

Address: Yifu Building 201

Leave a Comment