C Function Reference
This quick-reference guide contains lists of standard C functions that you may find useful in this course. It is not an exhaustive list and all of this information is widely available on the internet; this is just a starting point if you're feeling lost. Pay particular attention to the list near the bottom of unsafe functions -- you are forbidden from using these on CS 261 projects.
All functions: C function reference
String Manipulation
- size_t strlen
(char *str)
Calculate the length of a null-terminated string - int snprintf
(char *buffer, int *bufsize, char *format, ...)
Copy a string or convert data to string - long strtol
(char *str, char **end, int base)
Convert a string to a long integer - float strtof
(char *str, char **end)
Convert a string to a float - double strtod
(char *str, char **end)
Convert a string to a double - char* strncat
(char *dest, char *src, size_t count)
Concatenate one string onto the end of another - int strncmp
(char *lhs, char *rhs, size_t count)
Compare two strings - char* strstr
(char *str, char *substr)
Search for a substring
File I/O
- FILE* fopen
(char *filename, char *mode)
Open a file (modes: 'r', 'w', 'a') - int fgetc
(FILE *stream)
Read a single character from a file - char* fgets
(char *str, int count, FILE *stream)
Read a line of text from a file - int fscanf
(FILE *stream, char *format, ...)
Read formatted data from a file (scanf assumes stdin) - size_t fread
(void *buffer, size_t size, size_t count, FILE *stream)
Read (size x count) bytes from a file - int fseek
(FILE *stream, long offset, int origin)
Set the current file position (origin: 'SEEK_SET', 'SEEK_CUR') - int fprintf
(FILE *stream, char *format, ...)
Write formatted text to a file (printf assumes stdout) - size_t fwrite
(void *buffer, size_t size, size_t count, FILE *stream)
Write (size x count) bytes to a file - int fclose
(FILE *stream)
Close a file
Memory Management
- void* malloc
(size_t size)
Allocate memory on the heap - void* calloc
(size_t num, size_t size)
Allocate (num x size) bytes on the heap and initialize all bytes to zero - void* memset
(void *dest, int ch, size_t count)
Sets bytes to the value of (unsigned char)ch - void* memcpy
(void *dest, const void *src, size_t count)
Copy bytes from one memory location to another - void free
(void *ptr)
Deallocates space previously allocated by malloc() or calloc()
Miscellaneous
- int getopt
(int argc, char **argv, const char *optstring)
Parse command-line parameters
Unsafe Functions
Some functions in the C standard library are now considered to be "unsafe" because they are vulnerable to buffer overruns. For the purposes of this class, you may not use any of the following unsafe functions:
UNSAFE | Safer alternative |
---|---|
atoi/atol | strtol |
atoll | strtoll |
atof | strtof / strtod |
gets | fgets |
strcat | strncat |
strcpy | snprintf |
sprintf | snprintf |
Be careful as you read through sample code that you find online or even in more reputable sources like textbooks! These functions were once considered standard and the use of them is still (unfortunately) widespread.
Formatted Input/Output
Here is a list of important format specifiers for fprintf / fscanf:
Code | Description |
---|---|
%d |
signed integer (int ) |
%lu |
unsigned long integer (size_t ) |
%x |
hex unsigned integer (unsigned int ) |
%lx |
hex unsigned long integer (unsigned long ) |
%f |
floating-point number (float or double ) |
%e |
scientific notation (float or double ) |
%c |
character (char ) |
%s |
character string (char* ) |
%p |
pointer |