0

I am doing my first "big/medium" project for a school work and I need to divide my code into some other c files. My doubt is if it is better have many files/header files with just a few little code, or have less files/header files and a little more code/functions into them?

Thank you!

p.s. I am a newbie programmer, so be patient and try to make the explanation easy to understand.

  • 2
    Take a look at the standard C library. Different headers have different sizes. Some are small (``, ``, ``, …) while some are big (``, ``, ``, ``, …). Group the declarations for related functions together in one header; group the declarations for unrelated functions in separate headers. Decide whether you'll go for 'one function per source file' or 'several functions per source file' or 'many functions per source file'. All have their merits. If you can get hold of Plauger's "The Standard C Library" (C90) book, read it. – Jonathan Leffler May 02 '17 at 18:44
  • Be aware that this is encroaching on the 'primarily opinion-based' reason for closing questions on SO. – Jonathan Leffler May 02 '17 at 18:45
  • There is a similar C++ [question here](http://stackoverflow.com/questions/531133/should-i-put-many-functions-into-one-file-or-more-or-less-one-function-per-fi). Some programmers advocate one function per file, but that seems extreme to me, I group functions by category - as does the C library. – Weather Vane May 02 '17 at 18:46

1 Answers1

5

My experience is that having code grouped into source/headers according to functionality increases ability to understand, test, maintain and reuse it.

How much code goes into each file will really depend on how complex the encapsulated functionality is. For example, I have a source file containing functions to create and append to WAV files. They are relatively small, and because they are cohesive, I can use them in whatever project I have that needs to create WAV files without bringing in a lot of other baggage. Other files may be large (or very large) but if the functionality is cohesive, I get the same benefits.

One thing that tripped me up when I started doing this was “multiple inclusions” caused by including the same header in a project multiple times without “protecting” it. Since you say you are a newbie, I’ll add a quick sample of what you can do to prevent it.

/**
   @file  my_header.h  
*/

ifndef  MY_HEADER_H // <- Prevents multiple inclusions
#define MY_HEADER_H // <- ...

#ifdef __cplusplus  // <- Allows this to be called from c++
extern "C" {        // <- See "name mangling for more info.
#endif              // <- ...

/**************************/
// your stuff goes here

struct my_struct
{
   // ...
};

// function prototypes, etc.

/**************************/
#ifdef __cplusplus
}
#endif

#endif // MY_HEADER_H
Justin J.
  • 396
  • 2
  • 12
  • In C, declarations may be repeated so long as they are identical. In my understanding the header [include guard](https://en.wikipedia.org/wiki/Include_guard) is there to prevent "header recursion". Actual definitions should not be in a header file. – Weather Vane May 02 '17 at 20:11
  • @WeatherVane : True, "header recursion" is a better description, and [function] declarations, defines, etc. may be repeated so long as they are identical. But if that struct is included again, it will cause an error. Just to be sure, I tried it on a gcc ARM tool chain and Visual Studio. gcc: error: redefinition of ‘struct wavfile_header’ Visual Studio: Error C2011 'wavfile_header': 'struct' type redefinition – Justin J. May 02 '17 at 20:31
  • That is because `struct my_struct { // ... };` is a variable definition and not a type declaration and should not be in the header file. – Weather Vane May 02 '17 at 20:35
  • 2
    My understanding is that a struct defines a type. In order for it to be a variable declaration you would have another line below with `struct my_struct variable_name;` to have a variable definition. – A. Blodgett May 02 '17 at 20:47
  • 1
    @A.Blodgett is right, it is a structure type definition, not a variable declaration – it does not create storage. I have it in the header so other entities can declare variables of that type. Whether or not this belongs in the header is off topic for this thread, but there are a lot of examples of it, for instance struct timeval in time.h. – Justin J. May 02 '17 at 21:18
  • @A.Blodgett you are right, perhaps a defect in MSVC which objects to `struct my_struct { int y; };` there twice. – Weather Vane May 02 '17 at 21:19