Unusual C++ use of #include

Wanted to share with you an unusual way of using the very well-known #include C++ preprocessor directive.

"The #include directive causes a copy of a specified file to be included in place of the directive" – C++ How To Program

The usual use is to write it at the very top of your .h/.cpp files to include other header files like (Windows.h, iostream, cstdio, etc..) to use the what is defined inside in your .h/.cpp file.

The unusual use is to use it to initialize a data-structure like arrays by including a text file that contains the array initialization data between the array initializer list parentheses – e.g XX XXX[] = { <HERE> }. This is is illustrated in the sample program below. The same concept can be used to initialize an array of any dimension.

A Simple Sample

// main.cpp
#include <cstdio>
#include <Windows.h>

struct Person
    char        *pName;
    unsigned    Age;
    unsigned    Height;
    char        Gender;

// Declare and initialize a 1D array of Persons structs using PersonTableData text file
// #include "PersonsTableData" will be expanded at COMPILE TIME to the content of PersonsTableData file
Person PersonsTable[] = {
    #include "PersonsTableData"

int main()
    for (int i = 0; i < _countof(PersonsTable); ++i)
        Person &person = PersonsTable[i];

        printf("%s\t%d\t%d\t%c\n", person.pName, person.Age, person.Height, person.Gender);

    return 0;


PersonsTableData text file content










It is not about how many programming constructs you memorize of your preferred programming language, it is about how can you utilize the constructs you know for your own good.

3 Responses to “Unusual C++ use of #include”

  1. Mohammad Ali Diab Says:

    First time to see this ., I usually see ppl use #define instead

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: