C Style coding standard: All you want to know about


Coding standard makes code writing cleaner and increases readability. C style is one such coding standard that standardizes the way C programs are written.

Every big organization follows this (or some other coding standard). You should start using this if you are an individual programmer right now. Lets understand what it is and what benefits you will get out of this.

What is C style in C programming and why should we use this?

Basically C style is a coding standard for programming languages such as C program or any other programming language. As a beginner when we start coding we never follow this or any kind of coding standard. But at work place or as part of industry standard (usually bigger companies with good CMM value) the C style is very much mandatory. On the other way nobody will force you to follow C style in smaller or startup organizations but as a coder its always a good idea to have clean code.

The C style coding standard basically makes the code look clean and easily understandable.

The coding standards define how the code in a program should be written. basically a coding standard guideline enhances the readability of a program.
Meaning:
– How should you start a new line for a statement?
– How to place braces for a bunch of lines of code?
– Should you be starting a line with some spaces or tabs? How many spaces or tabs?
– Should you indent the code after a conditional branch?

For example the following Wikipedia link illustrates how a while loop with braces can be placed in code.
Following link is a K&R style of writing C code.

Observe the below code snippet to understand C style:


    int main(int argc, char *argv[])
    {
        ...
        |
        v
         while (x == y) {
           |
           v
            something();
            somethingelse();
               |
               v
            if (some_error)
       -->      do_correct();
            else
                continue_as_usual();
        }

        finalthing();
        ...
    }

The while () starts after one tab and the code that is there inside the while further starts after another tab.
Further more the if else statements inside the while loop starts after one more tab as compared to the while() statements.

Now see the below code. It is the same code placed above.
does it look good at all ??

int main(int argc, char *argv[])
{
    ...
while (x==y) {
something();
somethingelse();
if (some_error)
do_correct();
else
continue_as_usual();
}
finalthing();
...
}

I think the basic understanding should be clear by now.
If you are more curious to explore more on this then the following link will help you which explains a more detailed C style coding standard guide.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.