C is statically typed: the compiler knows the type of every expression. Values are bits interpreted by that type. There is no Java-style boxed Integer. There is no runtime class tag on an int.
printf conversion to use.int for everything” — that hides overflow and size_t bugs.Mental model: pick the type for the domain (count of bytes → size_t; error vs success → int status; text byte → unsigned char or char with a policy). Then know the promotions the compiler will still apply in expressions.
| Type | At least | Notes |
|---|---|---|
char | 8 bits | May be signed or unsigned — implementation-defined. Not “a character” in Unicode. |
signed char / unsigned char | 8 bits | Use these when you mean a small integer or a byte. |
short / int / long / long long | 16 / 16 / 32 / 64 | int is the default. long is 64-bit on LP64 (typical 64-bit Unix) and 32-bit on LLP64 (Windows). |
The standard guarantees minimum ranges, not exact widths. For exact widths use <stdint.h>: int32_t, uint64_t when the implementation provides them.
#include <limits.h> INT_MAX CHAR_BIT /* bits in a byte; 8 on every machine you will use */
+ - * is undefined behaviour. Do not rely on wrap.u > -1 trap (usually true).unsigned u = 0;
if (u - 1 > 0) { } /* UINT_MAX > 0 — true, not a negative */
int i = -1;
unsigned n = 10;
if (i < n) { } /* i converts to unsigned; usually false */
When unsigned wins: bit masks, raw bytes, sizes that cannot be negative. When signed wins: quantities that can be negative; loop indexes that count down through 0 (unsigned i-- never goes negative — it wraps).
size_t is the unsigned type of sizeof and of C library sizes (strlen, malloc arguments). Include <stddef.h> or a header that pulls it in.
sizeof is an operator. sizeof x and sizeof(int) are both legal. Parentheses are required for a type name.sizeof on an array in the same scope as the declaration yields the whole array in bytes — not a pointer. After an array decays to a pointer, sizeof is the pointer’s size.%zu for size_t (C99). %d is wrong.int a[10]; sizeof a; /* 10 * sizeof(int) */ sizeof a[0]; /* sizeof(int) */
In an expression, integer types narrower than int are promoted to int (or unsigned int) before arithmetic. Then the two sides of + are converted to a common type.
unsigned char a = 1, b = 2; int s = a + b; /* + happens as int */ char c = 'A'; int d = c + 1; /* promotion; 'A'+1 is int */
unsigned char + unsigned char does not overflow at 255 the way you might think — it promotes first._Bool. <stdbool.h> provides bool, true, false. Any nonzero converts to 1 when stored in _Bool.float / double / long double. Default floating constant is double. Suffix f for float.void means “no value.” void * is a generic object pointer (see Pointers). You cannot dereference void.string type. A string is a char array with a '\0'.int for an object’s size. Truncates on large objects; mix with size_t and the comparison lies.char for bytes that can be > 127 — sign extension surprises.printf("%d", (size_t)n) — wrong conversion. Use %zu.long is 64-bit on Windows.for (size_t i = n; i >= 0; i--) — unsigned, always true.size_t or unsigned with a comment.sizeof and library sizes are size_t. Match printf conversions.char as a text unit with unknown signedness. Use unsigned char for raw bytes.<stdint.h> when the width is part of a protocol or file format.~ Good luck with your revision! ~