C · 1 · Foundations2 / 35 · 6%
Types, Sizes & Operators
Primitive types are fixed-width — know exactly how many bytes each one holds.
Examples: int, char, sizeof, casting
shortcuts: ← prev · → next · M mark
1
Primitive Types
C guarantees minimum sizes; use <stdint.h> for exact widths.
Example
example
#include <stdio.h>
#include <stdint.h>
int main(void) {
char a = 'A'; // >= 1 byte
int b = 42; // >= 2 bytes (usually 4)
long c = 1000000L; // >= 4 bytes
double d = 3.14; // 8 bytes
uint32_t e = 0xDEADBEEF; // exactly 4 bytes
printf("int=%zu bytes\n", sizeof(int));
return 0;
}2
sizeof — Always Ask
Never assume type sizes. sizeof is a compile-time operator.
Example
example
printf("%zu %zu %zu %zu\n",
sizeof(char), sizeof(int), sizeof(long), sizeof(void*));Output
1 4 8 8 ← on a typical 64-bit Linux
TIP
%zu is the correct format specifier for size_t.
3
Operators
Arithmetic, comparison, bitwise, logical — plus C's compound assignment shortcuts.
Example
example
int x = 10 + 3 * 4; // 22, * before +
int y = x % 5; // remainder = 2
int m = (x > 20); // 1 (true)
int b = x & 0xF; // bitwise AND
x += 5; // x = x + 5
x <<= 1; // x = x * 24
Casting
Explicit type conversion. Implicit conversions are a bug factory.
Example
example
int a = 7, b = 2;
float wrong = a / b; // 3.0f ← integer division!
float right = (float)a / b; // 3.5f
size_t n = 100;
int i = (int)n; // narrow with intentWATCH OUT
Integer / integer is integer. Cast one operand first.