Tech News

C Cheat Sheet


C Standard Language Cheat Sheet: Built-In C Methods With Descriptions

Input & Output Functions
int fclose(FILE *stream)
Closes the stream. All buffers are flushed
void clearerr(FILE *stream)
Clears the end-of-file and error indicators for the given stream
int feof(FILE *stream)
Tests the end-of-file indicator for the given stream
int ferror(FILE *stream)
Tests the error indicator for the given stream
int fflush(FILE *stream)
Flushes the output buffer of a stream
int fgetpos(FILE *stream, fpos_t *pos)
Gets the current file position of the stream and writes it to pos
FILE *fopen(const char *filename, const char *mode)
Opens the filename pointed to by filename using the given mode
size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream)
Reads data from the given stream into the array pointed to by ptr
FILE *freopen(const char *filename, const char *mode, FILE *stream)
Associates a new filename with the given open stream and same time closing the old file in stream
int fseek(FILE *stream, long int offset, int whence)
Sets the file position of the stream to the given offset. The argument offset signifies the number of bytes to seek from the given whence position
int fsetpos(FILE *stream, const fpos_t *pos)
Sets the file position of the given stream to the given position. The argument pos is a position given by the function fgetpos
long int ftell(FILE *stream)
Returns the current file position of the given stream
size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)
Writes data from the array pointed to by ptr to the given stream
int remove(const char *filename)
Deletes the given filename so that it is no longer accessible
int rename(const char *old_filename, const char *new_filename)
Causes the filename referred to, by old_filename to be changed to new_filename
void rewind(FILE *stream)
Sets the file position to the beginning of the file of the given stream
void setbuf(FILE *stream, char *buffer)
Defines how a stream should be buffered
int setvbuf(FILE *stream, char *buffer, int mode, size_t size)
Another function to define how a stream should be buffered
FILE *tmpfile(void)
Creates a temporary file in binary update mode (wb+)
char *tmpnam(char *str)
Generates and returns a valid temporary filename which does not exist
int fprintf(FILE *stream, const char *format, ...)
Sends formatted output to a stream
int printf(const char *format, ...)
Sends formatted output to stdout
int sprintf(char *str, const char *format, ...)
Sends formatted output to a string
int vfprintf(FILE *stream, const char *format, va_list arg)
Sends formatted output to a stream using an argument list
int vprintf(const char *format, va_list arg)
Sends formatted output to stdout using an argument list
int vsprintf(char *str, const char *format, va_list arg)
Sends formatted output to a string using an argument list
int fscanf(FILE *stream, const char *format, ...)
Reads formatted input from a stream
int scanf(const char *format, ...)
Reads formatted input from stdin
int sscanf(const char *str, const char *format, ...)
Reads formatted input from a string
int fgetc(FILE *stream)
Gets the next character (an unsigned char) from the specified stream and advances the position indicator for the stream
char *fgets(char *str, int n, FILE *stream)
Reads a line from the specified stream and stores it into the string pointed to by str. It stops when either (n-1) characters are read, the newline character is read, or the end-of-file is reached, whichever comes first
int fputc(int char, FILE *stream)
Writes a character (an unsigned char) specified by the argument char to the specified stream and advances the position indicator for the stream
int fputs(const char *str, FILE *stream)
Writes a string to the specified stream up to but not including the null character
int getc(FILE *stream)
Gets the next character (an unsigned char) from the specified stream and advances the position indicator for the stream
int getchar(void)
Gets a character (an unsigned char) from stdin
char *gets(char *str)
Reads a line from stdin and stores it into the string pointed to by, str. It stops when either the newline character is read or when the end-of-file is reached, whichever comes first
int putc(int char, FILE *stream)
Writes a character (an unsigned char) specified by the argument char to the specified stream and advances the position indicator for the stream
int putchar(int char)
Writes a character (an unsigned char) specified by the argument char to stdout
int puts(const char *str)
Writes a string to stdout up to but not including the null character. A newline character is appended to the output
int ungetc(int char, FILE *stream)
Pushes the character char (an unsigned char) onto the specified stream so that the next character is read
void perror(const char *str)
Prints a descriptive error message to stderr. First the string str is printed followed by a colon and then a space
Character Functions
isdigit()
Returns non-0 if the argument is a digit between 0 and 9
isalpha()
Returns a non-0 value if the argument is a letter of the alphabet
isalnum()
Returns a non-0 value if the argument is a letter or digit
islower()
Returns non-0 if the argument is a lowercase letter
isupper()
Returns a non-0 value if the argument is an uppercase letter
Location Functions
char *setlocale(int category, const char *locale)
Sets or reads location dependent information
struct lconv *localeconv(void)
Sets or reads location dependent information
Mathematics Functions
acos(x)
Returns the arc cosine of x in radians
asin(x)
Returns the arc sine of x in radians
atan(x)
Returns the arc tangent of x in radians
atan2(y, x)
Returns the arc tangent in radians of y/x based on the signs of both values to determine the correct quadrant
cos(x)
Returns the cosine of a radian angle x
cosh(x)
Returns the hyperbolic cosine of x
sin(x)
Returns the sine of a radian angle x
sinh(x)
Returns the hyperbolic sine of x
tanh(x)
Returns the hyperbolic tangent of x
exp(x)
Returns the value of e raised to the xth power
frexp(x, int *exponent)
The returned value is the mantissa and the integer pointed to by exponent is the exponent. The resultant value is x = mantissa * 2 ^ exponent
ldexp(x, int exponent)
Returns x multiplied by 2 raised to the power of exponent
log(x)
Returns the natural logarithm (base-e logarithm) of x
log10(x)
Returns the common logarithm (base-10 logarithm) of x
modf(x, *integer)
The returned value is the fraction component (part after the decimal), and sets integer to the integer component
pow(x, y)
Returns x raised to the power of y
sqrt(x)
Returns the square root of x
ceil(x)
Returns the smallest integer value greater than or equal to x
fabs(x)
Returns the absolute value of x
floor(x)
Returns the largest integer value less than or equal to x
fmod(x, y)
Returns the remainder of x divided by y
Time & Date Functions
char *asctime(const struct tm *timeptr)
Returns a pointer to a string which represents the day and time of the structure timeptr
clock_t clock(void)
Returns the processor clock time used since the beginning of an implementation defined era (normally the beginning of the program)
char *ctime(const time_t *timer)
Returns a string representing the localtime based on the argument timer
double difftime(time_t time1, time_t time2)
Returns the difference of seconds between time1 and time2 (time1-time2)
struct tm *gmtime(const time_t *timer)
The value of timer is broken up into the structure tm and expressed in Coordinated Universal Time (UTC) also known as Greenwich Mean Time (GMT)
struct tm *localtime(const time_t *timer)
The value of timer is broken up into the structure tm and expressed in the local time zone
time_t mktime(struct tm *timeptr)
Converts the structure pointed to by timeptr into a time_t value according to the local time zone
size_t strftime(char *str, size_t maxsize, const char *format, const struct tm *timeptr)
Formats the time represented in the structure timeptr according to the formatting rules defined in format and stored into str
time_t time(time_t *timer)
Calculates the current calender time and encodes it into time_t format
Miscellaneous Functions
void longjmp(jmp_buf environment, int value)
Restores the environment saved by the most recent call to setjmp() macro in the same invocation of the program with the corresponding jmp_buf argument
void (*signal(int sig, void (*func)(int)))(int)
Sets a function to handle signal i.e. a signal handler
int raise(int sig)
Causes signal to be generated. The sig argument is compatible with the SIG macros
malloc()
Provides dynamic memory allocation
rand()
Generates a random number
srand()
Used to set the starting point for rand()
Type Functions


