Skip to content

Instantly share code, notes, and snippets.

@mattecapu
Last active January 18, 2017 09:04
Show Gist options
  • Save mattecapu/6631e02c3bc4c1f77b5c83836e4de3ab to your computer and use it in GitHub Desktop.
Save mattecapu/6631e02c3bc4c1f77b5c83836e4de3ab to your computer and use it in GitHub Desktop.
List of commonly used idioms
// alias type to new_name
typedef type new_name;
// declare an array
int vector[SIZE];
// declare an array, size undetermined
int vector[] = { 3, 4 };
void read_str( char s[] );
// main signature
int main (int argc, char *argv[]);
#include<stdlib.h>
// allocates size bytes of memory, if allocation fails returns NULL
// (memo: cast the returned pointer to the desired type)
void* malloc (size_t size);
// allocates a block of memory for an array of num elements, each of them size bytes long, and initializes all its bits to zero.
void* calloc (size_t num, size_t size);
// reallocates memory pointed by ptr, possibly moving it
void* realloc (void* ptr, size_t size);
// release memory previously allocated at ptr
void free (void* ptr);
// terminate execution gracefully
void exit (int status);
// biggest random integer returned by rand()
RAND_MAX // 32767
// returns a random integer in the interval [0, RAND_MAX]
int rand (void);
// initializes the pseudo-random number generator
void srand (unsigned int seed);
// sorts an array (base points to the first element of the sortee)
// compar(a, b) should return <0 if a < b, 0 if a = b, >0 if a > b
void qsort (void* base, size_t num, size_t size, int (*compar)(const void*,const void*));
#include<time.h>
// returns the current Unix epoch
time_t time (time_t* timer);
#include<stdio.h>
// default streams for OS I/O
stdout // write
stdin // read
stderr // write
// prints to stdout
// format is a string
// arguments after char are expected to be pointers
// %i - integer
// %d - decimal (optionally preceded by +/-)
// %f, %e, %g - floating point decimals
// %c - char
// %s - string of chars, stopping at the first whitespace char found
// %*<char> - read but ignore (do not store)
int printf ( const char * format, ... );
// reads from stdin, specifiers are read and stored in pointers passed after format
int scanf ( const char * format, ... );
// get character from stdin
int getchar ( void );
// file declaration
FILE* file_handle;
// open a file and returns its pointer, mode can be "r", "w" or "a" (with "+" appended for update)
// a NULL pointer is returned if file can't be opened
FILE * fopen ( const char * filename, const char * mode );
// close stream, if fails returns EOF
int fclose ( FILE * stream );
// like scanf(), but reads from the file pointed by stream
int fscanf ( FILE * stream, const char * format, ... );
// like printf(), but outputs to the file pointed by stream
int fprintf ( FILE * stream, const char * format, ... );
// returns a non-zero value if stream is at EOF
int feof ( FILE * stream );
// read a char from stream, returns EOF in case of error
int fgetc ( FILE * stream );
// reads bytes from a binary stream
size_t fread ( void * ptr, size_t size, size_t count, FILE * stream );
// writes bytes to a binary stream
size_t fwrite ( const void * ptr, size_t size, size_t count, FILE * stream );
#include<math.h>
// returns fractional part of x, stores integer part in intpart
double modf (double x, double* intpart);
#include<string.h>
// copy source to destination
char * strcpy ( char * destination, const char * source );
void * memcpy ( void * destination, const void * source, size_t num );
// appends a copy source string to destination
char * strcat ( char * destination, const char * source );
// returns the number of chars stored in str (\0 is not counted)
// sizeof(str) >= strlen(str)
size_t strlen ( const char * str );
#include<limits.h>
SHRT_MAX // 32.767
SHRT_MIN // -32.768
USHRT_MAX // 65.535
INT_MAX // 32.767
INT_MIN // -32.768
UINT_MAX // 65.535
LONG_MAX // 2.147.483.647
LONG_MIN // -2.147.483.648
ULONG_MAX // 4.294.967.295
#include<float.h>
FLT_MIN_10_EXP // -38
DBL_MIN_10_EXP // -308
LDBL_MIN_10_EXP
FLT_MAX_10_EXP // +38
DBL_MAX_10_EXP // +308
LDBL_MAX_10_EXP
FLT_EPSILON // 1.19209290e-07
DBL_EPSILON // 2.22044e-16
LDBL_EPSILON
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment