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

TypeSize (typical)Range
char1 byte-128 to 127 (signed) or 0–255
unsigned char1 byte0 to 255
short / short int2 bytes-32,768 to 32,767
unsigned short2 bytes0 to 65,535
int4 bytes-2,147,483,648 to 2,147,483,647
unsigned int4 bytes0 to 4,294,967,295
long4 or 8 bytesplatform-dependent
unsigned long4 or 8 bytesplatform-dependent
long long8 bytes-9.2×10^18 to 9.2×10^18
unsigned long long8 bytes0 to 18.4×10^18

Sizes are minimum guarantees: char ≥ 8 bits, short ≥ 16, int ≥ 16, long ≥ 32, long long ≥ 64. Use sizeof or <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+)

TypeWidthSigned range
int8_texactly 8 bits-128 to 127
uint8_texactly 8 bits0 to 255
int16_texactly 16 bits-32768 to 32767
uint16_texactly 16 bits0 to 65535
int32_texactly 32 bits-2^31 to 2^31-1
uint32_texactly 32 bits0 to 2^32-1
int64_texactly 64 bits-2^63 to 2^63-1
uint64_texactly 64 bits0 to 2^64-1
intptr_tpointer-sized
uintptr_tpointer-sized unsigned
ptrdiff_tpointer difference
size_tresult of sizeofunsigned
ssize_tsigned 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

TypeSizePrecisionRange (approx)
float4 bytes~7 decimal digits±3.4×10^38
double8 bytes~15 decimal digits±1.7×10^308
long double10/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

PrefixBaseExample
nonedecimal255
0x / 0Xhexadecimal0xFF
0octal0377
0b / 0Bbinary0b11111111 (C23+)
SuffixType
noneint
u / Uunsigned int
l / Llong
ul / ULunsigned long
ll / LLlong long
ull / ULLunsigned 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: intlongfloatdouble. - 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

sizeof returns size_t (unsigned). Use %zu in printf.