Thread: C Best Practices Help

  1. #1
    Old Fashioned
    Join Date
    Nov 2016
    Posts
    137

    C Best Practices Help

    Does anybody have any resources for learning C programming best practices?

    I know these can certainly be subjective, but I am sure there are some things that are generally considered "safer" or better to do than others.
    I am already a software developer who has spent most of his time in C#.NET/JavaScript (Mainly C# though...Please don't judge!) so I am not having as hard of a time wrapping my head around the syntax as I am with the thought/design process that is not objected-oriented and also just sort of "How things are done" in C. This is much harder to pick up at first and also not taught in most books, which tend to focus on syntax with a few basic examples that don't necessarily follow good coding practices (use a lot of global variables, don't use header files a at all, etc...)

    That said, could you point me toward any reputable style guides or general best practices when it comes to C? It is very hard to wrap my head around some of the concepts coming from an entirely OOP world where I thought of everything first in terms of classes and encapsulating everything/programming against interfaces. I feel like a fish out of water, despite actually loving the experience of C.

    Also, if there are any known C programming related blogs or other online resources/communities you can recommend, that would be awesome. I am trying to get into the "C World" so-to-speak and it can be a bit tricky compared to trying to jump into something like PHP/JavaScript which hogs the heck out of all the "learn to code" communities, lol.

    Also, in case anyone is curious, I've been working through C All-In-One Desk Reference for Dummies by Sam Gookin and I also have the Learn C in 21 Days book to get the syntax stuff down. Currently messing with Linked Lists but there's definitely room for improvement as far as me getting the pointer/struct stuff down.

    Thanks!
    Last edited by Asymptotic; 12-01-2016 at 12:06 AM.

  2. #2
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Get yourself the online K&R C (second edition) PDF online and have a read through for starters. As for practices - that is subjective from author to author and what "they" consider to be best practices. i.e:

    Code:
    scanf("%s", &x);
    or

    Code:
    scanf("%23s", &x);
    Both work as far as the compiler is concerned, but then you have Microsoft's own variations of their "decepracated" old C functions. Even though they do fully conform to the old C89 standard. One of the best pieces of advise is to get a good book - one that is highly rated and well received. C Primer (fifth or sixth edition) are fantastic - and cover a lot of the language in great detail - they also give you pitfalls to avoid and certain errors that can appear. Overall though, the best code is one that is correct, well written and well documented with good indentation. Nobody can write what everyone would consider "the best code" - so keep trying new things from different angles and see what fits together.

    Welcome to cboard

    Ada x
    Double Helix STL

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Design Principles, Best Practices and Design Patterns for C (or Procedural Programming in general)? - Stack Overflow
    C Programming Language

    You can do 'OO-lite' in C quite easily if that's something you're comfortable with.

    Also, C forces you to do a lot more thinking up front as to how you're going to solve the problem. If you're provided with a set of interfaces already defined, the problem is constrained to begin with. But when you've got to come up with your own interfaces in the first place, you have a lot more freedom to make things exactly how you want (and a lot more freedom to make a mess of it at the same time).
    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.

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by Asymptotic View Post
    Also, if there are any known C programming related blogs or other online resources/communities you can recommend, that would be awesome. I am trying to get into the "C World" so-to-speak and it can be a bit tricky compared to trying to jump into something like PHP/JavaScript which hogs the heck out of all the "learn to code" communities, lol.
    In addition to the advice already received, I would recommend you spend some time around here. Most threads are created by beginners and novices, so the regulars are often suggesting common idioms, improvements, and best practices.

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Case in point:

    Quote Originally Posted by swgh View Post
    Code:
    scanf("%s", &x);
    or

    Code:
    scanf("%23s", &x);
    If "x" is a char array, then you don't need the '&' there.

  6. #6
    Old Fashioned
    Join Date
    Nov 2016
    Posts
    137
    Awesome. Thanks for the info and resources!

    C is so much fun! I really wish I had started with C to begin with but it is what it is... At least I'll know what's on the other side now.

    By the way, what is everyone's favorite applications written in C?

  7. #7
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Quote Originally Posted by Asymptotic View Post
    Awesome. Thanks for the info and resources!

    C is so much fun! I really wish I had started with C to begin with but it is what it is... At least I'll know what's on the other side now.

    By the way, what is everyone's favorite applications written in C?
    Varies from person to person. I would probably personally say the Quake Engine is very impressive - as is the entire UNIX Operating System.
    Double Helix STL

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bad practices or good?
    By shivam1992 in forum C++ Programming
    Replies: 4
    Last Post: 03-04-2013, 03:15 PM
  2. getchar() best practices
    By albundy in forum C Programming
    Replies: 2
    Last Post: 09-03-2011, 05:55 AM
  3. Best Practices For Includes
    By valaris in forum C++ Programming
    Replies: 13
    Last Post: 03-09-2009, 03:12 AM
  4. coding practices
    By pobri19 in forum C++ Programming
    Replies: 2
    Last Post: 07-17-2008, 04:56 PM
  5. deque best practices
    By George2 in forum C++ Programming
    Replies: 10
    Last Post: 03-02-2008, 08:11 PM

Tags for this Thread