Ask Your Question
4

How can I use .c instead of .h header files in MISRA C?

asked 2023-06-28 06:19:21 +0000

scrum gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2023-06-28 06:29:02 +0000

ladyg gravatar image

It is not recommended to use .c files as header files in MISRA C, as it can violate the MISRA C rules related to the separation of interface and implementation.

Header files in MISRA C should contain only declarations, type definitions, and macro definitions, while the implementation code should be placed in source files (.c files). This separation is important for ensuring the modularity and portability of the code.

If you have code in a .c file that you want to use as a header file, you should move the declarations and definitions into a separate header file (.h) and include it in your source file.

For example, if you have the following function in a.c:

int foo(int x, int y)
{
   return x + y;
}

You should move the function declaration into a header file, a.h:

#ifndef A_H
#define A_H

int foo(int x, int y);

#endif /* A_H */

And then include a.h in your source file:

#include "a.h"

int main()
{
   int result = foo(3, 4);
   return 0;
}
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-06-28 06:19:21 +0000

Seen: 8 times

Last updated: Jun 28 '23