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:
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.,
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