Show Menu
Cheatography

C Programming Cheat Sheet (DRAFT) by

a cheat sheet for C programming language

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

Console Input/­Output

#include <st­dio.h>
Formatted Data
scanf()
Read value/s (type defined by format string) into variable/s (type must match) from the input stream. Stops reading at the first whites­pace.
&
prefix not required for arrays (including strings.) (unsafe)
printf()
Prints data (formats defined by the format string) as a string to the output stream
Altern­ative
fgets(­str­Name, length, stdin); sscanf();
Uses fgets to limit the input length, then uses sscanf to read the resulting string in place of scanf. (safe)
Characters
 
getchar()
Returns a single charac­ter's ANSI code from the input stream buffer as an integer. (safe)
putcha­r(int)
Prints a single character from an ANSI code integer to the output stream buffer.
Strings
 
fgets(­str­Name, length, stdin);
Reads a line from the input stream into a string variable. (Safe)
puts("s­tri­ng") 
Prints a string to the output stream.

Data Types

Basic Data Type
Floati­ng-­point, integer, double, character
Derived Data Type
Union, structure, array, etc
Enumerated Data Type
Enums
Void Data Type
Empty Value
Boolean Type
True or False

Primary Data Types

Integer
int
Storing Whole Numbers
Character
char
Refers to all ASCII character sets and single alphabets
Long
long
Long integer
Floating point
float
Refer to all the real number value or decimal point
Double (Long float)
double
Include all large type of numeric that do not come under floating point or integer
Void
void
No value

Derived Data Types

Data Type
Descri­ption
Arrays
A Sequence of a finite number of data items
Function
A self-c­ont­ained block of single or multiple statem­ents.
Pointers
Special form of variables for holding other variables’ addresses.
Unions
The memory that we allocate to the largest data type gets reused for all the other types present in the group.
Structures
A collection of various different types of data type items
 

Standard Library Functions

#include<...>
 
Header File
Descri­ption
stdio.h
Standard input/­output header file
conio.h
Console input/­output header file
string.h
String related functions are defined in this header file
string.h
Contains general functions used in C
math.h
All math related functions are defined here
time.h
Contains time and clock related functions
ctype.h
All character handling functions are defined here
stdarg.h
Variable argument functions are declared here
signal.h
Signal handling functions are declared here
setjmp.h
Includes all jump functions
locale.h
Includes locale functions
errno.h
Includes error handling functions
assert.h
Includes diagnostic functions

Commenting

//
Insert single­-line comment
example:
//This is a single line comment
 
/*
Insert multip­le-line comment
example:
/*This is
a multiple
line comment*/

Condit­ional Statements

if
used to specify a block of code to be executed, if a specified condition is true
else
used to specify a block of code to be executed, if the same condition is false
else if
used to to specify a new condition to test, if the first condition is false
switch
Used instead of writing many
if...else
statements
break
Used to stop the execution of more code and case testing inside the block

Looping

while
Loops through a block of code as long as a specified condition is true
do/while
This loop will execute the code block once, before checking if the condition is true, then it will repeat the loop as long as the condition is true
for
Loops through a block of code for a specified amount of repeti­tions
continue
Breaks one iteration (in the loop), if a specified condition occurs, and continues with the next iteration in the loop
break
Skips rest of loop contents and exits loop
 

Keywords

auto
double
int
struct
break
else
long
switch
case
enum
register
typedef
char
extern
return
union
const
float
short
unsigned
continue
for
signed
void
default
goto
sizeof
volatile
do
if
static
while

Control characters (Escape sequences)

\a
Alert (bell) character
\b
Backspace
\f
Formfeed
\n
New line
\r
Carriage return
\t
Horizontal tab
\v
Vertical tab
\\
Backslash
\?
Question mark
\'
Single quote
\"
Double quote
\0
null
\nnn
Any octal ANSI character code
\xhh
Any hexade­cimal ANSI character code

Arithmetic Operators

Operator
Name
Descri­ption
+
Addition
Adds together two values
-
Subtra­ction
Subtracts one value from another
*
Multip­lic­ation
Multiplies two values
/
Division
Divides one value by another
%
Modulus
Returns the division remainder
++
Increment
Increases the value of a variable by 1
--
Decrement
Decreases the value of a variable by 1
compute
,
calculate
, or
add
statments can also be used

Format Specifiers

%a
Signed hexade­cimal float
%c
A character
%d
or
%i
Signed decimal integer
%6d
Print as a decimal integer, at least 6 characters wide
%e
Signed decimal with scientific notation
%f
Signed decimal float
%6f
Print as floating point, at least 6 characters wide
%.2f 
Print as floating point, 2 characters after decimal point
%6.2f
Print as floating point, at least 6 wide and 2 after decimal point
%g
Shortest repres­ent­ation of
%f
or
%e
%o
Unsigned octal integer
%s
Character string
%u
Unsigned decimal integer
%x
Unsigned hexade­cimal integer
%p
Display a pointer
%%
Print a %
 

Primitive Variable Types

applicable but not limited to most ARM, AVR, x86 & x64 instal­lations
[class] [quali­fier] [unsigned] type/void name;
by ascending arithmetic conversion
Integers
Type
Bytes
Value Range
char
1
unsigned
or
signed
unsigned char
1
0 to 255
signed char
1
-128 to 127
int
2 / 4
unsigned
or
signed
unsigned int
2 / 4
0 to 65,535 or 231-1
signed int
2 / 4
-32,768 to 32,767 or -231 to 232-1
short
2
unsigned
or
signed
unsigned short
2
0 to 65,535
signed short
2
-32,768 to 32,767
long
4 / 8
unsigned
or
signed
unsigned long
4 / 8
0 to 232-1 or 264-1
signed long
4 / 8
-231 to 231-1 or -263 to 263-1
long long
8
unsigned
or
signed
unsigned long long
8
0 to 264-1
signed long long
8
-263to 263-1
//
signed
is the default modifier of
int
and
char
data type (allows + or - value)
//
unsigned
only stores positive values
Floats
   
Type
Bytes
Value Range (Norma­lized)
float
4
±1.2×10-38 to ±3.4×1038
double
8 / 4
±2.3×10-308 to ±1.7×10308 or alias to
float
for AVR
long double
ARM: 8, AVR: 4, x86: 10, x64:16
Qualifiers
   
const type
Flags variable as read-only (compiler can optimise)
volatile type
Flags variable as unpred­ictable (compiler cannot optimise)
Storage Classes
   
register
Quick access required. May be stored in RAM or a register. Maximum size is register size.
static
Retained when out of scope.
static
global variables are confined to the scope of the compiled object file they were declared in.
extern
Variable is declared by another file
Typecasting
   
(type)a
Returns
a
as data
type
char x = 1, y = 2; float z = (float) x / y;
Some types (denoted with or) are archit­ecture dependant.

There is no primitive boolean type, only zero (false,
0
) and non-zero (true, usually
1
).