C Programming - C Preprocessor - Discussion

4. 

It is necessary that a header files should have a .h extension?

[A]. Yes
[B]. No

Answer: Option B

Explanation:

No, the header files have any kind of extension.


Avkash Agrawal said: (May 7, 2011)  
We know that we use many headder file such as

stdio.h, dos.h, conio.h, stdlib.h, graphics.h, iostream.h etc.

So how can you say that there is no need of this extension ?

Atul said: (Aug 4, 2011)  
Can you tell me what are all the other extensions other than .h ?

Manishsoni said: (Sep 27, 2011)  
How can this possible please anyone elaborate it ?

Mani Kandan said: (Oct 21, 2011)  
We can define fact.c, filename.c in header file.

So, if you mention filename.c, it will read the entire file.

Sundar said: (Jan 25, 2012)  
@ALL

Is is necessary that a header files should have ".h" ?

Answer is NO.

Because the header files can have any extension but it should be included with in C program with same extension.

Let me explain what I did to check this...

In Turbo C directory

C:\Turbo>copy stdio.h stdio.hdr
C:\Turbo>copy conio.h conio.exe

Then I used the line following line to include the header file.

#include<stdio.hdr>
#include<conio.exe>

void main()
{
printf("Welcome to IndiaBIX.com !");
getch();
}


The output : Welcome to IndiaBIX.com !

Kapil said: (Apr 13, 2012)  
No such header files exist without .h extension.

Moni said: (Jul 19, 2012)  
In c++ it accept without .h extension but in c its possible ?

Lalit Kumawat said: (Sep 13, 2012)  
Without .h extension it is not possible to include the function like printf, scanf etc.

Jyoti Nagpal said: (Jun 14, 2013)  
#include"filename.c".

"this is also a header file".

In a extern concept, many different files can use same variable.

By using include the whole file.

That's all about it.

Thanks.

Ram Narayan Kumar said: (Jul 5, 2013)  
Header files only puts the contents whatever it have on the top of program so it may have any other extension.

Aditya said: (Jul 19, 2014)  
Header files can have any extension but it should be included with in C program with same extension.

We can define filename.c in header file.

Himani said: (Feb 26, 2015)  
Then why do all the predefined header files have ".h" as there extension.

Bhawna said: (Apr 16, 2015)  
Please elaborate the answer.

Aksahy said: (Jul 14, 2017)  
@Sundar.

It's NOT WORKS IN code blocks.

#include<stdio.hdr>
#include<conio.exe>
void main()
{
printf("Welcome to IndiaBIX.com !");
getch();
}

The output : Welcome to IndiaBIX.com!

Harish said: (Sep 18, 2017)  
@ALl.

vi hi.q
{
#include<stdio.h>
}

vi prog.c
#include"hi.q"
int main()
{
printf("in prog.c file\n");
}

cc hi.q prog.c
o/p error: file format not recognized
But if we use with hi.h, then it's working properly. so, I think the extension should be .h only.

Deepak said: (Jun 17, 2020)  
We include files into our source as follows:

In case of files placed in your _include_ dir
which you tell the compiler to look into
#include <filename.ext>

In case of other files, which are not in compiler's known include dir
#include "relative/path/to/filename.ext".

In either case, what the pre-possessor does is pretty simple. It reads the whole content of that file and replaces this line with *the whole content*. That's it. Nothing more, nothing less.

Any extension is fine as long as you keep text data into it which it could "include". Any extension and any text content should work.

Haven't given it a try, but if a binary format file (like an image) is included, the preprocessor will most likely read it as ASCII files and copy it too, and then complain about the grammar.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.