Number Literals
Integers |
|||
|
binary |
|
binary |
|
octal |
|
decimal |
|
hexadecimal |
|
hexadecimal |
Real Numbers |
|||
|
|||
single precision float ( |
|||
|
|||
double precision float ( no |
|||
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) |
Primitive Variable Types
*applicable but not limited to most ARM, AVR, x86 & x64 installations |
||
|
||
by ascending arithmetic conversion |
||
Integers |
||
Type |
Bytes |
Value Range |
|
1 |
|
|
1 |
0 to 2 8-1 |
|
1 |
-2 7 to 2 7-1 |
|
2 / 4 |
|
|
2 / 4 |
0 to 2 16-1 OR 2 31-1 |
|
2 / 4 |
-2 15 to 2 15-1 OR -2 31 to 2 32-1 |
|
2 |
|
|
2 |
0 to 2 16-1 |
|
2 |
-2 15 to 2 15-1 |
|
4 / 8 |
|
|
4 / 8 |
0 to 2 32-1 OR 2 64-1 |
|
4 / 8 |
-2 31 to 2 31-1 OR -2 63 to 2 63-1 |
|
8 |
|
|
8 |
0 to 2 64-1 |
|
8 |
-2 63 to 2 63-1 |
Floats |
||
Type |
Bytes |
Value Range (Normalized) |
|
4 |
±1.2×10 -38 to ±3.4×10 38 |
|
8 / 4 |
±2.3×10 -308 to ±1.7×10 308 OR alias to |
|
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. |
|
|
Variable is declared by another file. |
|
Typecasting |
||
|
Returns |
|
|
Extended Variable Types
|
||
by ascending arithmetic conversion |
||
From the |
||
Type |
Bytes |
Value Range |
|
1 |
-2 7 to 2 7-1 |
|
1 |
0 to 2 8-1 |
|
2 |
-2 15 to 2 15-1 |
|
2 |
0 to 2 16-1 |
|
4 |
-2 31 to 2 31-1 |
|
4 |
0 to 2 32-1 |
|
8 |
-2 63 to 2 63-1 |
|
8 |
0 to 2 64-1 |
From the |
||
Type |
Bytes |
Value Range |
|
1 |
|
Structures
Defining
|
|
struct strctName{ type x; type y; };
|
A structure type
strctName with two members,
x and
y .
Note trailing semicolon
|
struct item{ struct item *next; };
|
A structure with a recursive structure pointer inside. Useful for linked lists.
|
Declaring
|
|
struct strctName varName;
|
A variable
varName as structure type
strctName .
|
struct strctName *ptrName;
|
A
strctName structure type pointer,
ptrName .
|
struct strctName{ type a; type b; } varName;
|
Shorthand for defining
strctName and declaring
varName as that structure type.
|
struct strctName varName = { a, b };
|
A variable
varName as structure type
strctName and initialising its members.
|
Accessing
|
|
varName.x
|
Member
x of structure
varName .
|
ptrName->x
|
Value of structure pointer
ptrName member
x .
|
Bit Fields
|
|
struct{char a:4, b:4} x;
|
Declares
x with two members
a and
b , both four bits in size (0 to 15.)
|
Array members can't be assigned bit fields.
|
类型定义
Defining |
|
|
Abbreviating a longer type name to |
|
Creating a |
|
Creating an enumerated |
Declaring |
|
|
Variable |
|
Structure |
Unions
Defining |
|
|
A union type |
Declaring |
|
|
A variable |
Accessing |
|
|
Members cannot store values concurrently. Setting |
Enumeration
Defining |
|
|
A custom data type |
Declaring |
|
|
A variable |
Assigning |
|
|
Variable |
Evaluating |
|
|
Testing the value of |
Pointers
Declaring |
|
|
Pointers have a data |
|
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 |
|
Memory address of normal variable |
|
Dereferencing a |
Arrays
Declaring
|
|
type name[int];
|
You set array length.
|
type name[int] = {x, y, z};
|
You set array length and initialise elements.
|
type name[int] = {x};
|
You set array length and initialise all elements to
x .
|
type name[] = {x, y, z};
|
Compiler sets array length based on initial elements.
|
Size cannot be changed after declaration.
|
|
Dimensions
|
|
name[int]
|
One dimension array.
|
name[int][int]
|
Two dimensional array.
|
Accessing
|
|
name[int]
|
Value of element
int in array
name .
|
*(name + int)
|
Same as
name[int] .
|
Elements are contiguously numbered ascending from 0 .
|
|
&name[int]
|
Memory address of element
int in array
name .
|
name + int
|
Same as
&name[int] .
|
Elements are stored in contiguous memory.
|
|
Measuring
|
|
sizeof(array) / sizeof(arrayType)
|
Returns length of
array .
(Unsafe)
|
sizeof(array) / sizeof(array[0])
|
Returns length of
array .
(Safe)
|
Strings
|
Single quotes. |
|
Double quotes. |
|
Null terminator. |
Strings are |
|
|
|
is equivalent to |
|
|
|
|
|
|
Escape Characters
\a
|
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
|
Any octal ANSI character code.
|
||
\xhh
|
Any hexadecimal ANSI character code.
|
Functions
Declaring
|
|
type/void funcName([args...]){ [return var;] }
|
|
Function names follow the same restrictions as variable names but must also be unique.
|
|
type/void
|
Return value type (
void if none.)
|
funcName()
|
Function name and argument parenthesis.
|
args...
|
Argument types & names (
void if none.)
|
{}
|
Function content delimiters.
|
return var;
|
Value to return to function call origin. Skip for
void type functions. Functions exit immediately after a
return .
|
By Value vs By Pointer
|
|
void f(type x); f(y);
|
Passing variable
y to function
f argument
x (by value.)
|
void f(type *x); f(array);
|
Passing an array/string to function
f argument
x (by pointer.)
|
void f(type *x); f(structure);
|
Passing a structure to function
f argument
x (by pointer.)
|
void f(type *x); f(&y);
|
Passing variable
y to function
f argument
x (by pointer.)
|
type f(){ return x; }
|
Returning by value.
|
type f(){ type x; return &x; }
|
Returning a variable by pointer.
|
type f(){ static type x[]; return &x; }
|
Returning an array/string/structure by pointer. The
static qualifier is necessary otherwise
x won't exist after the function exits.
|
Passing by pointer allows you to change the originating variable within the function.
|
|
Scope
|
|
int f(){ int i = 0; }
|
|
i is declared inside f() , it doesn't exist outside that function.
|
|
Prototyping
|
|
type funcName(args...);
|
|
Place before declaring or referencing respective function (usually before main .)
|
|
type funcName([args...])
|
Same
type ,
name and
args... 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 ( |
Command Line Arguments |
|
|
Three arguments, |
|
Two arguments, |
Conditional (Branching)
if, else if, else
|
|
if(a) b;
|
Evaluates
b if
a is true.
|
if(a){ b; c; }
|
Evaluates
b and
c if
a is true.
|
if(a){ b; }else{ c; }
|
Evaluates
b if
a is true,
c otherwise.
|
if(a){ b; }else if(c){ d; }else{ e; }
|
Evaluates
b if
a is true, otherwise
d if
c is true, otherwise
e .
|
switch, case, break
|
|
switch(a){ case b: c; }
|
Evaluates
c if
a equals
b .
|
switch(a){ default: b; }
|
Evaluates
b if
a matches no other case.
|
switch(a){ case b: case c: d; }
|
Evaluates
d if
a equals either
b or
c .
|
switch(a){ case b: c; case d: e; default: f; }
|
Evaluates
c ,
e and
f if
a equals
b ,
e and
f if
a equals
d , otherwise
f .
|
switch(a){ case b: c; break; case d: e; break; default: f; }
|
Evaluates
c if
a equals
b ,
e if
a equals
d and
e otherwise.
|
Iterative (Looping)
while
|
|
int x = 0; while(x < 10){ x += 2; }
|
|
Loop skipped if test condition initially false.
|
|
int x = 0;
|
Declare and initialise integer
x .
|
while()
|
Loop keyword and condition parenthesis.
|
x < 10
|
Test condition.
|
{}
|
Loop delimiters.
|
x += 2;
|
Loop contents.
|
do while
|
|
char c = 'A'; do { c++; } while(c != 'Z');
|
|
Always runs through loop at least once.
|
|
char c = 'A';
|
Declare and initialise character
c .
|
do
|
Loop keyword.
|
{}
|
Loop delimiters.
|
c++;
|
Loop contents.
|
while();
|
Loop keyword and condition parenthesis.
Note semicolon.
|
c != 'Z'
|
Test condition.
|
for
|
|
int i; for(i = 0; n[i] != '\0'; i++){} (C89)
|
|
OR
|
|
for(int i = 0; n[i] != '\0'; i++){} (C99+)
|
|
Compact increment/decrement based loop.
|
|
int i;
|
Declares integer
i .
|
for()
|
Loop keyword.
|
i = 0;
|
Initialises integer
i .
Semicolon.
|
n[i] != '\0';
|
Test condition.
Semicolon.
|
i++
|
Increments
i .
No semicolon.
|
{}
|
Loop delimiters.
|
continue
|
|
int i=0; while(i<10){ i++; continue; i--;}
|
|
Skips rest of loop contents and restarts at the beginning of the loop.
|
|
break
|
|
int i=0; while(1){ if(x==10){break;} i++; }
|
|
Skips rest of loop contents and exits loop.
|
控制台输入/输出
|
|
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 |
文件输入/输出
|
|
Opening |
|
|
|
|
Declares |
|
Returns a stream location pointer if successful, |
|
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 |
Random Access |
|
|
Return current file position as a long integer. |
|
Sets current file position. Returns false is successful, true otherwise. The |
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 |
|
Writes string |
Formatted Data |
|
|
Same as |
|
Same as |
Alternative |
|
|
Uses |
Binary |
|
|
Reads a |
|
Writes a |
Placeholder Types (f/printf And f/scanf)
|
||
Type |
Example |
Description |
|
|
Signed decimal integer. |
|
|
Unsigned decimal integer. |
|
|
Unsigned octal integer. |
|
|
Unsigned hexadecimal integer. |
|
|
Signed decimal float. |
|
|
Signed decimal w/ scientific notation. |
|
|
Shortest representation of |
|
|
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. |
Placeholder Formatting (f/printf And f/scanf)
%[Flags][Width][.Precision][Length]Type
|
|
Flags
|
|
-
|
Left justify instead of default right justify.
|
+
|
Sign for both positive numbers and negative.
|
#
|
Precede with
0 ,
0x or
0X for
%o ,
%x and
%X tokens.
|
space
|
Left pad with spaces.
|
0
|
Left pad with zeroes.
|
Width
|
|
integer
|
Minimum number of characters to print: invokes padding if necessary. Will not truncate.
|
*
|
Width specified by a preceding argument in
printf .
|
Precision
|
|
.integer
|
Minimum # of digits to print for
%d ,
%i ,
%o ,
%u ,
%x ,
%X . Left pads with zeroes. Will not truncate. Skips values of 0.
|
|
Minimum # of digits to print after decimal point for
%a ,
%A ,
%e ,
%E ,
%f ,
%F (default of 6.)
|
|
Minimum # of significant digits to print for
%g &
%G .
|
|
Maximum # of characters to print from
%s (a string.)
|
.
|
If no
integer is given, default of 0.
|
.*
|
Precision specified by a preceding argument in
printf .
|
Length
|
|
hh
|
Display a
char as
int .
|
h
|
Display a
short as
int .
|
l
|
Display a
long integer.
|
ll
|
Display a
long long integer.
|
L
|
Display a
long double float.
|
z
|
Display a
size_t integer.
|
j
|
Display a
intmax_t integer.
|
t
|
Display a
ptrdiff_t integer.
|
Preprocessor Directives
#include <inbuilt.h>
|
Replaces line with contents of a standard C header file.
|
#include "./custom.h"
|
Replaces line with contents of a custom header file.
Note dir path prefix & quotations.
|
#define NAME value
|
Replaces all occurrences of
NAME with
value .
|
Comments
|
C Reserved Keywords
_Alignas
|
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
|
_Noreturn
|
double
|
register
|
unsigned
|
_Static_assert
|
else
|
restrict
|
void
|
_Thread_local
|
enum
|
return
|
volatile
|
auto
|
extern
|
short
|
while
|
_A-Z...
|
__...
|
C / POSIX Reserved Keywords
E[0-9]...
|
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
|
|
Header Reserved Keywords
Name
|
Reserved By Library
|
d_...
|
dirent.h
|
l_...
|
fcntl.h
|
F_...
|
fcntl.h
|
O_...
|
fcntl.h
|
S_...
|
fcntl.h
|
gr_...
|
grp.h
|
..._MAX
|
limits.h
|
pw_...
|
pwd.h
|
sa_...
|
signal.h
|
SA_...
|
signal.h
|
st_...
|
sys/stat.h
|
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
|
Heap Space
|
|
Allocating |
|
|
Returns a memory location if successful, |
|
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 |
标准库
|
|
Randomicity |
|
|
Returns a (predictable) random integer between 0 and RAND_MAX based on the randomiser seed. |
|
The maximum value |
|
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 |
The Character Type Library
#include <ctype.h>
|
|
tolower(char)
|
Lowercase
char .
|
toupper(char)
|
Uppercase
char .
|
isalpha(char)
|
True if
char is a letter of the alphabet, false otherwise.
|
islower(char)
|
True if
char is a lowercase letter of the alphabet, false otherwise.
|
isupper(char)
|
True if
char is an uppercase letter of the alphabet, false otherwise.
|
isnumber(char)
|
True if
char is numerical (
0 to
9 ) and false otherwise.
|
isblank
|
True if
char is a whitespace character (
' ', '\t', '\n' ) and false otherwise.
|
字符串库
|
|
|
Returns # of |
|
Copies strings. Copies string |
|
Concatenates strings. Copies string |
|
Compares strings. Returns false if string |
|
Searches for string |
Alternatives |
|
|
Copies strings. Copies |
|
Concatenates strings. Copies |
|
Compares first |
时间库
|
|
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 |
|
Returns a |
|
Breaks |
Unary Operators
by descending evaluation precedence
|
|
+a
|
Sum of
0 (zero) and
a . (0 + a)
|
-a
|
Difference of
0 (zero) and
a . (0 - a)
|
!a
|
Complement (logical NOT) of
a . (~a)
|
~a
|
Binary ones complement (bitwise NOT) of
a . (~a)
|
++a
|
Increment of
a by
1 . (a = a + 1)
|
--a
|
Decrement of
a by
1 . (a = a - 1)
|
a++
|
Returns
a then increments
a by
1 . (a = a + 1)
|
a--
|
Returns
a then decrements
a by
1 . (a = a - 1)
|
(type)a
|
Typecasts
a as
type .
|
&a;
|
Memory location of
a .
|
sizeof(a)
|
Memory size of
a (or
type ) in bytes.
|
二进制运算符
by descending evaluation precedence |
|
|
Product of |
|
Quotient of dividend |
|
Remainder of integers dividend |
|
Sum of |
|
Difference of |
|
Left bitwise shift of |
|
Right bitwise shift of |
|
Less than. True if |
|
Less than or equal to. True if |
|
Greater than. True if |
|
Greater than or equal to. True if |
|
Equality. True if |
|
Inequality. True if |
|
Bitwise AND of |
|
Bitwise exclusive-OR of |
|
Bitwise inclusive-OR of |
|
Logical AND. True if both |
|
Logical OR. True if either |
三元&分配运算符
by descending evaluation precedence |
|
|
Evaluates |
|
Assigns value of |
|
Assigns product of |
|
Assigns quotient of dividend |
|
Assigns remainder of integers dividend |
|
Assigns sum of |
|
Assigns difference of |
|
Assigns left bitwise shift of |
|
Assigns right bitwise shift of |
|
Assigns bitwise AND of |
|
Assigns bitwise exclusive-OR of |
|
Assigns bitwise inclusive-OR of |