Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

is header file must in c programming?

+14 votes
asked Mar 29 by Sai kiran Mamidi (220 points)

6 Answers

0 votes
answered Mar 30 by sk1a345 (140 points)
Yes if you want that your code should run efficiently without giving any error then it is required to include header files. For example #include<stdio.h> this header file contains the meaning of the predefined functions like printf() and scanf() in c. unless and untill you are not adding this header file you cannot use the predefined functions in your code.
0 votes
answered Mar 30 by Mayur Gaikwad (250 points)
It is possible to write code not using is header files but using header files is more reliable  than not using.
0 votes
answered Mar 31 by Kritika (140 points)
yes header file is must in c programming
0 votes
answered Apr 1 by Alex (140 points)
Depends on what you mean.

If you mean needing to link a header file, technically no but you probably wouldn't. For starters, without any includes you need to be proficient in your systems assembly to log anything into the terminal.

If you mean needing to make your header files, while it is not something you are forced to do, it is a good idea if you are making a somewhat large project where putting everything into one file may not be very practical.
+1 vote
answered Apr 1 by Peter Minarik (86,240 points)
edited Apr 16 by Peter Minarik

No, it's not. However, they exist for convenience and to help deal with larger projects so code wouldn't need to be copied over and over.

You can include header files provided by others, e.g. <stdio.h> for standard input/output operations, and you can also make your own header files if you want to share types across multiple files or you'd like to offer functionality for 3rd parties (e.g. you're creating an API).

It is highly recommended to create header files for anything more than a single file project.

Update

I've seen some people saying you need stdio.h or other headers. This is not true. They exist for your convenience, but you do not absolutely need to use them. You can just pick the function declarations from those headers that you would actually use in your code. They work because the header file contains just the declarations, but the definitions live somewhere else and they will be linked against your compiled project automatically (at least the standard library functions).

Here's an illustration below. Enjoy!

extern int printf(const char * format, ...);
extern int scanf(const char * format, ...);

int main()
{
    printf("Did you know you do not absoutely need to include stdio.h?\n");
    scanf("%*s");
    printf("Isn't this amazing?!");

    return 0;
}
0 votes
answered Apr 10 by 19G01A0488 Saikiran (140 points)
printf and scanf functions are there in  header files only i you want to use them you have to include header file
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...