Enumerated Types and Data in Memory, CS1 – Study Notes
offline

Difficulty: Beginner | Prerequisites: Basic C syntax (variables, types, printf/scanf)


Big Picture

This material covers two foundational pieces of C programming: enumerated types and how data lives in memory. Enumerated types let you create readable, named constants instead of scattering magic numbers through your code. The memory section introduces bits, bytes, words, and the sizeof operator, which you will need before tackling pointers and dynamic memory allocation. If you have not yet worked with basic C types (int, float, double, char), review those first.


TL;DR

Enumerated types (enum) let you define a custom type with named integer constants, improving readability and reducing bugs from bare numbers. Data in memory is stored as bits grouped into bytes and words, and the sizeof operator tells you how much space a given type or variable occupies.


Key Terms

Enumerated type (enum)

A user-defined type that assigns names to a set of integer constants. Created using typedef enum. Think of it as giving meaningful labels to numbers so your code reads like English instead of a spreadsheet of integers.

Atomic type (primitive type)

A built-in data type provided by C: int, float, double, char, and so on. In simple terms, these are the basic building blocks the language gives you before you start defining your own types.

Bit

A single zero or one. The word comes from the contraction of "binary digit." Think of it as the smallest possible piece of information a computer can store.

Byte

Eight consecutive bits. Can store a single char. In simple terms, one byte is one character's worth of storage.

Word

A chunk of memory whose size depends on the computer, almost always 2 or 4 bytes. Typically stores an int. Think of it as the "natural" unit of data that your processor handles in one go.

sizeof

An operator (not a function) that returns the size, in bytes, of a type or variable. In simple terms, it answers "how many bytes does this thing take up?"


Core Content

Enumerated Types in C

  • An enum creates a new type that is a subset of an existing atomic type (usually int).

  • The primary reason to use enums is readability. Named constants are easier to understand and debug than raw numbers.

Declaration with typedef:

typedef enum { North, East, South, West } directionT;
  • Once declared, you can create variables of that type:

directionT housedir;

Default integer mapping:

  • The compiler assigns integer values starting at 0 unless you say otherwise.

    • North = 0, East = 1, South = 2, West = 3

Custom integer values:

typedef enum {
    penny = 1,
    nickel = 5,
    dime = 10,
    quarter = 25,
    halfdollar = 50
} coinT;
  • You can assign any integer you like to each constant. Useful when the numeric value itself carries meaning (coin values in cents, for instance).

Why use enumerated types?

  • You do not need to remember or hard-code numeric values throughout your programme.

  • Code becomes self-documenting, which makes debugging considerably easier.


Data and Memory Fundamentals

  • Memory can be pictured as a long array of numbered cells, each storing one byte.

  • With 4 megabytes of memory, you would have locations numbered from 0 to 4 x 2^20 - 1.

How variables map to memory:

  • A char occupies 1 byte. If the variable ch is stored at memory location 1000, the character value sits in that single cell.

  • An int occupies 4 consecutive bytes. The variable's address is the first of those four bytes.

The sizeof operator:

  • Use it on a type: sizeof(int)

  • Use it on a variable: sizeof x

  • Returns the number of bytes the type or variable occupies. Essential when you start allocating memory dynamically later in the course.


Real-World Applications

Enumerated types appear everywhere in systems programming: network protocols use enums to label packet types, operating systems use them for error codes, and game engines use them for states (idle, running, jumping). The memory model matters any time you care about performance or interact with hardware, which is most of embedded and systems work.


Common Misconceptions

  • Students often assume enum values always start at 0 and increment by 1. They do by default, but you can override any or all of them with custom assignments.

  • Students sometimes treat sizeof as a function. It is an operator. The parentheses are required only when you pass a type name; with a variable, sizeof x is valid C.

  • Students sometimes confuse bits and bytes. A byte is always 8 bits, and that distinction matters when calculating memory sizes.


Why It Matters / Exam Flags

⚠️ Know the default numbering of enum constants (starts at 0, increments by 1) and how custom values override that.

⚠️ Be able to calculate how many bytes a given number of megabytes translates to (4 MB = 4 x 2^20 bytes).

⚠️ Understand what sizeof returns and when parentheses are needed.


Quick Self-Test

  1. True or false: In typedef enum { A, B, C } myType;, the value of B is 2.

  1. Fill in the blank: A byte is ______ consecutive bits.

  1. True or false: sizeof is a function from the standard library.

  1. Fill in the blank: An int is typically stored over ______ consecutive bytes.

  1. True or false: You can assign the same integer value to two different enum constants.


Practice Q&A

Q: What integer value does West hold in typedef enum { North, East, South, West } directionT;?

A: 3. The compiler assigns 0 to North, 1 to East, 2 to South, and 3 to West.

Q: Write a typedef enum for traffic light colours where Red = 0, Yellow = 1, and Green = 2, using default numbering.

A: typedef enum { Red, Yellow, Green } trafficLightT; – default numbering handles 0, 1, 2 automatically.

Q: If an int is 4 bytes and a double is 8 bytes, what does sizeof(int) + sizeof(double) evaluate to?

A: 12.

Q: Why would you use an enum instead of #define constants?

A: Enums group related constants under a single type, which lets the compiler do type checking. #define constants are simple text substitution with no type safety.


Connections to Other Topics

This material connects directly to pointers and dynamic memory allocation, both of which require you to understand how data is laid out in memory. The sizeof operator reappears constantly when calling malloc and calloc. Enumerated types also connect to structs, where you might use an enum field to track state or category within a composite data type.


Related Terms / Search Tags

enum, enumerated type, typedef enum, C enum, atomic type, primitive type, bit, byte, word, sizeof, sizeof operator, memory layout, memory cells, binary digit, CS1, Computer Science I, UCF, dynamic memory background