int isalnum(int c)
Checks whether the passed character is alphanumeric
int isalpha(int c)
Checks whether the passed character is alphabetic

int iscntrl(int c)
Checks whether the passed character is control character

int isdigit(int c)
Checks whether the passed character is decimal digit

int isgraph(int c)
Checks whether the passed character has graphical representation using locale

int islower(int c)
Checks whether the passed character is lowercase letter

int isprint(int c)
Checks whether the passed character is printable

int ispunct(int c)
Checks whether the passed character is a punctuation character

int isspace(int c)
Checks whether the passed character is white-space

int isupper(int c)
Checks whether the passed character is an uppercase letter

int isxdigit(int c)
Checks whether the passed character is a hexadecimal digit

int tolower(int c)
Converts uppercase letters to lowercase










General Functions
double atof(const char *str)
Converts the string pointed to, by the argument str to a floating-point number (type double)
int atoi(const char *str)
Converts the string pointed to, by the argument str to an integer (type int)
long int atol(const char *str)
Converts the string pointed to, by the argument str to a long integer (type long int)
double strtod(const char *str, char **endptr)
Converts the string pointed to, by the argument str to a floating-point number (type double)
long int strtol(const char *str, char **endptr, int base)
Converts the string pointed to, by the argument str to a long integer (type long int)
unsigned long int strtoul(const char *str, char **endptr, int base)
Converts the string pointed to, by the argument str to an unsigned long integer (type unsigned long int)
void *calloc(size_t nitems, size_t size)
Allocates the requested memory and returns a pointer to it
void free(void *ptr)
Deallocates the memory previously allocated by a call to calloc, malloc, or realloc
void *malloc(size_t size)
Allocates the requested memory and returns a pointer to it
void *realloc(void *ptr, size_t size)
Attempts to resize the memory block pointed to by ptr that was previously allocated with a call to malloc or calloc
void abort(void)
Causes an abnormal program termination
int atexit(void (*func)(void))
Causes the specified function func to be called when the program terminates normally
void exit(int status)
Causes the program to terminate normally
char *getenv(const char *name)
Searches for the environment string pointed to by name and returns the associated value to the string
int system(const char *string)
The command specified by string is passed to the host environment to be executed by the command processor
void *bsearch(const void *key, const void *base, size_t nitems, size_t size, int (*compar)(const void *, const void *))
Performs a binary search
void qsort(void *base, size_t nitems, size_t size, int (*compar)(const void *, const void*))
Sorts an array
int abs(int x)
Returns the absolute value of x
div_t div(int numer, int denom)
Divides numer (numerator) by denom (denominator)
long int labs(long int x)
Returns the absolute value of x
ldiv_t ldiv(long int numer, long int denom)
Divides numer (numerator) by denom (denominator)
int rand(void)
Returns a pseudo-random number in the range of 0 to RAND_MAX
void srand(unsigned int seed)
This function seeds the random number generator used by the function rand
int mblen(const char *str, size_t n)
Returns the length of a multibyte character pointed to by the argument str
size_t mbstowcs(schar_t *pwcs, const char *str, size_t n)
Converts the string of multibyte characters pointed to by the argument str to the array pointed to by pwcs
int mbtowc(whcar_t *pwc, const char *str, size_t n)
Examines the multibyte character pointed to by the argument str
size_t wcstombs(char *str, const wchar_t *pwcs, size_t n)
Converts the codes stored in the array pwcs to multibyte characters and stores them in the string str
int wctomb(char *str, wchar_t wchar)
Examines the code which corresponds to a multibyte character given by the argument wchar
String Functions
strcat()
Concatenates a copy of string2 to string1
strcmp()
Compares two strings
strcpy()
Copies contents of string2 to string1
void *memchr(const void *str, int c, size_t n)
Searches for the first occurrence of the character c (an unsigned char) in the first n bytes of the string pointed to, by the argument str
int memcmp(const void *str1, const void *str2, size_t n)
Compares the first n bytes of string1 and string2
void *memcpy(void *dest, const void *src, size_t n)
Copies n characters from src to dest
void *memmove(void *dest, const void *src, size_t n)
Another function to copy n characters from string2 to string1
void *memset(void *str, int c, size_t n)
Copies the character c (an unsigned char) to the first n characters of the string pointed to, by the argument str
char *strcat(char *dest, const char *src)
Appends the string pointed to, by src to the end of the string pointed to by dest
char *strncat(char *dest, const char *src, size_t n)
Appends the string pointed to, by src to the end of the string pointed to, by dest up to n characters long
char *strchr(const char *str, int c)
Searches for the first occurrence of the character c (an unsigned char) in the string pointed to, by the argument str
int strcmp(const char *str1, const char *str2)
Compares the string pointed to, by string1 to the string pointed to by string2
int strncmp(const char *str1, const char *str2, size_t n)
Compares at most the first n bytes of string1 and string2
int strcoll(const char *str1, const char *str2)
Compares string str1 to str2. The result is dependent on the LC_COLLATE setting of the location
char *strcpy(char *dest, const char *src)
Copies the string pointed to, by src to dest
char *strncpy(char *dest, const char *src, size_t n)
Copies up to n characters from the string pointed to, by src to dest
size_t strcspn(const char *str1, const char *str2)
Calculates the length of the initial segment of string1 which consists entirely of characters not in string2
char *strerror(int errnum)
Searches an internal array for the error number errnum and returns a pointer to an error message string
size_t strlen(const char *str)
Computes the length of the string str up to but not including the terminating null character
char *strpbrk(const char *str1, const char *str2)
Finds the first character in the string string1 that matches any character specified in string2
char *strrchr(const char *str, int c)
Searches for the last occurrence of the character c (an unsigned char) in the string pointed to by the argument str
size_t strspn(const char *str1, const char *str2)
Calculates the length of the initial segment of string1 which consists entirely of characters in string2
char *strstr(const char *haystack, const char *needle)
Finds the first occurrence of the entire string needle (not including the terminating null character) which appears in the string haystack
char *strtok(char *str, const char *delim)
Breaks string str into a series of tokens separated by delim
size_t strxfrm(char *dest, const char *src, size_t n)
Transforms the first n characters of the string src into current locale and places them in the string dest

No comments:

Post a Comment