Programming Style of C Language :-

 

The style of a C Language describes how to use various elements of C language to increase clarity, readability and understandability of c programs. Various elements that are included to describe c-style are:-

1.Names.

2.Spaces.

3.Line Breaks.

4.Comments.

5.Indentation.

6.Blank lines.

7.Braces.

1.Names: Use Meaningful names for File names. Variable names. Function names. If two (or) three words are used in names, combine them with Underscores. File name should have an extension .c. Function name should be terminated with()

Examples:

Sum_of_two_numbers.c. file1.c, calendar.c                  /* File names */

Area_of_circle, x. y. z                     /* variable names */

 

Print_address().read_data(), calc_interest()               /*Function names*/

2.Spaces :- use at least one space between two or three elements of a program, one or more spaces will be treated as a single space.

 For example:-

For 1=1 to 10

        S= a + b + c (or) s = a + b + c /* Both are treated as same*/

 

3. Line breaks :- Terminate each statement with a semicolon (;)

 

Ex. I=0;j=5; k=I+j; (Here these are 3 statements though they are written in a single line). To increase readability, include a line break at the end of each statement as shown below:-

I=0;

J=5;

K=i+j:

4. Comments :- Comments are used for documentation. These are ignored by c compiler. These are used only to increase the readability and understandability of a program. The comment in c should start with /* and can include one or more lines of text and it should end with */

If comment includes many lines then it is called as Multi line comment

Example:-

/*Program to find the sum of two numbers*/

Author: James

INDIA*/

If a comment includes a single line then it is called as a single line comment

Example:- The above multi line comments can be written as three single line comments as

 

/* Program to find the sum of two numbers */

/* Author: James* /

/* Andhra Pradesh */

 

Note: In Between the symbols /* and any thing written is treated as comment. With in the comment any spelling mistakes or any errors are not considered as errors by compiler.

5. Indentation :- Indentation is the number of spaces/tabs left over at the beginning of each line. It is used to increase the clarity and readability of the program.

Example:-

If (>0)

X = 4;

else

X=5;                      /* without indentation */

 

 

If (I >0)

X=4;

else

X=5;                      /* with indentation */

6. Blank lines :-Use blank lines in between two statements to increase the readability. Such as a blank line can be used to separate main function from user defined functions. Or declaration section and executable section can be separated by one or more blank line as shown below.

 

Int main()

 

{

Int x,y;                            // declaration section

P=0;                               // executable section

}

7. Braces :-{  and } can be used to write the compound statements and also to increase readability of the program.

Example:-

If (1>0)

{

X = 9;

}

else

{

X=10;

}

If more than one statement is to be used in if part or else part or within the body of any loop then they must be placed in between { and } as shown below Example.

If (i>0)

{

X = 7;

y=10;

}

else

{

P=1;

Q=6;

}

 

Comments

Post a Comment

Popular posts from this blog

Introduction Structure of C-Language

Structure of C-Language