C Programming Style

C Programming Coding Styles is very important to protect software from theft and plagiarism. As C is Procedure Oriented Programming Language we can write continuous functions / coding statements but we need to make the code maintainable. The person who reads the code later should be capable of understanding and modifying it, so we follow generic coding style as explained below

First Comment includes the Code Copyright information its an open source code or the copyright protected we need to include who has the right to reuse the code, warranty of code and other licensing software that you provide and company email address.

/*
 * Copyright (c) COMPANY_NAME Software.  All rights Reserved.
 *
 *
 *
 */


Second Comment includes the name of module, module abstract and module developer name with email address.

/*
 * Module Name:
 *
 *        errno.c
 *
 * Abstract:
 *
 *        Linux errno codes
 *
 * Authors: Developer Name (developer@yourcompany.com)
 *
 */


Header Files This is the start of the file contents that includes Standard and User defined Header Files

#include <stdio.h>

Global Struct and Typedef Variables need to define that program uses

Static Include Static Variables and Static Functions.

/*******************************************************************
* function name foo()
*
* Description of the function purpose printf foo and returns zero
*******************************************************************/
int foo();

...


Main Function main() function is the program where execution starts as given below the function starts with open flower brace "{" from the next line on-wards we provide a space width of 4 spaces and start the function statements. Also if there is some conditional statements like if, for while and switch we follow the same. For each start of the conditional statement we provide a spacing of plus 4 and start the conditional statements that need to be executed. Function ends with closing flower braces "}".

/*******************************************************************
* main()
* Starts the process and executes function foo()
*******************************************************************/
int main() {

    // main function statements starts with tab-width: 4 spaces
    printf("%s\n","In Function main()");
    ...

    if ( condition ) {
        // if module statements starts with tab-width: 8 spaces
    }

    ...
    foo();
    ...

    return value;
}

...


User Defined Function foo() function is user defined function where we follow same process while writing it as we did it in for main() function. Function starts with open flower brace "{" from the next line on-wards we provide a space width of 4 spaces and start the function statements. Also if there is some conditional statements like if, for while and switch we follow the same. For each start of the conditional statement we provide a spacing of plus 4 and start the conditional statements that need to be executed. Function ends with closing flower braces "}".

int foo() {

    //Your function contents starts here
    printf("%s\n","In Function foo()");
    ...

    return 0;
}

Post a Comment

0 Comments