#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
