Number Literals
Integers | |||
| binary |
| binary |
| octal |
| decimal |
| hexadecimal |
| hexadecimal |
Real Numbers | |||
/
| |||
single precision float (
suffix ) | |||
/
| |||
double precision float ( no
suffix ) | |||
Signage | |||
/
| positive |
| negative |
Binary notation
0b...
/
0B...
is available on GCC and most but not all C compilers.
Variables
Declaring | |
| A variable. |
| A variable & initialising it. |
| Multiple variables of the same type. |
| A constant variable: can't assign to after declaration (compiler enforced.) |
Naming | |
| Alphanumeric, not a keyword, begins with a letter. |
| Doesn't begin with a letter. |
| Reserved keyword. |
| Non-alphanumeric. |
| |
Longer than 31 characters (C89 & C90 only) |
Constants are
CAPITALISED
. Function names usually take the form of a verb eg.
plotRobotUprising()
.
Primitive Variable Types
*applicable but not limited to most ARM, AVR, x86 & x64 installations | ||
| ||
by ascending arithmetic conversion | ||
Integers | ||
Type | Bytes | Value Range |
| 1 | OR
|
| 1 | 0 to 28-1 |
| 1 | -27 to 27-1 |
| 2 / 4 | OR
|
| 2 / 4 | 0 to 216-1 OR 231-1 |
| 2 / 4 | -215 to 215-1 OR -231 to 232-1 |
| 2 | OR
|
| 2 | 0 to 216-1 |
| 2 | -215 to 215-1 |
| 4 / 8 | OR
|
| 4 / 8 | 0 to 232-1 OR 264-1 |
| 4 / 8 | -231 to 231-1 OR -263 to 263-1 |
| 8 | OR
|
| 8 | 0 to 264-1 |
| 8 | -263 to 263-1 |
Floats | ||
Type | Bytes | Value Range (Normalized) |
| 4 | ±1.2×10-38 to ±3.4×1038 |
| 8 / 4 | ±2.3×10-308 to ±1.7×10308 OR alias to
for AVR. |
| ARM: 8, AVR: 4, x86: 10, x64: 16 | |
Qualifiers | ||
| Flags variable as read-only (compiler can optimise.) | |
| Flags variable as unpredictable (compiler cannot optimise.) | |
Storage Classes | ||
| Quick access required. May be stored in RAM OR a register. Maximum size is register size. | |
| Retained when out of scope.
global variables are confined to the scope of the compiled object file they were declared in. | |
| Variable is declared by another file. | |
Typecasting | ||
| Returns
as data
. | |
|
Some types (denoted with OR) are architecture dependant.
There is no primitive boolean type, only zero (false,
0
) and non-zero (true, usually
1
.)
Extended Variable Types
| ||
by ascending arithmetic conversion | ||
From the | ||
Type | Bytes | Value Range |
| 1 | -27 to 27-1 |
| 1 | 0 to 28-1 |
| 2 | -215 to 215-1 |
| 2 | 0 to 216-1 |
| 4 | -231 to 231-1 |
| 4 | 0 to 232-1 |
| 8 | -263 to 263-1 |
| 8 | 0 to 264-1 |
From the | ||
Type | Bytes | Value Range |
| 1 |
/
or
/
|
The
stdint.h
library was introduced in C99 to give integer types architecture-independent lengths.
Structures
Defining | |
| A structure type
with two members,
and
. Note trailing semicolon |
| A structure with a recursive structure pointer inside. Useful for linked lists. |
Declaring | |
| A variable
as structure type
. |
| A
structure type pointer,
. |
| Shorthand for defining
and declaring
as that structure type. |
| A variable
as structure type
and initialising its members. |
Accessing | |
| Member
of structure
. |
| Value of structure pointer
member
. |
Bit Fields | |
| Declares
with two members
and
, both four bits in size (0 to 15.) |
Array members can't be assigned bit fields. |
Type Definitions
Defining | |
| Abbreviating a longer type name to
. |
| Creating a
from a structure. |
| Creating an enumerated
type. |
Declaring | |
| Variable
as type
. |
| Structure
as type
. |
Unions
Defining | |
| A union type
with two members,
&
. Size is same as biggest member size. |
Declaring | |
| A variable
as union type
. |
Accessing | |
| Members cannot store values concurrently. Setting
will corrupt
. |
Unions are used for storing multiple data types in the same area of memory.
Enumeration
Defining | |
| A custom data type
with two possible states:
or
. |
Declaring | |
| A variable
of data type
. |
Assigning | |
| Variable
can only be assigned values of either
or
. |
Evaluating | |
| Testing the value of
. |
Pointers
Declaring | |
| Pointers have a data
like normal variables. |
| They can also have an incomplete type. Operators other than assignment cannot be applied as the length of the type is unknown. |
| A data structure pointer. |
| An array/string name can be used as a pointer to the first array element. |
Accessing | |
| A memory address. |
| Value stored at that address. |
| Value stored in structure pointer
member
. |
| Memory address of normal variable
. |
| Dereferencing a
pointer as a
pointer. |
A pointer is a variable that holds a memory location.
Arrays
Declaring | |
| You set array length. |
| You set array length and initialise elements. |
| You set array length and initialise all elements to
. |
| Compiler sets array length based on initial elements. |
Size cannot be changed after declaration. | |
Dimensions | |
| One dimension array. |
| Two dimensional array. |
Accessing | |
| Value of element
in array
. |
| Same as
. |
Elements are contiguously numbered ascending from | |
| Memory address of element
in array
. |
| Same as
. |
Elements are stored in contiguous memory. | |
Measuring | |
| Returns length of
. (Unsafe) |
| Returns length of
. (Safe) |
Strings
character | Single quotes. |
string | Double quotes. |
| Null terminator. |
Strings are
arrays. | |
| |
is equivalent to | |
| |
| |
evaluates as false. |
Strings must include a
char
element for
\0
.
Escape Characters
| alarm (bell/beep) |
| backspace |
| formfeed |
| newline |
| carriage return |
| horizontal tab |
| vertical tab |
| backslash |
| single quote |
| double quote |
| question mark | ||
| Any octal ANSI character code. | ||
| Any hexadecimal ANSI character code. |
Functions
Declaring | |
| |
Function names follow the same restrictions as variable names but must also be unique. | |
| Return value type (
if none.) |
| Function name and argument parenthesis. |
| Argument types & names (
if none.) |
| Function content delimiters. |
| Value to return to function call origin. Skip for
type functions. Functions exit immediately after a
. |
By Value vs By Pointer | |
| Passing variable
to function
argument
(by value.) |
| Passing an array/string to function
argument
(by pointer.) |
| Passing a structure to function
argument
(by pointer.) |
| Passing variable
to function
argument
(by pointer.) |
| Returning by value. |
| Returning a variable by pointer. |
| Returning an array/string/structure by pointer. The
qualifier is necessary otherwise
won't exist after the function exits. |
Passing by pointer allows you to change the originating variable within the function. | |
Scope | |
| |
| |
Prototyping | |
| |
Place before declaring or referencing respective function (usually before | |
| Same
,
and
as respective function. |
| Semicolon instead of function delimiters. |
main()
| |
Anatomy | |
| Program entry point. |
| # of command line arguments. |
| Command line arguments in an array of strings. #1 is always the program filename. |
| Exit status (
) returned to the OS upon program exit. |
Command Line Arguments | |
| Three arguments,
,
and
. |
| Two arguments,
and
. |
main
is the first function called when the program executes.
Conditional (Branching)
if, else if, else | |
| Evaluates
if
is true. |
| Evaluates
and
if
is true. |
| Evaluates
if
is true,
otherwise. |
| Evaluates
if
is true, otherwise
if
is true, otherwise
. |
switch, case, break | |
| Evaluates
if
equals
. |
| Evaluates
if
matches no other case. |
| Evaluates
if
equals either
or
. |
| Evaluates
,
and
if
equals
,
and
if
equals
, otherwise
. |
| Evaluates
if
equals
,
if
equals
and
otherwise. |
Iterative (Looping)
while | |
| |
Loop skipped if test condition initially false. | |
| Declare and initialise integer
. |
| Loop keyword and condition parenthesis. |
| Test condition. |
| Loop delimiters. |
| Loop contents. |
do while | |
| |
Always runs through loop at least once. | |
| Declare and initialise character
. |
| Loop keyword. |
| Loop delimiters. |
| Loop contents. |
| Loop keyword and condition parenthesis. Note semicolon. |
| Test condition. |
for | |
(C89) | |
OR | |
(C99+) | |
Compact increment/decrement based loop. | |
| Declares integer
. |
| Loop keyword. |
| Initialises integer
. Semicolon. |
| Test condition. Semicolon. |
| Increments
. No semicolon. |
| Loop delimiters. |
continue | |
| |
Skips rest of loop contents and restarts at the beginning of the loop. | |
break | |
| |
Skips rest of loop contents and exits loop. |
Console Input/Output
| |
Characters | |
| Returns a single character's ANSI code from the input stream buffer as an integer. (safe) |
| Prints a single character from an ANSI code integer to the output stream buffer. |
Strings | |
| Reads a line from the input stream into a string variable. (Unsafe, removed in C11.) |
Alternative | |
| Reads a line from the input stream into a string variable. (Safe) |
| Prints a string to the output stream. |
Formatted Data | |
| Read value/s (type defined by format string) into variable/s (type must match) from the input stream. Stops reading at the first whitespace. |
| Prints data (formats defined by the format string) as a string to the output stream. |
Alternative | |
| Uses
to limit the input length, then uses
to read the resulting string in place of
. (safe) |
The stream buffers must be flushed to reflect changes. String terminator characters can flush the output while newline characters can flush the input.
Safe functions are those that let you specify the length of the input. Unsafe functions do not, and carry the risk of memory overflow.
File Input/Output
| |
Opening | |
| |
| Declares
as a FILE type pointer (stores stream location instead of memory location.) |
| Returns a stream location pointer if successful,
otherwise. |
| String containing file's directory path & name. |
| String specifying the file access mode. |
Modes | |
/
| Read existing text/binary file. |
/
| Write new/over existing text/binary file. |
/
| Write new/append to existing text/binary file. |
/
/
| Read and write existing text/binary file. |
/
/
| Read and write new/over existing text/binary file. |
/
/
| Read and write new/append to existing text/binary file. |
Closing | |
| Flushes buffers and closes stream. Returns
if successful,
otherwise. |
Random Access | |
| Return current file position as a long integer. |
| Sets current file position. Returns false is successful, true otherwise. The
is a long integer type. |
Origins | |
| Beginning of file. |
| Current position in file. |
| End of file. |
Utilities | |
| Tests end-of-file indicator. |
| Renames a file. |
| Deletes a file. |
Characters | |
| Returns character read or EOF if unsuccessful. (safe) |
| Returns character written or EOF if unsuccessful. |
Strings | |
| Reads
characters from file
into string
. Stops at
and
. (safe) |
| Writes string
to file
. Returns non-negative on success,
otherwise. |
Formatted Data | |
| Same as
with additional file pointer parameter. (unsafe) |
| Same as
with additional file pointer parameter. |
Alternative | |
| Uses
to limit the input length, then uses
to read the resulting string in place of
. (safe) |
Binary | |
| Reads a
of
s from
to array
. (safe) |
| Writes a
of
s to file
from array
. |
Safe functions are those that let you specify the length of the input. Unsafe functions do not, and carry the risk of memory overflow.
Placeholder Types (f/printf And f/scanf)
| ||
Type | Example | Description |
or
|
| Signed decimal integer. |
|
| Unsigned decimal integer. |
|
| Unsigned octal integer. |
or
|
| Unsigned hexadecimal integer. |
or
|
| Signed decimal float. |
or
|
or
| Signed decimal w/ scientific notation. |
or
|
or
| Shortest representation of
/
or
/
. |
or
|
or
| Signed hexadecimal float. |
|
| A character. |
|
| A character string. |
| A pointer. | |
|
| A percent character. |
| No output, saves # of characters printed so far. Respective printf argument must be an integer pointer. |
The pointer format is architecture and implementation dependant.
Placeholder Formatting (f/printf And f/scanf)
| |
Flags | |
| Left justify instead of default right justify. |
| Sign for both positive numbers and negative. |
| Precede with
,
or
for
,
and
tokens. |
| Left pad with spaces. |
| Left pad with zeroes. |
Width | |
| Minimum number of characters to print: invokes padding if necessary. Will not truncate. |
| Width specified by a preceding argument in
. |
Precision | |
| Minimum # of digits to print for
,
,
,
,
,
. Left pads with zeroes. Will not truncate. Skips values of 0. |
Minimum # of digits to print after decimal point for
,
,
,
,
,
(default of 6.) | |
Minimum # of significant digits to print for
&
. | |
Maximum # of characters to print from
(a string.) | |
| If no
is given, default of 0. |
| Precision specified by a preceding argument in
. |
Length | |
| Display a
as
. |
| Display a
as
. |
| Display a
integer. |
| Display a
integer. |
| Display a
float. |
| Display a
integer. |
| Display a
integer. |
| Display a
integer. |
Preprocessor Directives
| Replaces line with contents of a standard C header file. |
| Replaces line with contents of a custom header file. Note dir path prefix & quotations. |
| Replaces all occurrences of
with
. |
Comments
// We're single-line comments!// Nothing compiled after // on these lines./* I'm a multi-line comment! Nothing compiled betweenthese delimiters. */
C Reserved Keywords
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
C / POSIX Reserved Keywords
|
|
|
|
|
|
|
|
|
|
| |
Header Reserved Keywords
Name | Reserved By Library |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Heap Space
| |
Allocating | |
| Returns a memory location if successful,
otherwise. |
| Memory for a variable. |
| Memory for an array/string. |
| Memory for a structure. |
Deallocating | |
| Removes the memory allocated to
. |
Reallocating | |
| Attempts to resize the memory block assigned to
. |
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
| |
Randomicity | |
| Returns a (predictable) random integer between 0 and RAND_MAX based on the randomiser seed. |
| The maximum value
can generate. |
| Seeds the randomiser with a positive integer. |
| Returns the computer's tick-tock value. Updates every second. |
Sorting | |
| |
| Sort using the QuickSort algorithm. |
| Array/string name. |
| Length of the array/string. |
| Byte size of each element. |
| Comparison function name. |
compFunc | |
| |
| Function name unimportant but must return an integer. |
| Argument names unimportant but must identical otherwise. |
| Negative result swaps
for
, positive result swaps
for
, a result of
doesn't swap. |
C's inbuilt randomiser is cryptographically insecure: DO NOT use it for security applications.
The Character Type Library
| |
| Lowercase
. |
| Uppercase
. |
| True if
is a letter of the alphabet, false otherwise. |
| True if
is a lowercase letter of the alphabet, false otherwise. |
| True if
is an uppercase letter of the alphabet, false otherwise. |
| True if
is numerical (
to
) and false otherwise. |
| True if
is a whitespace character (
) and false otherwise. |
The String Library
| |
| Returns # of
in string
as an integer. Excludes
. (unsafe) |
| Copies strings. Copies string
over string
up to and including
. (unsafe) |
| Concatenates strings. Copies string
over string
up to and including
, starting at the position of
in string
. (unsafe) |
| Compares strings. Returns false if string
equals string
, true otherwise. Ignores characters after
. (unsafe) |
| Searches for string
inside string
. Returns a pointer if successful,
otherwise. (unsafe) |
Alternatives | |
| Copies strings. Copies
characters from string
over string
up to and including
. (safe) |
| Concatenates strings. Copies
characters from string
over string
up to and including
, starting at the position of
in string
. (safe) |
| Compares first
characters of two strings. Returns false if string
equals string
, true otherwise. Ignores characters after
. (safe) |
Safe functions are those that let you specify the length of the input. Unsafe functions do not, and carry the risk of memory overflow.
The Time Library
| |
Variable Types | |
| Stores the calendar time. |
| Stores a time & date breakdown. |
tm structure members: | |
| Seconds, 0 to 59. |
| Minutes, 0 to 59. |
| Hours, 0 to 23. |
| Day of the month, 1 to 31. |
| Month, 0 to 11. |
| Years since 1900. |
| Day of the week, 0 to 6. |
| Day of the year, 0 to 365. |
| Daylight saving time. |
Functions | |
| Returns unix epoch time (seconds since 1/Jan/1970.) |
| Stores the current time in a
variable. |
| Returns a
variable as a string. |
| Breaks
down into
members. |
Unary Operators
by descending evaluation precedence | |
| Sum of
(zero) and
. (0 + a) |
| Difference of
(zero) and
. (0 - a) |
| Complement (logical NOT) of
. (~a) |
| Binary ones complement (bitwise NOT) of
. (~a) |
| Increment of
by
. (a = a + 1) |
| Decrement of
by
. (a = a - 1) |
| Returns
then increments
by
. (a = a + 1) |
| Returns
then decrements
by
. (a = a - 1) |
| Typecasts
as
. |
| Memory location of
. |
| Memory size of
(or
) in bytes. |
Binary Operators
by descending evaluation precedence | |
| Product of
and
. (a × b) |
| Quotient of dividend
and divisor
. Ensure divisor is non-zero. (a ÷ b) |
| Remainder of integers dividend
and divisor
. |
| Sum of
and
. |
| Difference of
and
. |
| Left bitwise shift of
by
places. (a × 2b) |
| Right bitwise shift of
by
places. (a × 2-b) |
| Less than. True if
is less than
and false otherwise. |
| Less than or equal to. True if
is less than or equal to
and false otherwise. (a ≤ b) |
| Greater than. True if
is greater than than
and false otherwise. |
| Greater than or equal to. True if
is greater than or equal to
and false otherwise. (a ≥ b) |
| Equality. True if
is equal to
and false otherwise. (a ⇔ b) |
| Inequality. True if
is not equal to
and false otherwise. (a ≠ b) |
| Bitwise AND of
and
. (a ⋂ b) |
| Bitwise exclusive-OR of
and
. (a ⊕ b) |
| Bitwise inclusive-OR of
and
. (a ⋃ b) |
| Logical AND. True if both
and
are non-zero. (Logical AND) (a ⋂ b) |
| Logical OR. True if either
or
are non-zero. (Logical OR) (a ⋃ b) |
Ternary & Assignment Operators
by descending evaluation precedence | |
| Evaluates
if
evaluates as true or
otherwise. (if(x){ a; } else { b; }) |
| Assigns value of
to
. |
| Assigns product of
and
to
. (a = a × b) |
| Assigns quotient of dividend
and divisor
to
. (a = a ÷ b) |
| Assigns remainder of integers dividend
and divisor
to
. (a = a mod b) |
| Assigns sum of
and
to
. (a = a + b) |
| Assigns difference of
and
to
. (a = a - b) |
| Assigns left bitwise shift of
by
places to
. (a = a × 2b) |
| Assigns right bitwise shift of
by
places to
. (a = a × 2-b) |
| Assigns bitwise AND of
and
to
. (a = a ⋂ b) |
| Assigns bitwise exclusive-OR of
and
to
. (a = a ⊕ b) |
| Assigns bitwise inclusive-OR of
and
to
. (a = a ⋃ b) |