C Programming Cheatsheet

ADVERTISEMENT

C Programming Cheatsheet
String Format Specifiers
Character Escapes
%[flags][width][.precision][length]specifier
\0 - NULL
Constructs
\b - backspace
%c
char
Do-While Loop
\f - form feed (new page)
i=0;
%hhd||%hhi
signed char
\n - newline
do { printf("%d\n", i); ++i;}
\r - carriage return
%hhu
unsigned char
while(i<10);
\t - tab
For Loop
%hhn
signed char*
\v - vertical tab
for (i=0; i<10; ++i) {
Arithmetic Operators
%lc
wint_t
printf("%d\n", i);
}
%ls
+
wchar_t*
Addition
While Loop
%s
string
-
Subtraction
register int i=0;
while (i<10) { ++i; }
%d || %i
*
signed int
Multiplication
If, else if, else
%u
unsigned int
/
Division
if (0 == 1) {
register signed int ZERO = 0;
%hi
%
Modulus/Remainder
short int
} else if (8 <= 4) {
%hu
unsigned short int
++
Increment by 1
const float PIf = 3.14F;
} else {
%hn
--
Decrement by 1
short int*
static char SYM[3] = "π\0";
++>
%l
signed long int
Pre-increment and compare
}
Macros if
%ln
--> Pre-decrement and compare
long int*
#ifdef __linux__
Equality Operators
%ll
signed long long int
#
include "custom_header.h"
#
include <system_header.h>
==
Equal to
%lln
long long int*
#endif
!=
Not equal to
%llu
unsigned long long int
Switch-case
switch (INPUT) {
<
Less than
%f || %F
float or double (%F is uppercase)
case 0: break;
>
Greater than
%Lf || %Le
long double
default: break;
}
<=
Less than or equal to
%e || %E
scientific notation (mantissa/exponent)
Ternary Operator
>= Greater than or equal to
%g || %G
shortest representation of %e||%E
int out = (input == 7 ? 5 : 3);
Logical Operators
Goto
%o
octal unsigned int
label:
Operand Meaning Example
%x
lowercase hex unsigned int
goto label;
&&
And
(x && y)
Define Datatype
%X
uppercase hex unsigned int
typedef struct { int x, y; } point_t;
||
Or
(x || y)
%a || %A
hexadecimal float-point
typedef union __number {
!
Not
!(x < y)
int i; double d;
%ji
intmax_t
} number_t;
%ju
uintmax_t
Define Enum
Bitwise Operators
%jn
intmax_t*
enum cardsuit {
&
AND
CLUBS = 0,
%zi || %zu
size_t || ssize_t
DIAMONDS, HEARTS, SPADES
|
OR
%zn
size_t*
};
^
Exclusive OR (XOR)
Variable Aliases and Constants
%ti || %tu
ptrdiff_t
~
Ones Complement (NOT)
const double PI = 3.14159;
%tn
ptrdiff_t*
const double *ARCHIMEDES_NUM = &PI;
<<
Left-shift
%p
pointer address
extern const double PI;
// In Header
>>
Right-shift
char PI_SYM[3] = "π\0";
// Unicode
%n
NULL
char PI_UTF8[] = u8"π\0";
Assignment Operators
char16_t PI_UTF16[] = u"π\0";
%%
literal %
Operand Meaning Equivalent
char32_t PI_UTF32[] = U"π\0";
Width and Precision
Arrays
=
Assign
None
%.3f
float precision of 3 (like 3.141)
double num[2] = { 3.14, 5.0 };
+=
Add
X = X + Y
unsigned int LargeArray[2][4] = {
%4d
4 digit wide int (like 2015)
{ 0, 1, 2, 3 }, { 4, 5, 6, 7 } };
-=
X = X - Y
Subtract
%2.2f
2 digits wide and 2 precise (19.95)
char words[2][] = { "BSD", "AIX" };
*=
Multiply
X = X * Y
Order of Operations
Flags
/=
Divide
X = X / Y
() [] -> . ::
! ~ - + * & ++ --
-
Left-justify
%=
Modulus
X = X % Y
* / %
+ -
+
Right-justify
<<=
Left-shift
X = X << Y
<< >>
< <= > >=
SPACE
Blank space
>>=
X = X >> Y
Right-shift
!= ==
& (Bitwise)
#
Preceded hex & octal with "0x" || "0"
&=
AND
X = X & Y
^ (Bitwise)
| (Bitwise)
0
Left-pad with zeros
|=
X = X | Y
OR
Integer from variable - printf("%d", num);
&& || (Logical)
Ternary operator
Save integer to variable - scanf("%d", &num);
^=
XOR
X = X ^ Y
Assignment
Comma Operator
Save string to variable - scanf("%s", str_var);
Created by Devyn Collier Johnson <DevynCJohnson@Gmail.com> (2017) More cheatsheets at DCJTech.info

ADVERTISEMENT

00 votes

Related Articles

Related forms

Related Categories

Parent category: Education
Go
Page of 4