Study
CSC-301 route
Unit 1 · Lesson 1 of 5 12 min

Primitive Types and Data in Memory

A variable is not a box with a name. It is an address and a set of rules for reading what is there.

A primitive type is a fixed-size pattern of bits with an agreed meaning — which means the same four bytes can be an integer, a float, or garbage, depending only on the rule you read them by. Every structure in this course is built on that bargain: know the size, know the reading rule.

Types Are Sizes and Rules

A primitive type is a fixed-size pattern of bits with an agreed meaning. An integer, a floating-point number, a character and a boolean each occupy a known number of bytes, and the operations on them are defined by the type, not by the value.

Four facts follow, and every one of them causes bugs when forgotten.

Size is fixed by the type. An int in a particular language and machine is 4 bytes; a char is 1. The size does not grow to fit your number.

Range is fixed by the size. A 32-bit signed integer holds −2,147,483,648 to 2,147,483,647. Add one to the largest and it wraps to the smallest — the overflow that has crashed spacecraft and payment systems.

Integers and floating-point numbers are different kinds of thing. Integers are exact; floating-point values are binary fractions, so 0.1 is not exactly representable, and 0.1 + 0.2 is not 0.3. Money in a float is a bug waiting for an audit.

Characters are numbers. A character is a code point in a character set — ASCII for the English letters, Unicode for the world. 'A' is 65, and 'A' + 1 is 'B', which is why string comparison is numeric comparison underneath.

Choosing a type is choosing a size, a range and a set of rules. There is no neutral choice.

One 32-bit integer at address 1000 byte 1000 00000000 byte 1001 00000000 byte 1002 00000000 byte 1003 00000101 address 1000 address 1003 the value 5, stored little-endian: least significant byte first What the diagram tells you The address is of the first byte; the size is four; the meaning depends on the type you read it as. Read the same four bytes as a float and you get a tiny denormal number, not 5.
Bits have no type. A type is the agreement about how many bytes to take and how to interpret them.

Sizes, Ranges and the Costs of Getting Them Wrong

Sizes in one common model: char 1 byte, short 2, int 4, long long 8, float 4, double 8, bool usually 1. A pointer is the size of an address on that machine: 8 bytes on a 64-bit system.

The practical failures:

  • Overflow: an int counter in a system handling billions of transactions rolls over to a negative number. Use a wider type, or a checked type.
  • Truncation: assigning a double to an int silently drops the fraction. 4.99 becomes 4.
  • Precision: storing money as float accumulates errors; store the smallest unit as an integer, or use a decimal type.
  • Character set assumptions: char in C is one byte, so it cannot hold a Unicode code point above 255. Text handling needs an explicit encoding decision.

None of these is a language flaw. Each is the consequence of a fixed representation being asked to do something it was not sized for.

Pause & think

Your friend's payment app computes 0.1 + 0.2 == 0.3 and finds it false. The two sides print as the same number, though. What is happening, and what is the safe fix?

📌 If you remember nothing else…

  • A type fixes a size, a range and a set of operations; variables are addresses plus those rules.
  • A 32-bit signed integer overflows at 2,147,483,647; floating point is inexact by construction.
  • Truncation and precision loss happen silently, which is what makes them expensive.
  • Characters are numbers in an encoding; text handling needs an explicit choice of encoding.

Quick check

Did it land?

1. Why is 0.1 + 0.2 not exactly 0.3 in a program that uses floating-point numbers?
2. A 32-bit signed integer holds values up to 2,147,483,647. What happens when 1 is added to it?
3. What does a type tell the machine?

Spotted a mistake?