Reputation: 31
Is the following a valid syntax? If yes, the please explain how it works .
double array[SIZE][SIZE] = {
#include "float_values.txt"
}
Upvotes: 3
Views: 159
Reputation: 606
Yes, this code snippet is valid.
The preprocessor will find the #include
directive and search for a file named float_values.txt
within the given search paths. Then the contents of that file will be taken and #include "float_values.txt"
will be replaced with the contents of the file.
If the resulting code is valid purely depends on the content of the file.
To be valid, the file must contain data to initialize a two dimensional array of doubles, but must not contain more values than permitted by the value of SIZE
.
Less values would be okay as the remaining doubles would be default initialized.
Upvotes: 1
Reputation: 6031
Yes, this is valid C syntax.
In C and C++, #include
directives are very simple: they just copy and paste the contents of the file you're using into the current file, replacing the #include
directive.
For example, if your "float_values.txt" file looked like this:
{1.0, 2.0},
{3.0, 4.0}
Then the preprocessor would transform your code to look like this:
double array[SIZE][SIZE] = {
{1.0, 2.0},
{3.0, 4.0}
}
However, you must ensure SIZE
is defined correctly.
Upvotes: 4