Search code examples
cfunctionlinkerfunction-declarationfunction-definition

Should every function in C have its own .c file ? or can i put all the functions of my program in one .c file?


I'm new to C programming and I just started studying functions. Should I put each function in a .c and .h file, or can I put all of the functions and headers in one .c and .h file?


Solution

  • No! There is no need to do it at all. That would be affect your performance really badly.

    Usually we only tend to create new files .h to store functions/methods that we want to use repeatedly in other projects or programs I guess.

    Your program should be as follows:

    Let's call this prog1.c

    #include <only libs that you need>
    
        void functionA (parameterType parameter){
            //Whatever your function needs to do
        }
        int main(){
           //this is your main function and where the program will start working.
           functionA(parameters) /*Here is how you call functionA if it's void giving it's needed parameters.*/
        }
    

    Later on, with more knowledge you'll learn when you need to store or not the functions in others files to learn and keep it organized. There's no need for that now. You should focus the most on learning how they work and communicate with each other.

    If you need to use functionA in other file, well then you will just need to include that .c file, like this:

    In prog2.c you start it by calling out

    #include "prog1.c"