Types are sizes and meanings
In Python, an integer can grow as large as memory allows, and 7 / 2 is 3.5. In C, every value has a type that fixes two things at compile time: how many bytes it occupies, and how those bytes are interpreted. An int is usually four bytes holding a signed whole number; a double is eight bytes holding a binary floating-point number. Nothing grows, and arithmetic happens at the width of the types involved.
That is why C is fast (the processor works on fixed-size values directly) and why it surprises newcomers. 7 / 2 is 3. Adding 1 to the largest unsigned value gives 0. 0.1 + 0.2 is not exactly 0.3. A negative number compared with an unsigned one can come out "bigger". None of this is random: there are a handful of rules, and this lesson teaches them with programs whose output you can check.
The arithmetic types
Integers
C has five standard signed integer types, from smallest to largest: signed char, short, int, long and long long, each with an unsigned partner. The standard sets only minimum ranges (an int must hold at least −32767 to 32767), so the actual sizes depend on the platform. Here they are on 64-bit Linux:
#include <limits.h>
#include <stdint.h>
#include <stdio.h>
int main(void) {
printf("char %zu byte\n", sizeof(char));
printf("short %zu bytes\n", sizeof(short));
printf("int %zu bytes, up to %d\n", sizeof(int), INT_MAX);
printf("long %zu bytes, up to %ld\n", sizeof(long), LONG_MAX);
printf("long long %zu bytes\n", sizeof(long long));
printf("float %zu bytes\n", sizeof(float));
printf("double %zu bytes\n", sizeof(double));
printf("size_t %zu bytes\n", sizeof(size_t));
printf("int32_t %zu bytes, from %d to %d\n", sizeof(int32_t), INT32_MIN, INT32_MAX);
printf("uint8_t %zu byte, up to %d\n", sizeof(uint8_t), UINT8_MAX);
return 0;
}char 1 byte
short 2 bytes
int 4 bytes, up to 2147483647
long 8 bytes, up to 9223372036854775807
long long 8 bytes
float 4 bytes
double 8 bytes
size_t 8 bytes
int32_t 4 bytes, from -2147483648 to 2147483647
uint8_t 1 byte, up to 255sizeof gives the size of a type or value in bytes, as a size_t, printed with %zu. On 64-bit Windows, long is 4 bytes, not 8; code that assumed otherwise has broken many ports. That is why modern C uses the fixed-width types from <stdint.h> when the size matters: int8_t, int16_t, int32_t, int64_t and their uint…_t partners. Two more types appear everywhere:
size_t: an unsigned type big enough for the size of any object. Lengths, counts and array indexes use it.ptrdiff_t: a signed type for the difference between two pointers.
Definition— signed and unsigned integers
A signed integer type represents negative and positive values (C23 requires two's complement, which every current machine uses). An unsigned type represents only non-negative values, and its arithmetic is defined modulo 2ⁿ, where n is its number of bits: results that do not fit wrap around.
The plain char type is one byte. Whether plain char is signed or unsigned depends on the platform (signed on x86-64 Linux, unsigned on ARM64 Linux), which matters the moment you store bytes above 127 in one.
Floating point
float (usually 4 bytes, about 7 significant decimal digits) and double (8 bytes, about 15–16 digits) are binary floating-point types, in practice always in the IEEE 754 format. A floating constant such as 3.5 is a double; 3.5f is a float. Use double unless you have a reason not to; printf prints both with %f, %g or %e.
Booleans and characters
<stdbool.h> provides bool, true and false (in C23 they are keywords and the header is no longer needed). Conditions in C are integers: zero is false and anything else is true, and comparison operators produce the int values 0 and 1. A character constant such as 'A' is simply an int holding the character's code, 65 in ASCII.
Declaring and qualifying
A declaration names a type and a variable, optionally with an initial value: int count = 0;. Always initialise: a local variable without an initialiser holds an indeterminate value, and reading it is a bug. const makes a variable read-only after initialisation (const double gravity = 9.81;), and the compiler rejects assignments to it. Integer constants can be written in decimal (255), hexadecimal (0xFF) or octal (0377, with a leading zero, a trap for anyone who pads numbers with zeros), with suffixes u, l and ll for unsigned and longer types. C23 adds binary constants such as 0b1010 (long supported by GCC and Clang as an extension) and digit separators such as 1'000'000.
Integer arithmetic
C's arithmetic operators are +, -, *, / and % (remainder). On integers, two of them behave differently from mathematics:
#include <inttypes.h>
#include <limits.h>
#include <stdio.h>
int main(void) {
/* Integer division truncates towards zero; % takes the sign of the left operand. */
printf(" 7 / 2 = %d, 7 %% 2 = %d\n", 7 / 2, 7 % 2);
printf("-7 / 2 = %d, -7 %% 2 = %d\n", -7 / 2, -7 % 2);
printf(" 7 / -2 = %d, 7 %% -2 = %d\n", 7 / -2, 7 % -2);
/* Unsigned arithmetic wraps around, by definition. */
unsigned int big = UINT_MAX;
printf("UINT_MAX = %u, UINT_MAX + 1 = %u\n", big, big + 1u);
/* Fixed-width types and their printf macros from inttypes.h. */
int64_t population = 8100000000; /* too big for a 32-bit int */
printf("people: %" PRId64 "\n", population);
/* Mixing integer and floating point: the order of operations matters. */
printf("5 / 2 * 2.0 = %.1f\n", 5 / 2 * 2.0);
printf("5 / 2.0 * 2 = %.1f\n", 5 / 2.0 * 2);
return 0;
} 7 / 2 = 3, 7 % 2 = 1
-7 / 2 = -3, -7 % 2 = -1
7 / -2 = -3, 7 % -2 = 1
UINT_MAX = 4294967295, UINT_MAX + 1 = 0
people: 8100000000
5 / 2 * 2.0 = 4.0
5 / 2.0 * 2 = 5.0The rules on display:
- Integer division truncates toward zero, so −7 / 2 is −3, not −4 as "floor division" in Python would give. The remainder is defined so that
(a / b) * b + a % b == aalways holds, which makes-7 % 2equal to −1. To test whether a number is odd, comparen % 2 != 0, nevern % 2 == 1, which fails for negative odd numbers. - Unsigned arithmetic wraps:
UINT_MAX + 1is 0. This is defined behaviour, and hash functions and checksums rely on it. - Signed overflow is not defined. If an
intcalculation exceedsINT_MAX, the C standard says nothing about what happens; compilers assume it never does and optimise on that basis. Lesson 9 is about exactly this, and about tools that catch it. For now: choose types wide enough for your values. - Printing fixed-width types needs the macros in
<inttypes.h>, such asPRId64forint64_t, becauseint64_tislongon some systems andlong longon others. String literals placed side by side are joined, so"%" PRId64 "\n"becomes one format string. - Evaluation order matters for types.
5 / 2 * 2.0computes5 / 2first, in integers, giving 2, and only then multiplies by 2.0. Moving the2.0earlier changes the answer.
Floating point and conversions
Floating point is an approximation
A double stores a binary fraction with 53 significant bits. Most decimal fractions, including 0.1, 0.2 and 0.3, have no exact binary form, so each is stored as the nearest representable value, and tiny errors appear:
#include <math.h>
#include <stdio.h>
int main(void) {
double a = 0.1 + 0.2;
printf("0.1 + 0.2 = %.17g\n", a);
printf("equal to 0.3? %s\n", a == 0.3 ? "yes" : "no");
printf("close to 0.3? %s\n", fabs(a - 0.3) < 1e-9 ? "yes" : "no");
float f = 16777216.0f; /* 2 to the power 24 */
printf("float 16777216 + 1 = %.1f\n", f + 1.0f);
double d = 16777216.0;
printf("double 16777216 + 1 = %.1f\n", d + 1.0);
printf("1.0 / 0.0 = %f, sqrt(-1.0) = %f\n", 1.0 / 0.0, sqrt(-1.0));
return 0;
}0.1 + 0.2 = 0.30000000000000004
equal to 0.3? no
close to 0.3? yes
float 16777216 + 1 = 16777216.0
double 16777216 + 1 = 16777217.0
1.0 / 0.0 = inf, sqrt(-1.0) = -nanThree lessons from this program. Never compare computed floating-point values with ==; compare the difference with a tolerance suited to your problem. A float has only 24 significant bits, so above 2²⁴ it cannot even represent every whole number, and adding 1 does nothing; this is one reason to prefer double. And floating point has special values: dividing by zero gives infinity, and invalid operations give NaN, "not a number" (glibc prints the sign of the NaN, hence -nan). The functions sqrt and fabs come from <math.h>; on Linux, programs that use it are linked with -lm.
The ?: in this program is the conditional operator: cond ? a : b evaluates to a if cond is true and b otherwise. It is an expression, so it can sit inside a function call.
Implicit conversions
When an expression mixes types, C converts them by two rules:
Definition— integer promotions
Before arithmetic, values of types smaller than int (char, short, uint8_t and so on) are converted to int. So arithmetic never happens on bytes or shorts directly.
Definition— usual arithmetic conversions
When a binary operator's operands have different types, both are converted to a common type: if either is floating point, to the wider floating type; otherwise to the wider integer type, and if one is signed and the other unsigned of the same rank, to the unsigned type.
The first rule is usually harmless and sometimes convenient. The second contains C's most famous trap:
int a = -1;
unsigned int b = 1;
if (a < b) puts("less"); else puts("not less"); /* prints "not less" */a is converted to unsigned int, where −1 becomes 4294967295, which is larger than 1. GCC with -Wextra warns: comparison of integer expressions of different signedness: 'int' and 'unsigned int' [-Wsign-compare]. The same trap appears whenever a signed index is compared with a size_t length. Keep sizes and indexes in size_t, or convert explicitly and deliberately.
Conversions also happen on assignment. Assigning a value to a narrower type keeps the low bits (for unsigned targets) and assigning a double to an int truncates toward zero. A cast, written (type)value, makes a conversion explicit, which documents it for the reader and silences warnings such as -Wconversion.
Bits, logic and characters
C's bitwise operators work on the bits of integers: & (and), | (or), ^ (exclusive or), ~ (not), << and >> (shifts). They are the everyday tools of flags, masks, hardware registers and compact data. The logical operators &&, || and ! work on truth values and short-circuit: a && b does not evaluate b if a is false.
#include <stdint.h>
#include <stdio.h>
enum { READ = 4, WRITE = 2, EXEC = 1 }; /* permission bits */
int main(void) {
/* Small types are promoted to int before arithmetic. */
uint8_t x = 200, y = 100;
int sum = x + y; /* computed as int: 300 */
uint8_t wrapped = (uint8_t)(x + y); /* converted back to 8 bits: 300 - 256 */
printf("sum = %d, wrapped = %d\n", sum, wrapped);
char letter = 'A';
printf("'A' is %d, and the next letter is %c\n", letter, letter + 1);
/* Bitwise operators combine and test flags. */
int perms = READ | WRITE; /* 4 | 2 = 6 */
printf("perms = %d\n", perms);
printf("can write? %s\n", (perms & WRITE) ? "yes" : "no");
printf("can exec? %s\n", (perms & EXEC) ? "yes" : "no");
perms ^= WRITE; /* toggle WRITE off */
printf("after toggling write: %d\n", perms);
printf("1 << 10 = %d\n", 1 << 10);
/* && and || stop as soon as the answer is known. */
int divisor = 0;
if (divisor != 0 && 10 / divisor > 1) {
printf("never printed\n");
} else {
printf("short-circuit kept us from dividing by zero\n");
}
return 0;
}sum = 300, wrapped = 44
'A' is 65, and the next letter is B
perms = 6
can write? yes
can exec? no
after toggling write: 4
1 << 10 = 1024
short-circuit kept us from dividing by zeroThe enum line is a common way to name integer constants; lesson 6 covers enumerations fully. perms ^= WRITE is a compound assignment, short for perms = perms ^ WRITE; every binary arithmetic and bitwise operator has one (+=, -=, *=, <<= and so on). The increment and decrement operators ++ and -- add or subtract one; x++ yields the old value and ++x the new one, a difference that matters only when the value is used.
Precedence
C has fifteen levels of operator precedence, and two of them catch everyone. Shifts bind more loosely than arithmetic, so 1 + 2 << 1 is (1 + 2) << 1. And bitwise operators bind more loosely than comparisons, so flags & MASK == 0 means flags & (MASK == 0). GCC's -Wall warns about both (suggest parentheses), and the professional habit is simply to parenthesise any expression that mixes bitwise operators with anything else.
Choosing types in real code
Professional C code follows a few habits that remove most type bugs before they happen:
intfor ordinary small numbers, such as loop counters over small ranges, flags and return codes. It is the type the processor and the compiler handle most naturally.size_tfor sizes, lengths and indexes. It is whatsizeof,strlenand the memory functions use, so mixing it withintinvites the signed/unsigned trap. Be careful counting down:for (size_t i = n - 1; i >= 0; i--)never ends, because an unsignediis always at least 0.- Fixed-width types at boundaries: file formats, network protocols, hardware registers and anything stored or sent elsewhere use
uint8_t,uint16_t,int32_tand so on, so the layout is the same on every machine. - Unsigned types for bits, not for "numbers that should not be negative". A quantity that can be subtracted, such as a difference of two times, belongs in a signed type; making it unsigned turns small negative results into huge positive ones.
doublefor measurements; integers for money. Store amounts as whole numbers of the smallest unit and convert only for display.- Turn on more warnings.
-Wconversionand-Wsign-conversion(not included in-Wall -Wextra) flag every implicit narrowing or sign change. They are noisy in old code bases but valuable in new ones, and most teams that adopt them fix code until the warnings disappear rather than silencing them.
Behind all of these is one principle: make the type say what the value means. A reader who sees size_t count or int64_t offset_ns knows the range and the unit without reading another line, and the compiler can check more for you.
Common mistakes
Misconception
"int is 32 bits and long is 64 bits." Only minimums are guaranteed, and real platforms differ: long is 32 bits on 64-bit Windows and 64 bits on 64-bit Linux and macOS. Use int32_t, int64_t and size_t when the size matters.
Misconception
"Overflow wraps around, like in assembly." That is true for unsigned types only. Signed overflow is undefined behaviour: the optimiser may assume it cannot happen and remove the very checks you wrote to detect it. Check before the operation, or use the overflow-checking builtins and C23's <stdckdint.h> shown in lesson 9.
Misconception
"x = 3.7 stores 4 in an int." Conversion from floating point to integer truncates toward zero: 3.7 becomes 3 and −3.7 becomes −3. Use round, floor or ceil from <math.h> when you mean rounding.
Misconception
"char holds a character." It holds a small integer, the code of one byte. A letter such as é or 日 takes several bytes in UTF-8 and does not fit in one char. Lesson 5 deals with text properly.
Exercises
All outputs below were checked by compiling and running the code with GCC (-std=c17 -Wall -Wextra).
1. Types, values and operators
CoreComputationalWhat do -17 / 5 and -17 % 5 evaluate to in C? Give both numbers.
2. Types, values and operators
CoreComputationalGiven uint8_t a = 250, b = 10;, what do printf("%d %d\n", a + b, (uint8_t)(a + b)); print? Give both numbers.
3. Types, values and operators
CoreComputationalWhat does printf("%d\n", 1 + 2 * 3 << 1); print? (GCC also warns about it.)
4. Types, values and operators
CoreComputationalWhat is the value of (6 & 3) | (1 << 3)?
5. Types, values and operators
CoreComputationalWith int a = -1; unsigned int b = 1;, what does if (a < b) puts("less"); else puts("not less"); print, and why?
6. Types, values and operators
Warm-upHumanWhich is the right way to check whether a computed double x equals 0.3?
7. Types, values and operators
Warm-upComputationalWhat is the value of the int expression 10 / 4 * 4?
Further reading and resources
- cppreference.com, Arithmetic typesEvery integer and floating type, its minimum range and its typical sizes.
- cppreference.com, Implicit conversionsThe precise rules for integer promotion and the usual arithmetic conversions.
- cppreference.com, C Operator PrecedenceThe full precedence and associativity table, worth bookmarking.
- What Every Computer Scientist Should Know About Floating-Point Arithmetic · David Goldberg · 1991The classic paper on why 0.1 + 0.2 is not 0.3, and what to do about it.