Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Just wait till you find out that you can do:

const char *s =

#include "test.txt"

;



Sure, but only if the text file looks like a C string literal, i.e. starts and ends with double quotes (which would make it into a weird text file).

Doing

    const char *s = "
    #inclued "test.txt"
    ";
won't work, since the preprocessor won't interpret directives inside string literals of course.

In many assemblers, there is a directive called "incbin" which pastes in unstructured binary data at the point of usage. I just found a very clever C and C++ wrapper [1] for that, which gives you an INCBIN() macro. Nice!

[1]: https://github.com/graphitemaster/incbin


An incbin-like facility is coming to the standard: https://thephd.dev/finally-embed-in-c23


Yeah, I actually had heard about that but it didn't show up in my first search. Thanks!


Note that C23 will include a variant of incbin spelled #embed: Semantically, the preprocessor will insert a list of integers which you can use to initialize an array.


Also, for clarity, it is fully expected that the compiler will use the as-if rule to optimize it. Most likely by having a dedicated token/ast node that only decomposes to comma separated values if if actually needs to, with common usage in initalizers being handled by simply copying the data directly into a static data segment without ever creating an integer list at all.


Yeah, ignoring incbin hackery, a better example than strings might be

  float vertices[] = {
  #include "stuff.txt"
  };




Consider applying for YC's Winter 2026 batch! Applications are open till Nov 10

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: