Show Menu
Cheatography

C Reference Cheat Sheet (DRAFT) by

Basics about C

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Main Function

int main(int argc, char *argv[­]){­return int;}
Function Arguments
Descri­ption
int argc
Number of command line args
char *argv[]
Command line args in an array name of program

Data Types

Type
Bytes
Range
char
1
U:(0 to 28-1)
S:(-27 to 27-1)
short
2
U:(0 to 216-1)
S:(-215 to 215-1)
int
4
U:(0 to 231-1)
S:(-231 to 232-1)
long
4
U:(0 to 231-1)
S:(-231 to 232-1)
float
4
6 decimal places
long long
8
U:(0 to 264-1)
S:(-231 to 232-1)
double
8
15 decimal places
long double
10
19 decimal places

Math

#include <math.h>
Function
Descri­ption
sin(x)
sinh(x)
asin(x)
Sine of x
Hyperbolic sine of x
Arc sine of x
cos(x)
cosh(x)
acos(x)
Cosine of x
Hyperbolic cosine of x
Arc cosine of x
tan(x)
tanh(x)
atan(x)
Tangent of x
Hyperbolic tangent of x
Arc tangent of x
exp(x)
Returns value of e raised to the xth power
log(x)
log10(x)
Natural logarithm (base-e) of x
Base-10 of x
pow(x,y)
Returns x raised to the power of y
sqrt(x)
Square root of x
abs(x)
Absolute value of x (<s­tdl­ib.h­>)

Standard Library

#include <stdlib.h>
Function
Descri­ption
rand()
Returns a random long
qsort(­array, length, size, compr)
Quick Sort "­arr­ay" of array "­len­gth­" with elements of "­siz­e" by function "­com­pr"
Compr Function
int cmpfunc(const void * a, const void * b);{return (*(int*)a - *(int*)b );}

Char Library

#include <ctype.h>
tolowe­r(char)
Lowercase char
touppe­r(char)
Uppercase char
islowe­r(char)
Checks if char is lowercase
isuppe­r(char)
checks if char is uppercase
isnumb­er(­char)
Checks if char is 0-9
isalph­a(char)
Checks if char is a letter
isblan­k(char)
Checks if char is whitespace
 

Strings

#include <string.h>
Function
Descri­ption
strlen(str)
Return length of string "­str­"
strcat(dest,src)
Appends "­src­" to end of the string "­des­t"
strncat(dest,src,n)
Appends "­src­" to end of the string "­des­t" by upto "­n" characters long
strcpy(dest,str)
Copy string "­str­" to "­des­t" and return "­des­t"
strncpy(dest,src,n)
Copy "­n" characters from "­src­" to "­des­t", return "­des­t"
strcmp(str1,str2)
Compares "­str­1" to "­str­2"
strncmp(str1,str2,n)
Compares the first "­n" bytes of "­str­1" to "­str­2"
strtok­(st­r,d­elim)
Breaks string "­str­" into a series of tokens separated by "­del­im"
memcpy(dest,src,n)
Copies "­n" characters from "­src­" to "­des­t"
memset(str,c,n)
Copies the char "­c" to the first "­n" characters of the string "­str­"

Arrays

Declar­ation
type arrayName[size];
type arrayName[size][size];
1-Dimensional
2-Dimensional
Initia­liz­ation
int myArray[2] = {3,4};
int myArray[2] = {3}
Each element
All elements 3
Accessing
int a = myArra­y[c];
Value at "­c" position
&m­yAr­ray[c]
Memory address at "­c" position
Array Size
sizeof­(my­Array);
Returns length of array
Passing to Function
void foo(int *myArray)
Passed as pointer to array
void foo(int myArray[])
Passed array

Pointers

A pointer is a variable who's value is an address of another variable
In Arrays
 

Keywords (Non-t­rivial)

Keyword
Definition
auto
Defines variables as having a local lifetime
default
Declar­ation of a default case
extern
Extend the visiblity of the variables and functions
register
Hints to compiler that a given variable can be put in a register
union
Allows storage of different data types in same memory location
volatile
Used when a variable can change unexpe­ctedly
typedef
Assigns altern­ative names to existing types
static
Preserves value even after out of scope
enum
Used to assign names to integral constants
continue
Forces the next iteration in a loop

Memory

Function
Descri­ption
calloc(n,size)
Alloc array of "­n" elements each of size in bytes "­siz­e"
p=(T*)calloc(n,sizeof(t))
malloc(n)
Alloc array of "­n" bytes
p=(T*)malloc(sizeof(t))
realloc(addr,size)
Re-all­ocates memory extending it upto "­siz­e"
free(addr)
Releases block of memory specified by "­add­r"

File Input / Output

#include <stdio.h>
Function
Descri­ption
fopen(filename,mode)
Open file/c­reate new
(Mode: r,w,a,­r+,­w+,a+)
fclose( FILE *fp )
Closes file(R­etuns 0 for success, EOF for failure)
fputc(int c, FILE *fp)
Writes character vaue to output stream pointed by fp
fputs(­const char s, FILE fp)
Writes string to output stream pointed by fp
fgetc(FILE * fp)
Reads a character from input stream fp
fgets(char buf,
 int n, FILE
fp)
Reads n characters from input stream fp into buf

Standard Input / Output

Function
Descrition
getchar()
Reads next available character from screen
putcha­r(int c)
Puts character "­c" on screen
gets(char *s)
Reads line from stdin to buffer "­s"
puts(const char *s)
Writes string "­s" to stdout
scanf(­const char *forma­t,..)
Scans input from stdin into format
printf­(const char *forma­t,..)
Writes output to stdout according to format