0% found this document useful (0 votes)
1 views6 pages

Coding Standards

Uploaded by

kgautam21080410
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views6 pages

Coding Standards

Uploaded by

kgautam21080410
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Why Follow Standards and Guidelines in C?

Ensures Readability and Maintainability

When code follows proper standards:

 It is easy to read

 Others can understand quickly

 It is easier to modify later

👉 Example:

❌ Poor style:

int a,b;main(){scanf("%d%d",&a,&b);printf("%d",a+b);}

✅ Good style:

#include <stdio.h>

int main() {

int num1, num2;

scanf("%d %d", &num1, &num2);

printf("%d", num1 + num2);

return 0;

Second program is much clearer.

Reduces Debugging and Development Time

When multiple developers work on the same project:

 Standard naming avoids confusion

 Proper indentation reduces mistakes

 Easy to find errors

In large software (like operating systems), thousands of programmers


work together. If everyone writes differently, it becomes messy.
Example:
The Linux kernel follows strict coding standards so that contributors
worldwide can work smoothly.

Promotes Consistency

Standards ensure:

 Same indentation style

 Same naming rules

 Same commenting style

This makes the program look professional and organized.

Improves Runtime and Security

Following standards helps:

 Prevent buffer overflow errors

 Improve memory handling

Many security issues happen because programmers ignore safe coding


practices.

DOCUMENTATION
1) Add Clear, Concise Comments for
Complex Logic
Comments explain why something is done (not
just what is done).
Example
// Calculate factorial using recursion
int factorial(int n) {
if (n == 0)
return 1;
return n * factorial(n - 1);
}
✔ Comment explains the logic clearly.
❌ Do not write unnecessary comments like:
int a; // declare integer a
That is obvious.

2) Include File Headers


At the top of each file, include:
 Purpose of program
 Author name
 Date
 Version

Example
/*
* File Name: student.c
* Purpose: To calculate student average marks
* Author: Vidhya K
* Date: 18-02-2026
* Version: 1.0
*/
This helps in:
 Large projects
 Team development
 Version tracking

Maintain External Documentation


For big projects, maintain separate documents
like:
 User manual
 Design document
 API documentation
Large systems like the Linux kernel maintain
extensive documentation so developers
worldwide can understand the system.

✅ Header File Practices in C


Header files (.h) contain:
 Function declarations
 Macro definitions
 Structure definitions

Use Include Guards


Include guards prevent multiple inclusion errors.
❌ Problem Without Include Guards
If a header file is included twice:
→ Compiler error
→ Redefinition problem

How it works:
 #ifndef → If not defined
 #define → Define it
 #endif → End condition
So file is included only once.

Avoidance of Undefined Behaviour (UB)


🔹 What is Undefined Behaviour?
Undefined Behaviour means:
The C language does not define what will
happen.
The program may crash, give wrong output, or
behave unpredictably.
The compiler is free to do anything.

🔹 Examples of Undefined Behaviour


1️⃣ Out-of-Bounds Array Access
int arr[3] = {1, 2, 3};
printf("%d", arr[5]); // ❌ Undefined behaviour
Why?
Array size is 3 → valid index: 0,1,2
Accessing index 5 → memory outside array.

2️⃣ Null Pointer Dereference


int *ptr = NULL;
printf("%d", *ptr); // ❌ Undefined behaviour
Dereferencing NULL may crash the program.

🔹 How to Avoid Undefined Behaviour?


✔ Always check array bounds
✔ Check pointers before using
✔ Initialize variables
✔ Avoid division by zero

You might also like