liveBook · Manning

Opaque type

As explained in Modern C

An opaque data type is a type whose internal representation or purpose is hidden from the code that manipulates it.

An opaque type hides its representation or purpose from the code that manipulates it. Programs use the type through functional interfaces rather than inspecting its internals. section 8.3.1

Writing to a stream without inspecting it

The C library exposes FILE* for streams. Code can pass a stream to fputc or fputs and test whether the pointer is null, but it need not know what FILE contains. Reimplementing puts with fputs(stdout) followed by fputc('\n', stdout) demonstrates the intended boundary: use operations, not representation. section 8.3.1

TAKEAWAY

  • Specify opaque types through functional interfaces. section 8.3.1
  • Do not rely on implementation details, including historical interface quirks. section 8.3.1
  • Treat pointers as opaque early, while postponing their memory-related purpose. section 6.0
  • Use thrd_t to identify threads through thrd_create and thrd_join without exposing its representation. section 18.0
int fputc(int c, FILE* stream);
int fputs(char const s[static 1], FILE* stream);


!@%STYLE%@!
{"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":0,\"ch\":0},{\"line\":0,\"ch\":3}],[{\"line\":1,\"ch\":0},{\"line\":1,\"ch\":3}],[{\"line\":0,\"ch\":4},{\"line\":0,\"ch\":9}],[{\"line\":0,\"ch\":0},{\"line\":0,\"ch\":3}],[{\"line\":1,\"ch\":0},{\"line\":1,\"ch\":3}],[{\"line\":0,\"ch\":17},{\"line\":0,\"ch\":21}],[{\"line\":1,\"ch\":34},{\"line\":1,\"ch\":38}],[{\"line\":0,\"ch\":0},{\"line\":0,\"ch\":3}],[{\"line\":1,\"ch\":0},{\"line\":1,\"ch\":3}],[{\"line\":1,\"ch\":4},{\"line\":1,\"ch\":9}],[{\"line\":1,\"ch\":10},{\"line\":1,\"ch\":20}],[{\"line\":1,\"ch\":23},{\"line\":1,\"ch\":29}],[{\"line\":0,\"ch\":17},{\"line\":0,\"ch\":21}],[{\"line\":1,\"ch\":34},{\"line\":1,\"ch\":38}]]"}
!@%STYLE%@!

Compare the compact fputc and fputs interfaces that operate on FILE* without exposing the stream representation. section 8.3.1

How Modern C covers it

How deep it goes
Defines interface-based abstraction and warns against representation dependence, but defers pointer internals and memory purpose.
Examples
Uses FILE* with fputc and fputs, manually reimplements puts, and identifies threads with thrd_t.
What it assumes
Assumes basic C types and introduces pointers only as opaque values before teaching their full memory-related purpose.
What you can do after
Apply it via C library interfaces while avoiding assumptions about hidden representations.
How it connects
Connects opaque types with functional interfaces, pointers, streams, the C standard library, threads, and typedef.

Also explained in

Compare how 4 books explain opaque type →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Modern C. The section links point to the parts of the book it draws on.