Unusual usage of .h file in C

artsin

During reading article about filtering, I've found some strange using of .h file - use it for filling array of coefficients:

#define N 100 // filter order
float h[N] = { #include "f1.h" }; //insert coefficients of filter
float x[N];
float y[N];

short my_FIR(short sample_data)
{
  float result = 0;

  for ( int i = N - 2 ; i >= 0 ; i-- )
  {
    x[i + 1] = x[i];
    y[i + 1] = y[i];
  }

  x[0] = (float)sample_data;

  for (int k = 0; k < N; k++)
  {
    result = result + x[k]*h[k];
  }
  y[0] = result;

  return ((short)result);
}

So, is it normal practice to use float h[N] = { #include "f1.h" }; this way?

Basile Starynkevitch

Preprocessor directives like #include are just doing some textual substitution (see the documentation of GNU cpp inside GCC). It can occur at any place (outside of comments and string literals).

However, a #include should have its # as the first non-blank character of its line. So you'll code

float h[N] = {
  #include "f1.h"
};

The original question did not have #include on its own line, so had wrong code.

It is not normal practice, but it is permitted practice. In that case, I would suggest using some other extension than .h e.g. use #include "f1.def" or #include "f1.data" ...

Ask your compiler to show you the preprocessed form. With GCC compile with gcc -C -E -Wall yoursource.c > yoursource.i and look with an editor or a pager into the generated yoursource.i

I actually prefer to have such data in its own source file. So I would instead suggest to generate a self-contained h-data.c file using e.g. some tool like GNU awk (so file h-data.c would start with const float h[345] = { and end with };...) And if it is a constant data, better declare it const float h[] (so it could sit in read-only segment like .rodata on Linux). Also, if the embedded data is big, the compiler might take time to (uselessly) optimize it (then you could compile your h-data.c quickly without optimizations).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Python unusual file sorting

From Dev

Unusual output of file

From Dev

Unusual C function declaration

From Dev

Readline.H history usage in C

From Dev

Unusual file structure with virtualenv in pycharm?

From Dev

Bind spec file unusual syntax

From Dev

Determine the cause of a File Of Unusual Size

From Dev

Meaning of echo "usage: sysinfo_page [[[-f file ] [-i]] | [-h]]"

From Dev

Unusual C++ function declaration

From Dev

C++: unusual operator overloading

From Dev

Unusual initializer for char array in C

From Dev

Unusual behaviour in first C program

From Dev

C++: unusual operator overloading

From Dev

Unusual form of FOR statement in C++

From Dev

Why is usage of the downloadURL & updateURL keys called unusual and how do they work?

From Dev

Unusual Code in almost each WordPress PHP File

From Dev

Getting unusual output in simple file handling program

From Dev

How to remove a file that start with ">" or other unusual characters

From Dev

C: Unusual behavior of for loop in Turbo C compiler

From Dev

C: Unusual behavior of for loop in Turbo C compiler

From Dev

C# Unusual Instantiation of a Method/Property

From Dev

Unusual C++ behaviour when calling constructor

From Dev

Is there a way to define unusual integer sizes in C++?

From Dev

Unusual Output from C Array Code

From Dev

Unusual/Wrong Behavior of C++ Program

From Dev

Printf having unusual behavior in C program

From Dev

Unusual/Wrong Behavior of C++ Program

From Dev

C++ pow unusual type conversion

From Dev

Is it possible to read and write to a .h file with objective c?