What is Cstyle programming and why should we use this?

Basically Cstyle 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 cstyle is very much mandatory. On the other way nobody will force you to follow cstyle in smaller or startup organizations but as a coder its always a good idea to have clean code.

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

What is Cstyle and why is it important

What is Cstyle and why is it important

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 cstyle:


    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 Cstyle coding standard guide.

Leave a Reply

Your email address will not be published. Required fields are marked *