String Operations
- Found in <string.h>
strcat(s, t) | concatenate t to the end of s |
strncat(s, t, n) | concatenate n characters of t to the end of s |
strcmp(s, t) | return negative, zero or positive for s < t, s == t or s > t |
strncmp(s, t, n) | same as strcmp but only in the first n characters |
strcpy(s, t) | copy t to s |
strncpy(s, t, n) | copy at most n characters to t to s |
strlen(s) | return length of s |
strchr(s, c) | return pointer to first c in s, or NULL if absent |
strrchr(s, c) | return pointer to last c in s, or NULL if absent |
Character Tests and Conversions
- Found in <ctype.h>
isalpha(c) | non-zero if c is alphabetic, 0 otherwise |
isupper(c) | non-zero if c is upper case, 0 otherwise |
islower(c) | non-zero if c is lower case, 0 otherwise |
isdigit(c) | non-zero if c is digit, 0 otherwise |
isalnum(c) | non-zero if isalpha(c) or isdigit(c), 0 otherwise |
isspace(c) | nonzero if c is blank, tab, newline, return, formfeed, vertical tab else 0 |
toupper(c) | return c converted to upper case |
tolower(c) | return c converted to lower case |
Ungetc
int ungetc(int c, FILE *fp);
pushes the character c back onto file pointed by fp, and returns either c or EOF in case of error. Only one character of pushback is guaranteed per file. ungetc may be used with any of the input functions including scanf, getc, getchar, etc.
Command Execution
int system(char *command);
executes the command, then resumes execution of the current program. The contents of command depend on the operating system.
For example, on a Unix-like system:
system("date");
prints the date and time to the standard output. It returns an integer status from the command executed.
Storage Management
malloc and calloc can be used to obtain blocks of memory dynamically.
void *malloc(size_t n);
returns a pointer to n bytes of uninitialised storage, or NULL if the request cannot be completed.
void *calloc(size_t n, size_t size);
returns a pointer to enough space for an array of n objects of the specified size, or NULL if the request cannot be completed. The storage is initialised to zero.
The pointer returned by malloc and calloc have proper alignment for the object in question, but it must be cast into the appropriate type.
float *ptr; ptr = (float *) calloc(10, sizeof(float));
free(ptr);
frees the space pointed to by ptr, where ptr was originally obtained by either malloc or calloc.
You shouldn’t use ptr after it has been freed.
Mathematical Functions
- Found in <math.h>
sin(x) | sine of x; x in radians |
cos(x) | cosine of x; x in radians |
atan2(y, x) | arctangent of y/x; in radians |
exp(x) | exponential function |
log(x) | natural logarithm (base e) of x (x > 0) |
log10(x) | common logarithm (base 10) of x (x > 0) |
pow(x, y) | x to the power of y |
sqrt(x) | square root of x (x ≥ 0) |
fabs(x) | absolute value of x |
Random Number Generation
rand() computes a sequence of pseudo-random integers between 0 and RAND_MAX (defined in <stdlib.h>). srand sets the seed for rand.
#include <stdio.h> #include <stdlib.h> int main() { srand(0); printf("%d\n", rand()); printf("%d\n", rand()); printf("%d\n", rand()); printf("%d\n", rand()); return 0; }