tags:

views:

207

answers:

6

In a C code, how you separate different sections of code, for example Implementation, Globals, etc? Is there a coding standard. I have seen many methods, but which one is preferred, I want to get community opinions on this..

/********************************************************* 
 *                        GLOBALS                        *
 *********************************************************/

or

/*
 * GLOBALS
 */

Or anything similar?

EDIT: Let me clarify, I am looking for preferred ways. Not standards actually. What have you seen in projects, or used yourself? Second point is that I like reading code, and if something is going to help you understand different sections while looking at the code through a limited console screen, I would add it to the standard. The layout of code is important as well as what it does. So the number one requirement is that such a separator must be easily noticeable while scrolling the source code very fast page-by-page.

+1  A: 

The standard is what you define it to be. The most important thing is to define a standard, then stick to it.

hexium
+3  A: 

There are a lot of arguments about code style, particularly about C, because of its flexibility. A great list of options can be found here.

The linux kernel style and the GNU style are pretty common ones. The most important thing is for your code to be self-consistent, no matter what you do. That goes for editing someone else's code too - even if you don't like their style choices, you have to play by their rules, to make future merging and modifications easier.

Carl Norum
+3  A: 

I never really saw a point in comments of that kind (another one often seen is similar headers for functions, which have nothing but function name). If you need to find a particular identifier, regardless of what it is, ctags - or, better yet, a decent IDE, can do that for you. What other value does such organization have?

Pavel Minaev
+1, but I think it's a bad example. It's more likely to be about grouping — functions dealing with this, functions dealing with that, etc.
Michael Krelin - hacker
A: 

There's no community coding standard per say, unless the community you're into enforces one. It is up to you and/or your team to make the decision as to what standard you want to adopt and see for yourselves the one you prefer.

There are however some standards that programmers tends to use without even noticing. Most of them can be found on this (rather old) page.

JP
+1  A: 

I'd use the second version; it's simpler, and is easier to maintain.

David Harris
+1  A: 

This sort of sectioning tends to be more important to beginners than to those experienced in reading C. There's a strong basic structure to most C source files:

  • Opening comment (project and file identification, copyright and licencing, basic explanation of what it contains).

  • Header files, possibly with some #define's (to choose a particular version of POSIX, for example).

  • Local type definitions, macros, etc. Also global variables, if you have any.

  • Local declarations (variables, static functions).

  • Functions in the file.

There can be variations, but declaring file static (or, worse, global) variables part way through the file is looked on as bad practice - ditto extra headers.

But there may not be anything formal separating those sections. Remember that box comments are harder to maintain - but section dividers seldom change so there isn't a lot to choose between them. I'd probably use a one-line comment to separate globals from what went before - if I used anything.

Jonathan Leffler