C Abstract Data Types

Intermediate
6 min read
Updated: Nov 2026
1 example

Interface Versus Implementation

An abstract data type (ADT) defines operations and their behavior without revealing the representation. A stack ADT offers push, pop, and peek; whether it uses an array or linked list is hidden. In C you build ADTs with a struct and a set of functions, ideally an opaque pointer declared in a header and defined in the .c file.

Remember
/* header: interface only */
typedef struct Counter Counter;
Counter *counter_new(void);
void     counter_inc(Counter *c);
int      counter_get(const Counter *c);
void     counter_free(Counter *c);

Cheat Sheet

Keep these facts handy while you work with Abstract Data Types in C.

TopicDetail
InterfaceFunction prototypes
ImplementationStruct definition and bodies
Opaque typeStruct hidden from users
BenefitChange internals freely

C Examples

Compile each example with gcc file.c -std=c11 -Wall -o out. Next: Choosing a Data Structure in C.

Example 1 — A counter ADT

Users call functions and never touch the fields.

C
#include <stdio.h>
#include <stdlib.h>

typedef struct Counter { int n; } Counter;

static Counter *counter_new(void) {
    Counter *c = malloc(sizeof *c);
    if (c) c->n = 0;
    return c;
}
static void counter_inc(Counter *c)   { c->n++; }
static void counter_reset(Counter *c) { c->n = 0; }
static int  counter_get(const Counter *c) { return c->n; }

int main(void) {
    Counter *c = counter_new();
    if (!c) return 1;
    counter_inc(c); counter_inc(c); counter_inc(c);
    printf("count=%d\n", counter_get(c));
    counter_reset(c);
    printf("after reset=%d\n", counter_get(c));
    free(c);
    return 0;
}

How It Works

  1. main only uses the four functions.
  2. If n became a long, no caller would change.
  3. In a real project the struct would live in the .c file for true hiding.

Edge Cases & Pitfalls

Check these before relying on Abstract Data Types in real programs.

leak

Exposing fields

If callers touch struct fields, you cannot change the representation later.

own

Unclear ownership

Document who frees the object.

Key Takeaways

  • Operations first, representation hidden.
  • Opaque pointers enforce it.
  • Document ownership.

One line: An abstract data type (ADT) defines operations and their behavior without revealing the representation.

Frequently Asked Questions

A description of a data type by its operations and behavior, independent of how it is implemented.
Declare an opaque struct pointer and function prototypes in a header, then define the struct and functions in a source file.
Continue with "Choosing a Data Structure in C" next. Match common operations to the structure that makes them cheap. The C Data Structures hub lists every topic in order.

Next: Choosing a Data Structure in C

Match common operations to the structure that makes them cheap.

Continue →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful