C Cheatsheet
Data Types
Use this C reference while you build software engineering projects, review code for technical interview prep, or polish examples for a software engineer resume.
Integer Types
| Type | Size (typical) | Range |
|---|---|---|
char | 1 byte | -128 to 127 (signed) or 0–255 |
unsigned char | 1 byte | 0 to 255 |
short / short int | 2 bytes | -32,768 to 32,767 |
unsigned short | 2 bytes | 0 to 65,535 |
int | 4 bytes | -2,147,483,648 to 2,147,483,647 |
unsigned int | 4 bytes | 0 to 4,294,967,295 |
long | 4 or 8 bytes | platform-dependent |
unsigned long | 4 or 8 bytes | platform-dependent |
long long | 8 bytes | -9.2×10^18 to 9.2×10^18 |
unsigned long long | 8 bytes | 0 to 18.4×10^18 |
Sizes are minimum guarantees:
char≥ 8 bits,short≥ 16,int≥ 16,long≥ 32,long long≥ 64. Usesizeofor<stdint.h>for exact sizes.
int a = 42; unsigned int b = 42u; long c = 123L; long long d = 123LL; unsigned long long e = 123ULL;
Fixed-Width Integer Types (<stdint.h>, C99+)
| Type | Width | Signed range |
|---|---|---|
int8_t | exactly 8 bits | -128 to 127 |
uint8_t | exactly 8 bits | 0 to 255 |
int16_t | exactly 16 bits | -32768 to 32767 |
uint16_t | exactly 16 bits | 0 to 65535 |
int32_t | exactly 32 bits | -2^31 to 2^31-1 |
uint32_t | exactly 32 bits | 0 to 2^32-1 |
int64_t | exactly 64 bits | -2^63 to 2^63-1 |
uint64_t | exactly 64 bits | 0 to 2^64-1 |
intptr_t | pointer-sized | |
uintptr_t | pointer-sized unsigned | |
ptrdiff_t | pointer difference | |
size_t | result of sizeof | unsigned |
ssize_t | signed size_t (POSIX) |
#include <stdint.h> #include <stddef.h> // size_t, ptrdiff_t uint32_t flags = 0xDEADBEEFu; int64_t big = INT64_MAX; // 9223372036854775807 size_t len = sizeof(int);
Limit macros (<limits.h>, <stdint.h>):
#include <limits.h> CHAR_MIN, CHAR_MAX, SCHAR_MIN, SCHAR_MAX, UCHAR_MAX SHRT_MIN, SHRT_MAX, USHRT_MAX INT_MIN, INT_MAX, UINT_MAX LONG_MIN, LONG_MAX, ULONG_MAX LLONG_MIN, LLONG_MAX, ULLONG_MAX // From <stdint.h> INT8_MIN, INT8_MAX, UINT8_MAX INT32_MIN, INT32_MAX, UINT32_MAX INT64_MIN, INT64_MAX, UINT64_MAX SIZE_MAX
Floating-Point Types
| Type | Size | Precision | Range (approx) |
|---|---|---|---|
float | 4 bytes | ~7 decimal digits | ±3.4×10^38 |
double | 8 bytes | ~15 decimal digits | ±1.7×10^308 |
long double | 10/16 bytes | ≥18 digits (platform) | extended |
float f = 3.14f; // f suffix required for float literal double d = 3.14159265358979; long double ld = 3.14159265358979L; // Literals 1.0 // double 1.0f // float 1.0L // long double 1e10 // 1×10^10 (double) 0x1.8p0 // hex float: 1.5 in base-2 (C99+)
Floating-point limits (<float.h>):
#include <float.h> FLT_MIN, FLT_MAX, FLT_EPSILON // float DBL_MIN, DBL_MAX, DBL_EPSILON // double LDBL_MIN, LDBL_MAX, LDBL_EPSILON // long double FLT_DIG, DBL_DIG // decimal digits of precision
Character Type
char c = 'A'; // single character in single quotes char c2 = 65; // same as 'A' (ASCII value) char c3 = '\n'; // newline escape // Escape sequences '\n' // newline '\t' // horizontal tab '\r' // carriage return '\\' // backslash '\'' // single quote '\"' // double quote '\0' // null character (value 0) '\a' // bell '\b' // backspace '\f' // form feed '\v' // vertical tab '\xHH' // hex value, e.g. '\x41' = 'A' '\NNN' // octal value, e.g. '\101' = 'A' // signedness of plain char is implementation-defined // use explicitly signed char or unsigned char when it matters signed char sc = -1; unsigned char uc = 255;
Boolean Type (<stdbool.h>, C99+)
#include <stdbool.h> bool flag = true; bool done = false; _Bool raw = 1; // underlying type; stdbool.h aliases it to bool // In C: any nonzero value is true, zero is false if (0) { /* false */ } if (1) { /* true */ } if (NULL){ /* false */ } if (ptr) { /* true if ptr != NULL */ }
Enumeration Types
enum Color { RED, GREEN, BLUE }; // RED=0, GREEN=1, BLUE=2 enum Color c = GREEN; enum Direction { NORTH = 1, SOUTH = 2, EAST = 4, WEST = 8 }; // typedef for cleaner usage typedef enum { MON=1, TUE, WED, THU, FRI, SAT, SUN } Day; Day today = WED; // WED = 3 printf("%d\n", GREEN); // enums are ints
Void Type
void foo(void); // function returning nothing, taking nothing void *ptr; // generic pointer — can hold any pointer type void *buf = malloc(64); int *ip = (int *)buf; // must cast before dereferencing
Type Aliases with typedef
typedef unsigned long long ull; typedef int (*FuncPtr)(int, int); // pointer-to-function type typedef struct Node Node; // forward declare struct alias typedef char Str[256]; // array type alias ull big = 1000000000000ULL; FuncPtr fn = add;
Integer Literals
| Prefix | Base | Example |
|---|---|---|
| none | decimal | 255 |
0x / 0X | hexadecimal | 0xFF |
0 | octal | 0377 |
0b / 0B | binary | 0b11111111 (C23+) |
| Suffix | Type |
|---|---|
| none | int |
u / U | unsigned int |
l / L | long |
ul / UL | unsigned long |
ll / LL | long long |
ull / ULL | unsigned long long |
int a = 42; int b = 0x2A; // hex 42 int c = 052; // octal 42 uint32_t d = 42u; long e = 42L; long long f = 42LL;
Type Conversions
Implicit (automatic) promotions:
- Integer types narrower than int promote to int in expressions.
- In mixed arithmetic, smaller type converts to larger: int → long → float → double.
- Signed/unsigned: if same size, signed converts to unsigned.
int i = 5; double d = i; // implicit int → double int back = (int)d; // explicit cast (truncates, no rounding) unsigned int u = -1; // wraps to UINT_MAX (defined behavior for unsigned) int overflow = INT_MAX + 1; // UNDEFINED BEHAVIOR — signed overflow
Explicit casts:
double pi = 3.14159; int truncated = (int)pi; // 3 float f = (float)(1.0 / 3.0); // truncate precision char c = (char)65; // 'A' void *vp = (void *)some_ptr;
sizeof and _Alignof
sizeof(int) // 4 on most platforms sizeof(double) // 8 sizeof(char) // always 1 sizeof(arr) // total bytes of array (not pointer!) sizeof(arr)/sizeof(arr[0]) // element count of array _Alignof(int) // alignment requirement (C11+) _Alignof(double) // typically 8
sizeofreturnssize_t(unsigned). Use%zuinprintf.