C Cheatsheet
48 essential methods for coding interviews
Showing 48 of 48 methods
| Method | Syntax | Description | Time | Priority |
|---|---|---|---|---|
strlen | size_t strlen(const char *str) | Returns the length of a string (excluding null terminator) | O(n) | essential |
strcpy | char *strcpy(char *dest, const char *src) | Copies string from src to dest including null terminator | O(n) | essential |
strncpy | char *strncpy(char *dest, const char *src, size_t n) | Copies at most n characters from src to dest | O(n) | essential |
strcat | char *strcat(char *dest, const char *src) | Appends src string to the end of dest | O(n + m) | essential |
strcmp | int strcmp(const char *s1, const char *s2) | Compares two strings lexicographically | O(n) | essential |
strchr | char *strchr(const char *str, int c) | Finds first occurrence of character c in string | O(n) | essential |
strstr | char *strstr(const char *haystack, const char *needle) | Finds first occurrence of substring needle in haystack | O(n * m) | essential |
memset | void *memset(void *ptr, int value, size_t num) | Fills memory block with specified byte value | O(n) | essential |
memcpy | void *memcpy(void *dest, const void *src, size_t n) | Copies n bytes from src to dest (non-overlapping) | O(n) | essential |
memmove | void *memmove(void *dest, const void *src, size_t n) | Copies n bytes from src to dest (handles overlapping) | O(n) | essential |
malloc | void *malloc(size_t size) | Allocates size bytes of uninitialized memory | O(1) typical | essential |
calloc | void *calloc(size_t num, size_t size) | Allocates and zero-initializes array of num elements | O(n) | essential |
free | void free(void *ptr) | Deallocates previously allocated memory | O(1) | essential |
qsort | void qsort(void *base, size_t num, size_t size, int (*cmp)(const void*, const void*)) | Sorts array using quicksort algorithm | O(n log n) average | essential |
bsearch | void *bsearch(const void *key, const void *base, size_t num, size_t size, int (*cmp)(const void*, const void*)) | Binary search in sorted array | O(log n) | essential |
atoi | int atoi(const char *str) | Converts string to integer | O(n) | essential |
snprintf | int snprintf(char *str, size_t size, const char *format, ...) | Writes formatted output to string with size limit | O(n) | essential |
printf | int printf(const char *format, ...) | Prints formatted output to stdout | O(n) | essential |
scanf | int scanf(const char *format, ...) | Reads formatted input from stdin | O(n) | essential |
fgets | char *fgets(char *str, int n, FILE *stream) | Reads line from stream into buffer (safe) | O(n) | essential |
abs | int abs(int n) | Returns absolute value of integer | O(1) | essential |
isalpha | int isalpha(int c) | Checks if character is alphabetic (a-z, A-Z) | O(1) | essential |
isdigit | int isdigit(int c) | Checks if character is digit (0-9) | O(1) | essential |
isalnum | int isalnum(int c) | Checks if character is alphanumeric | O(1) | essential |
tolower | int tolower(int c) | Converts character to lowercase | O(1) | essential |
toupper | int toupper(int c) | Converts character to uppercase | O(1) | essential |
sizeof | sizeof(type) or sizeof(expression) | Returns size in bytes of type or variable | O(1) | essential |
strncat | char *strncat(char *dest, const char *src, size_t n) | Appends at most n characters from src to dest | O(n + m) | common |
strncmp | int strncmp(const char *s1, const char *s2, size_t n) | Compares at most n characters of two strings | O(n) | common |
strrchr | char *strrchr(const char *str, int c) | Finds last occurrence of character c in string | O(n) | common |
strtok | char *strtok(char *str, const char *delim) | Tokenizes string by delimiters (modifies original string) | O(n) | common |
memcmp | int memcmp(const void *s1, const void *s2, size_t n) | Compares n bytes of two memory blocks | O(n) | common |
realloc | void *realloc(void *ptr, size_t new_size) | Resizes previously allocated memory block | O(n) | common |
strtol | long strtol(const char *str, char **endptr, int base) | Converts string to long with error detection and base support | O(n) | common |
sprintf | int sprintf(char *str, const char *format, ...) | Writes formatted output to string buffer | O(n) | common |
sscanf | int sscanf(const char *str, const char *format, ...) | Reads formatted input from string | O(n) | common |
fputs | int fputs(const char *str, FILE *stream) | Writes string to stream (no newline) | O(n) | common |
getchar | int getchar(void) | Reads single character from stdin | O(1) | common |
putchar | int putchar(int c) | Writes single character to stdout | O(1) | common |
fabs | double fabs(double x) | Returns absolute value of double | O(1) | common |
sqrt | double sqrt(double x) | Returns square root of x | O(1) | common |
pow | double pow(double base, double exp) | Returns base raised to power exp | O(1) | common |
floor | double floor(double x) | Rounds down to nearest integer | O(1) | common |
ceil | double ceil(double x) | Rounds up to nearest integer | O(1) | common |
isspace | int isspace(int c) | Checks if character is whitespace | O(1) | common |
rand | int rand(void) | Returns pseudo-random number between 0 and RAND_MAX | O(1) | common |
exit | void exit(int status) | Terminates program with status code | O(1) | useful |
assert | void assert(int expression) | Aborts if expression is false (debugging) | O(1) | useful |