liveBook · Manning

#embed

As explained in Modern C, Third Edition

#embed is a C23 preprocessing directive that inserts the binary contents of a file into a translation unit as a sequence of integer values.

#embed is a C23 preprocessing directive for embedding a file as binary data. The file becomes a sequence of comma-separated decimal byte values, which can initialize an array directly. section 14.6

Embedding an optional image

unsigned char logo[] = { #if defined(__has_embed) && __has_embed("picture.dat") # embed "picture.dat" #else "embed directive is missing" #endif }; The conditional checks whether the implementation supports #embed and whether picture.dat is available. The embedded bytes then initialize logo; otherwise, the fallback string is used. section 14.6

Use #embed when binary data must be compiled into every program execution or object file, such as:

  • A graphical logo displayed in a popup window. section 14.6
  • A binary signature included in every object file. section 14.6
  • A source file embedded into one character array and compared with bytes read from disk. section 14.6
unsigned char logo[] = {

#if defined(__has_embed) && __has_embed("picture.dat")
# embed "picture.dat"
#else
 "embed directive is missing"
#endif

};

Inspect the complete conditional initializer using __has_embed and the #embed directive. section 14.6

How Modern C, Third Edition covers it

How deep it goes
Defines byte insertion, feature testing, fallback handling, and efficient initializer implementation, but does not specify broader binary-file portability.
Examples
Uses picture.dat, a graphical logo, an embedded source file, and a fread/memcmp comparison.
What it assumes
Assumes familiarity with preprocessing conditionals, arrays, binary data, and basic file I/O.
What you can do after
Build fixed binary assets into arrays and provide conditional fallbacks when compiler support or files are unavailable.
How it connects
Connects preprocessing directives, #include, __has_include, binary files, array initializers, fread, fwrite, and memcmp.

Related concepts

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