Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Abcdeghjuvu hsjejkskksgshshjsj gsuejksje, Essays (university) of English Language

Fgjjbvchjjjj UF f hshshhsjej Bshsjsjjsjej hshsh

Typology: Essays (university)

2017/2018

Uploaded on 08/17/2018

unknown user
unknown user 🇮🇳

1 document

1 / 12

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
!
C"Coding"Convention"
C"Program"Style"Guide"
Sanjay&Vyas&
!
! !
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Abcdeghjuvu hsjejkskksgshshjsj gsuejksje and more Essays (university) English Language in PDF only on Docsity!

C Coding Convention

C Program Style Guide

Sanjay Vyas

  • Overview
  • Source File Organization
  • File Header Information
  • #include Section
  • Macro Definations
  • Global and Static Variables
  • Comments
  • Multi-­‐line Comments
  • Single-­‐line Comments
  • Identifier naming convention
  • Identifier Names
  • Indentation
  • Indenting blocks
  • Spacing
  • Spacing after keywords
  • Spacing in function calls................................................................................................................................

Source File Organization

File Header Information

Each source code file must have a comment block header, which states the

purpose of that file along with identification information.

Progam is the name of the physical file

Author is the name(s) of the programmer(s)

Description should give an overall synopsis of what the program does

Revision History tracks changes made to the file along with author name

  • Program: MagicSquare.c
  • Author: Sanjay Vyas (SV)
  • Description:
  • Magic Square generates a square where all rows, columns
  • and diagonals produce the same sum.
  • Revision History:
  • 1 - Jan- 2015 (SV): Project created.
  • 2 - Jan- 2015 (SV): Added basic logic for square.
  • 3 - Jan- 2015 (SV): Bug Fix: Invalid input caught correctly. ***********************************************************/

#include Section

The #includes should be right below the file header information and should

follow the order

1. System header files

2. User defined header files

  • There should NOT be a space between # and include.
  • There SHOULD be one space AFTER #include.
  • Separate system header files and user header file with one blank line. THESE ARE INCORRECT // Include header files #include <stdio.h> #include <stdlib.h> #include "MyMacros.h" // Include header files #include<stdio.h> // Keep a space after #include.

include <stdlib.h> // Do not keep a space between # and include.

include"MyMacros.h" // Leave a blank line after system includes.

Global and Static Variables

Global and static variables should be avoided as much as possible but if used,

they should be prefixed properly.

  • Global variables should be prefixed with g_ or global_
  • Static variables should be prefixed with s_ or static_ // Global and Static Variable int g_ListCount = 0 ; static int s_LastCounter = 0 ;

Comments

A program should be well-­‐documented in terms of explaining the workings of

the code, stating the purpose of different parts of the code and maintaining

revision history.

Multi-­‐line Comments

Multi-­‐line comments in C are enclosed using a pair of /* and */.

Multiline comments should be used for program header information, as stated

earlier. Multiline comments should also be used for functions headers and

explaining logic of piece of code.

FUNCTION HEADER MULTI-­‐LINE COMMENT

  • Function: GenerateSquare
  • Return Value: void
  • Parameters:
  • int size - An odd value of which the square is to be
  • generated.
  • Description: This function takes a positive odd value,
  • allocates memory on heap and then generates
  • a square where all rows, all columns and
  • both diagonal sum up to the same value. ***********************************************************/ void GenerateSquare(int size)

MULTI-­‐LINE COMMENT EXPLAINING LOGIC

// As we are generating a array of any given odd size, // we need to allocate it on the heap. // We will first allocate the rows array of pointer with size // and then allocate each row with size number of columns. extern int **g_square; g_square = (int **)malloc(size * sizeof(int *)); if (NULL == g_square) return 0 ; for (int i= 0 ; i<size; i++) { g_square[i]=(int *)calloc(size, sizeof(int)); if (NULL == g_square[i]) return 0 ; }

Identifier naming convention

Identifiers for variables, constants, macros, etc. should be named in a meaningful

manner. Very short names should be avoided, e.g., aaa, abc, n, m, l.

Identifiers, while being crisp, should reflect the purpose of the variable or

function.

Identifier Names int GenerateSquare(int size) { extern int **g_square; int i; // Loop variable for memory allocation. int value; // Value variable for filling up the square.

  • Indetifier name should indicate the purpose.
  • Avoid very short names, except in well known cases like i and j. THESE ARE INCORRECT int fun(int a, const char * v[]) { int aaa; int pnm; int l,m,n;

Indentation

Code should be well indented to make it more readable. Consistent indentation

should be followed thruout the code and the indentations should be made using

the tab key instead of multiple spaces.

Indenting blocks

INDENTING NESTED STATEMENTS

if (NULL == g_square) return 0 ; for (int i= 0 ; i<size; i++) { g_square[i]=(int *)calloc(size, sizeof(int)); if (NULL == g_square[i]) return 0 ; }

  • Blocks { } should be aligned one below the other.
  • For every level, indent with one TAB (4 is usually the standard). THESE ARE INCORRECT for (value= 0 ; value<sqr(size); value++) { g_square[row][col]=value; row--; col++; if (row < 0 && col>=size) { row=size- 1 ; col= 0 ; } else if (row < 0 ) row=size- 1 ; else if (col == size) col= 0 ;