How do you order your include statement

This is a discussion on How do you order your include statement within the C Programming forums, part of the General Programming Boards category; To All: I learned C when it was K&R C and I ordered by headers from specific to the general. ...

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    2,610

    How do you order your include statement

    To All:

    I learned C when it was K&R C and I ordered by headers from specific to the general.
    I am still working on learning C++ in my free time.

    Example in mymodule.c

    Code:
    #include "mymodule.h"
    #include "mylibrary.h"
    #include <3rdpartylibrary.h>
    #include <stdio.h>
    But, an C++ project I use/submit patches normally use general to the specific.

    Example in thisclassmodule.cpp

    Code:
    #include <iostream>
    #include <3rdpartyclass.h>
    #include "myclass.h"
    #include "thisclassmodule.h"
    Is this just an C++ thing?
    Is it recommend to use either order in C?

    Tim S.

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    I tend to include the specific ones on top as well. An include is just a copy paste of that file so it doesn't really matter I guess as long as you have guards to avoid conflicts.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by claudiu View Post
    I tend to include the specific ones on top as well. An include is just a copy paste of that file so it doesn't really matter I guess as long as you have guards to avoid conflicts.
    I usually include in this order:
    1. C++ std headers (if applicable)
    2. standard C headers
    3. Blank line
    4. Header file related to this source file (if applicable)
    5. Other header files

    But whatever you prefer...

  4. #4
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094

    Thumbs up

    Quote Originally Posted by EVOEx View Post
    I usually include in this order:
    1. C++ std headers (if applicable)
    2. standard C headers
    3. Blank line
    4. Header file related to this source file (if applicable)
    5. Other header files

    But whatever you prefer...
    Yes, the line break between categories is definitely a good idea!
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,590
    I tend to go for this order - start with the broadest standard first, with increasing levels of specialisation.

    ISO header files (stdio.h and the like)
    POSIX headers (if applicable)
    OS headers
    3rd party library headers
    Local library headers
    Local module headers
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    2,610
    Thank You All for your replies.

    I will likely use an order somewhat-like Salem's order for my next project.
    My current project has no real header order to it; it is using a C-Like "Rabbit Dynamic C" Compiler that does not support headers in a normal manner.
    My last large C project before that was a student project in 1998.

    Tim S.
    Last edited by stahta01; 09-29-2010 at 02:11 PM.

  7. #7
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,246
    The theory behind the general-to-specific order is that more specific header files may override certain preprocessor definitions from the more general header files.

    It's usually a bad idea to override definitions from a different header, but in a large project with many of your own headers it sometimes happens.

    That said, it's best if your code has no dependency on header inclusion order.

    Personally, I include C++ headers, then any C headers, then any platform headers. My own headers I include in alphabetical order.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  8. #8
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    I don't pay too much attention to the order, I just put filenames wrapped in double-quotes near the bottom.

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by stahta01 View Post
    To All:
    I learned C when it was K&R C and I ordered by headers from specific to the general.
    I am still working on learning C++ in my free time.
    Interesting answers so far....

    I tend to look at what needs something that's in something else (dependencies)

    For example, if I declare a project header that uses typedefs from windows, if I put it before the <windows.h> header I'm going to get a gazillion errors. So for me it's about having things in place before they're needed... same way I order my functions above Main or WinMain...

    I suppose this could be called "general to specific" but it's really about "planted before harvested"...

  10. #10
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by CommonTater View Post
    I tend to look at what needs something that's in something else (dependencies)

    For example, if I declare a project header that uses typedefs from windows, if I put it before the <windows.h> header I'm going to get a gazillion errors. So for me it's about having things in place before they're needed... same way I order my functions above Main or WinMain...
    Then that other header file should #include <windows.h>, you shouldn't write code that depends on the order of the #include statements.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  11. #11
    Registered User
    Join Date
    Jul 2007
    Posts
    131
    Quote Originally Posted by Salem View Post
    I tend to go for this order - start with the broadest standard first, with increasing levels of specialisation.

    ISO header files (stdio.h and the like)
    POSIX headers (if applicable)
    OS headers
    3rd party library headers
    Local library headers
    Local module headers
    I use this with exception that sys/ headers are on top.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 08-16-2010, 10:00 AM
  2. "Undefined Symbol" when compiling a socket program?
    By phummon in forum C++ Programming
    Replies: 4
    Last Post: 07-07-2010, 01:03 PM
  3. does not name a type ERROR
    By DarrenY in forum C++ Programming
    Replies: 3
    Last Post: 10-13-2007, 04:54 AM
  4. process programming
    By St0rM-MaN in forum Linux Programming
    Replies: 2
    Last Post: 09-15-2007, 07:53 AM
  5. Dialog Will not appear?
    By curlious in forum Windows Programming
    Replies: 1
    Last Post: 10-20-2003, 10:32 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21