In C/C++, is it strictly necessary for a header file to use the .h extension, or can headers be named without .h (for example, project-specific headers or C++ standard headers like <vector>)? Provide the most accurate answer and rationale.

Difficulty: Easy

Correct Answer: No

Explanation:


Introduction / Context:
Developers often assume header files must end with the .h extension. This question checks your understanding of how the preprocessor's #include works and common conventions in C and C++ projects.



Given Data / Assumptions:

  • We are talking about the C/C++ preprocessor and #include.
  • Standard headers may or may not use .h (e.g., C++ headers like , have no .h).
  • User or project headers can be named arbitrarily if the build system and includes are configured accordingly.


Concept / Approach:
The preprocessor inserts the contents of the file specified in #include. It does not require a specific filename extension. Build tools and IDEs sometimes rely on extensions for syntax highlighting or rules, but the language itself does not mandate .h. Many projects use .hpp, .hh, or no extension (rare but possible). The C++ standard library includes header names without .h (e.g., ), while legacy C headers use .h (e.g., ).



Step-by-Step Solution:
Recognize that #include takes a header name or path; the compiler opens that file literally.No language rule enforces .h; it is a convention for C headers.Therefore, headers need not have the .h extension to compile.



Verification / Alternative check:
Create myheader.inc (or .hpp) and include it with #include "myheader.inc"; you will observe successful compilation if the path is correct.



Why Other Options Are Wrong:
“Yes” and platform-limited answers are incorrect because the language and preprocessor are extension-agnostic. “Only for standard headers” is wrong; standard headers have their own names and not all use .h.



Common Pitfalls:
Confusing naming conventions with language rules; assuming IDE or linter expectations are language requirements.



Final Answer:
No

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion