Large arrays using preprocessor directives in C

Is the following syntax valid? If so, please explain how it works.

double array[SIZE][SIZE] = {
    #include "float_values.txt"
}

      

+3


source to share


2 answers


Yes, this is valid C syntax.

In C and C ++, directives are #include

very simple: they simply copy and paste the contents of the file you are using into the current file, replacing the directive #include

.

For example, if your file "float_values.txt" looks like this:

{1.0, 2.0},
{3.0, 4.0}

      



Then the preprocessor transforms your code like this:

double array[SIZE][SIZE] = {
    {1.0, 2.0},
    {3.0, 4.0} 
}

      

However, you must make sure it is SIZE

defined correctly.

+4


source


Yes, this piece of code is valid.



The preprocessor will find the directive #include

and search for a filename float_values.txt

within the specified search paths. Then the contents of this file will be accepted and #include "float_values.txt"

replaced by the contents of the file. If the resulting code is valid, it only depends on the content of the file. To be valid, the file must contain data to initialize a two-dimensional array of doublings, but must not contain more values ​​than the value allowed SIZE

. Smaller values ​​will be fine as the remaining doubles will be initialized by default.

+1


source







All Articles