Professional Documents
Culture Documents
Ashlyn Black C Reference
Ashlyn Black C Reference
Integers
0b11111111
binary
0B11111111
binary
0377
octal
255
decimal
0xff
hexadecimal
0xFF
hexadecimal
Bytes
88.0f / 88.1234567f
char
unsigned OR signed
unsigned char
0 to 28-1
88.0 / 88.123456789012345
signed char
-27 to 27-1
int
2/4
unsigned OR signed
Signage
unsigned int
2/4
0 to 216 -1 OR 231 -1
signed int
2/4
Real Numbers
42 / +42
positive
negative
-42
Binary notation 0b... / 0B... is available on GCC and most but not all C
compilers.
Variables
Declaring
int x;
A variable.
char x = 'C';
float x, y, z;
Value Range
short
unsigned OR signed
unsigned short
0 to 216 -1
signed short
-215 to 215 -1
long
4/8
unsigned OR signed
unsigned long
4/8
0 to 232 -1 OR 264 -1
signed long
4/8
long long
unsigned OR signed
0 to 264 -1
-263 to 263 -1
Floats
Naming
johnny5IsAlive;
Type
Bytes
letter.
2001ASpaceOddysey;
while;
Reserved keyword.
how exciting!;
Non-alphanumeric.
iamaverylongvariablenameohmygoshyesiam;
Longer than 31 characters (C89 & C90 only)
Constants are CAPITALISED. Function names usually take the form of a
verb eg. plotRobotUprising().
By Ashlyn Black
Sponsored by Readability-Score.com
cheatography.com/ashlyn-black/
Page 1 of 13.
https://readability-score.com
double
8/
4
long double
1.210-38 to 3.41038
for AVR.
There is no primitive boolean type, only zero (false, 0) and non-zero
Qualifiers
const type
volatile type
optimise.)
Storage Classes
register
static
Type
Bytes
Value Range
int8_t
-27 to 27-1
uint8_t
0 to 28-1
int16_t
-215 to 215 -1
uint16_t
0 to 216 -1
int32_t
-231 to 231 -1
uint32_t
0 to 232 -1
int64_t
-263 to 263 -1
uint64_t
0 to 264 -1
Typecasting
(type)a
Bytes
Value Range
bool
true / false or 0 / 1
type x; type y; };
item *next; };
Declaring
struct strctName
varName;
strctName.
By Ashlyn Black
Sponsored by Readability-Score.com
cheatography.com/ashlyn-black/
Page 2 of 13.
https://readability-score.com
Unions
struct strctName
Defining
*ptrName;
ptrName.
union uName{int
x; char y[8];}
a; type b; } varName;
Declaring
type.
struct strctName
varName = { a, b };
union uN vName;
Accessing
vName.y[int]
Accessing
varName.x
ptrName->x
Unions are used for storing multiple data types in the same area of
memory.
member x.
Enumeration
Bit Fields
struct{char a:4, b:4}
Defining
x;
enum bool {
false, true };
Declaring
Type Definitions
enum bool
Defining
varName;
a, b;}newType;
structure.
true}bool;
type.
Assigning
varName = true;
Evaluating
if(varName ==
false)
Declaring
uint16 x = 65535;
Pointers
Declaring
type *x;
By Ashlyn Black
Sponsored by Readability-Score.com
cheatography.com/ashlyn-black/
Page 3 of 13.
https://readability-score.com
Arrays (cont)
void *v;
is unknown.
A data structure pointer.
&name[int]
type
name + int
z[];
array element.
struct
Same as name[int].
*(name + int)
type *y;
Same as &name[int].
Elements are stored in contiguous memory.
Measuring
Accessing
sizeof(array) /
A memory address.
*x
y->a
&varName
*(type
sizeof(arrayType)
*)v
A pointer is a variable that holds a memory location.
Arrays
sizeof(array) /
sizeof(array[0])
Strings
'A' character
Single quotes.
"AB" string
Double quotes.
\0
Null terminator.
Strings are char arrays.
Declaring
type name[int];
y, z};
elements.
is equivalent to
char name[4] = {'A', 's', 'h', '\0'};
int i; for(i = 0; name[i]; i++){}
elements to x.
\0 evaluates as false.
z};
elements.
Size cannot be changed after declaration.
Dimensions
name[int]
name[int][int]
Accessing
name[int]
alarm (bell/beep)
\b
backspace
\f
formfeed
\n
newline
\r
carriage return
\t
horizontal tab
\v
vertical tab
\\
backslash
\'
single quote
\"
double quote
\?
question mark
\nnn
\xhh
By Ashlyn Black
Sponsored by Readability-Score.com
cheatography.com/ashlyn-black/
Page 4 of 13.
https://readability-score.com
Functions (cont)
Declaring
&x; }
Passing by pointer allows you to change the originating variable within the
also be unique.
type/void
funcName()
args...
{}
return var;
function.
Scope
int f(){ int i = 0; } i++;
i is declared inside f(), it doesn't exist outside that function.
Prototyping
type funcName(args...);
return.
main.)
By Value vs By Pointer
void f(type
type
x); f(y);
value.)
funcName([args...])
respective function.
void f(type
*x);
pointer.)
main()
f(array);
void f(type
*x);
pointer.)
Anatomy
f(structure);
void f(type
*x); f( &y);
pointer.)
type f(){
Returning by value.
int main
int argc
char *argv[]
return x; }
type f(){ type
return int;
x; return &x;
}
app two 3
By Ashlyn Black
Sponsored by Readability-Score.com
cheatography.com/ashlyn-black/
Page 5 of 13.
https://readability-score.com
do
Loop keyword.
if(a) b;
Evaluates b if a is true.
{}
Loop delimiters.
if(a){ b; c; }
c++;
Loop contents.
if(a){ b; }else{ c; }
Evaluates b if a is true, c
while();
otherwise.
if(a){ b; }else if(c){ d;
Evaluates b if a is true,
c != 'Z'
}else{ e; }
otherwise d if c is true,
for
otherwise e.
Test condition.
OR
switch(a){ case b: c; }
Evaluates c if a equals b.
switch(a){ default: b; }
Evaluates b if a matches no
other case.
Declares integer i.
or c.
for()
Loop keyword.
i = 0;
d: e; default: f; }
b, e and f if a equals d,
n[i] !=
otherwise f.
'\0';
Evaluates c if a equals b, e if a
i++
Increments i. No semicolon.
{}
Loop delimiters.
d; }
continue
int i=0; while(i<10){ i++; continue; i--;}
Iterative (Looping)
Skips rest of loop contents and restarts at the beginning of the loop.
while
int x = 0; while(x < 10){ x += 2; }
break
int i=0; while(1){ if(x==10){break;} i++; }
int x = 0;
while()
x < 10
Test condition.
{}
Loop delimiters.
Characters
x += 2;
Loop contents.
getchar()
Console Input/Output
#include <stdio.h>
do while
char c = 'A'; do { c++; } while(c != 'Z');
Always runs through loop at least once.
char c = 'A';
putchar(int)
Strings
By Ashlyn Black
Sponsored by Readability-Score.com
cheatography.com/ashlyn-black/
Page 6 of 13.
https://readability-score.com
File Input/Output
Alternative
fgets(strName,
length, stdin);
variable. (Safe)
puts("string")
#include <stdio.h>
Opening
FILE *fptr = fopen(filename, mode);
FILE *fptr
fopen()
Formatted Data
filename
mode
Modes
printf("I love
"r" / "rb"
%c %d!", 'C',
"w" / "wb"
"a" / "ab"
"r+" / "r+b" /
scanf("%d", &x)
99)
Alternative
fgets(strName,
length, stdin);
sscanf(strName,
scanf. (safe)
"rb+"
"w+" / "w+b" /
"wb+"
"%d", &x);
The stream buffers must be flushed to reflect changes. String terminator
characters can flush the output while newline characters can flush the
input.
"a+" / "a+b" /
"ab+"
file.
Closing
Safe functions are those that let you specify the length of the input. Unsafe
fclose(fptr);
By Ashlyn Black
Sponsored by Readability-Score.com
cheatography.com/ashlyn-black/
Page 7 of 13.
https://readability-score.com
fseek(fptr,
fgets(strName,
offset, origin);
length, fptr);
sscanf(strName, "%d",
Origins
&x);
SEEK_SET
Beginning of file.
Binary
SEEK_CUR
fread(void *ptr,
End of file.
sizeof(element),
SEEK_END
number, fptr)
Utilities
feof(fptr)
rename(strOldName,
Renames a file.
fwrite(void *ptr,
sizeof(element),
number, fptr)
strNewName)
remove(strName)
Safe functions are those that let you specify the length of the input.
Deletes a file.
Characters
fgetc(fptr)
unsuccessful. (safe)
fputc(int c, fptr)
Type
%d or %i
Strings
Example
-42
Description
Signed decimal integer.
%u
42
n, fptr)
%o
52
fputs(char *s,
%x or %X
2a or 2A
Unsigned hexadecimal
fptr)
Formatted Data
fscanf(fptr,
format, [...])
parameter. (unsafe)
fprintf(fptr,
format, [...])
parameter.
integer.
%f or %F
1.21
%e or %E
1.21e+9 or 1.21E+9
%g or %G
1.21e+9 or 1.21E+9
Shortest representation of
%f/%F or %e/%E.
%a or %A
Alternative
0x1.207c8ap+30 or
0X1.207C8AP+30
%c
%s
A String.
A character.
A character string.
By Ashlyn Black
Sponsored by Readability-Score.com
cheatography.com/ashlyn-black/
Page 8 of 13.
https://readability-score.com
A pointer.
.*
A percent character.
Length
hh
ll
space
Width
integer
Preprocessor Directives
#include
<inbuilt.h>
file.
#include
"./custom.h"
#define NAME
value
Precision
.integer
Minimum # of digits to print for %d, %i, %o, %u, %x, %X. Left
Comments
break
float
signed
_Alignof
case
for
sizeof
_Atomic
char
goto
static
_Bool
const
if
struct
_Complex
continue
inline
switch
_Generic
default
int
typedef
_Imaginary
do
long
union
By Ashlyn Black
Sponsored by Readability-Score.com
cheatography.com/ashlyn-black/
Page 9 of 13.
https://readability-score.com
Heap Space
_Noreturn
double
register
unsigned
#include <stdlib.h>
_Static_assert
else
restrict
void
Allocating
_Thread_local
enum
return
volatile
malloc();
auto
extern
short
while
_A-Z...
__...
type *x; x =
malloc(sizeof(type));
type *y; y =
E[A-Z]...
is[a-z]...
to[a-z]...
LC_[A-Z]...
SIG[A-Z]...
SIG_[A-Z]...
str[a-z]...
mem[a-z]...
wcs[a-z]...
..._t
malloc(sizeof(type) *
length );
Memory for a structure.
type));
Deallocating
free(ptrName);
Name
Reserved By Library
d_...
dirent.h
Reallocating
l_...
fcntl.h
realloc(ptrName, size);
F_...
fcntl.h
O_...
fcntl.h
S_...
fcntl.h
gr_...
grp.h
..._MAX
limits.h
pw_...
pwd.h
sa_...
signal.h
SA_...
signal.h
Randomicity
st_...
sys/stat.h
rand()
S_...
sys/stat.h
tms_...
sys/times.h
c_...
termios.h
V...
termios.h
I...
termios.h
O...
termios.h
TC...
termios.h
B[0-9]...
termios.h
to ptrName.
The memory addresses you see are from virtual memory the operating
system assigns to the program; they are not physical addresses.
Referencing memory that isn't assigned to the program will produce an
OS segmentation fault.
The Standard Library
#include <stdlib.h>
RAND_MAX
srand(unsigned
integer);
(unsigned)
time(NULL)
every second.
Sponsored by Readability-Score.com
cheatography.com/ashlyn-black/
Page 10 of 13.
https://readability-score.com
Sorting
isblank
array
Array/string name.
length
sizeof(type)
compFunc
strcpy(a, b)
compFunc
int compFunc( const void *a, const void b* ){ return(
strcat(a, b)
of \0 in string a. (unsafe)
int compFunc()
integer.
strcmp(a, b)
const void *b
otherwise.
- *(int *)b);
strstr(a, b)
strncat(a, b, n)
#include <ctype.h>
tolower(char)
Lowercase char.
toupper(char)
Uppercase char.
isalpha(char)
otherwise.
True if char is a lowercase letter of the alphabet,
Safe functions are those that let you specify the length of the input.
Unsafe functions do not, and carry the risk of memory overflow.
false otherwise.
isupper(char)
islower(char)
isnumber(char)
By Ashlyn Black
Sponsored by Readability-Score.com
cheatography.com/ashlyn-black/
Page 11 of 13.
https://readability-score.com
Variable Types
a--
(type)a
Typecasts a as type.
time_t
&a;
Memory location of a.
struct tm *x;
sizeof(a)
tm structure members:
Binary Operators
int tm_sec
Seconds, 0 to 59.
int tm_min
Minutes, 0 to 59.
int tm_hour
Hours, 0 to 23.
a * b;
Product of a and b. (a b)
int tm_mday
a / b;
int tm_mon
Month, 0 to 11.
int tm_year
int tm_wday
int tm_yday
int tm_isdst
Functions
Returns unix epoch time (seconds since
time(NULL)
1/Jan/1970.)
time(&time_t);
zero. (a b)
a % b;
a + b;
Sum of a and b.
a - b;
Difference of a and b.
a << b;
a >> b;
a < b;
a <= b;
variable.
ctime(&time_t)
x = localtime(
&time_t);
members.
Unary Operators
a >= b;
a == b;
a != b;
a & b;
-a
a ^ b;
!a
~a
++a
Increment of a by 1. (a = a + 1)
--a
Decrement of a by 1. (a = a - 1)
a++
By Ashlyn Black
Sponsored by Readability-Score.com
cheatography.com/ashlyn-black/
Page 12 of 13.
https://readability-score.com
a | b;
ashlynblack.com
a && b;
a || b;
x = a;
Assigns value of a to x.
a *= b;
a /= b;
a %= b;
a += b;
a -= b;
a <<= b;
a >>= b;
a &= b;
a ^= b;
a |= b;
By Ashlyn Black
Sponsored by Readability-Score.com
cheatography.com/ashlyn-black/
Page 13 of 13.
https://readability-score.com