Are Nested Header Files Possible in C?

  • Thread starter Thread starter aostraff
  • Start date Start date
  • Tags Tags
    files
Click For Summary
Nested header files are achievable in C, allowing the inclusion of one header file within another. However, while this is permitted, it can lead to confusion and increased compile times, so it's advisable to minimize their use. To prevent issues with a header being included multiple times in a source file from different paths, it's essential to implement include guards. This can be done using the standard preprocessor directives #ifndef, #define, and #endif, or by using the #pragma once directive if supported by the compiler.
aostraff
Messages
48
Reaction score
0
I was wondering if nested header files are achievable in C? I tried placing #include "header.h" inside a header file but I don't think it worked. thanks.
 
Last edited:
Technology news on Phys.org
Yes, that is allowed and it should work.
 
It does work, although it should be reduced as much as possible.
It's confusing and leads to long compile times.

To avoid problems of a header being included twice in a . cfile through differnet routes you should put include guards around it.
Code:
#ifndef NAME_OF_HEADER_H
#define NAME_OF_HEADER_H

... rest of header file

#endif

or if your compiler supports it, just put "#pragma once" at the top of the file
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
4
Views
5K
  • · Replies 7 ·
Replies
7
Views
2K
Replies
6
Views
2K
Replies
81
Views
7K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K