Skip to content

Instantly share code, notes, and snippets.

@valbaca
Created June 16, 2018 21:14
Show Gist options
  • Save valbaca/1ba78a17935c071492819dab22646a78 to your computer and use it in GitHub Desktop.
Save valbaca/1ba78a17935c071492819dab22646a78 to your computer and use it in GitHub Desktop.
succinct reference for the most used parts of C11 Standard Library, everything except time.h fits on one printed page. NOT meant to be comprehensive, but a reference for learning.
/* use $ man 3 <function> */
#include <stdlib.h>
void *calloc(size_t nitems, size_t size);
void *malloc(size_t size);
void *realloc(void *ptr, size_t size);
void free(void *ptr);
int rand(void); /* [0, RAND_MAX), use random() or arc4random() instead */
void srand(unsigned int seed);
#include <math.h>
double exp(double x);
double log(double x);
double pow(double x, double y);
double sqrt(double x);
double ceil(double x);
double fabs(double x);
double floor(double x);
double fmod(double x, double y);
#include <stdarg.h>
typedef va_list;
void va_start(va_list ap, last_arg); /* must call to start */
type va_arg(va_list ap, type); /* get next arg */
void va_end(va_list ap); /* must call to end */
#include <stddef.h>
#define NULL 0
typedef size_t
#include <stdio.h>
int printf(const char *format, ...); /* prefix: f=file, s=string, v=var arg */
int scanf(const char *format, ...); /* prefix: f=file, s=string */
FILE *fopen(const char *filename, const char *mode);
char *fgets(char *s, int n, FILE *stream);
int fputs(const char *s, FILE *stream);
#include <string.h>
size_t strlen(const char *s);
/* copies & moves are to dst from src: dst<-src */
void *memchr(const void *s, int c, size_t n); /* find c in s within n */
void *memcpy(void *dst, const void *src, size_t n); /* faster */
void *memmove(void *dst, const void *src, size_t n); /* overlap safe */
void *memset(void *s, int c, size_t n); /* put c in s n times */
char *strncat(char *dst, const char *src, size_t n); /* append dst += src */
char *strcpy(char *dst, const char *src);
char *strncpy(char *dst, const char *src, size_t n);
/* compare. result 0 means ==; <0 means s1 < s2; >0 means s1 > s2 */
int memcmp(const void *s1, const void *s2, size_t n);
int strcmp(const char *s1, const char *s2);
int strncmp(const char *s1, const char *s2, size_t n);
/* searching (t = text, p = pattern) */
void *memchr(const void *s, int c, size_t n);
char *strchr(const char *s, int c); /* find c in s */
char *strrchr(const char *s, int c); /* reverse search */
char *strpbrk(const char *t, const char *p); /* find s2 in s1 */
size_t strcspn(const char *t, const char *p); /* "complementary span" */
size_t strspn(const char *t, const char *p);
char *strtok(char *t, const char *p); /* tokenized search */
#include <time.h>
typedef clock_t; /* relative time, like a stopwatch "stop clock" */
typedef time_t; /* absolute time, like a calendar, "real time" */
struct tm { /* time struct, used to make human-readable */
int tm_sec; /* seconds after the minute (0 to 61, for leap secs) */
int tm_min; /* minutes after the hour (0 to 59) */
int tm_hour; /* hours since midnight (0 to 23) */
int tm_mday; /* day of the month (1 to 31) */
int tm_mon; /* months since January (0 to 11) NOTICE! 0-indexed months! */
int tm_year; /* years since 1900 */
int tm_wday; /* days since Sunday (0 to 6 Sunday=0) */
int tm_yday; /* days since January 1 (0 to 365) */
int tm_isdst; /* Daylight Savings Time (0 if not DST) */
};
/* create */
clock_t clock(void);
time_t time(time_t *timer); /* either call w/ 0 & use return OR with &time */
double difftime(time_t time1, time_t time2); /* t1 - t2 in seconds */
/* convert types */
struct tm *gmtime(const time_t *timer); /* UTC, time_t to struct tm */
struct tm *localtime(const time_t *timer); /* local, time_t to struct tm */
time_t mktime(struct tm *timeptr); /* struct tm to time_t */
/* to/from strings */
char *asctime(const struct tm *timeptr);
char *ctime(const time_t *timer);
size_t strftime(char *str, size_t max, const char *fmt, const struct tm *tptr);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment