×

C Tutorial

C Basics

C Data Types

C Input/Output

C Operators

C Conditional Statements

C Control Statements

C Strings

C Functions

C Arrays

C Structure and Unions

C Pointers

C Preprocessor Directives

C Command-line Arguments

C File Handlings

C Graphics

C Advance Topics

C Tips and Tricks

C Important Topics

C Practice

Some basic rules of writing a C program

In this article, I am going to list some of the basic rules of writing a C program. I would strongly recommend following all these rules to code like a professional programmer. By IncludeHelp Last updated : December 15, 2023

Some basic rules of writing a C program

C program is the set of statement written in C programming language. Each program should follow the standards (rules) to make your code working everywhere.

1) Comments/Documentation

Comments are not required, but the comments are as important as code. So it is first rule that you should remember to comment/document the code properly. Read more: How to comment the code?

What you can document?

  • Purpose of the code.
  • Name of the programmer, reviewer, code writing, editing date etc.
  • Resources (inputs) that will be used to run the code.
  • Explain each logic, what is that for and why you used?
  • You can also write note for the developers, in which you can explain your logic/algorithm in details.
  • Each function declaration, definition should be documented.
  • Anything that you can explain, but there is no need to write unnecessary comments with the code that is already cleared. (Read more: Important rules to remember while writing C, C++ program).

2) Statement termination by semicolon (;)

Each statement which does not has its body (please note that, statement without its body) must be terminated by the semicolon (;).

The statements which should be terminated

Example

int a; //variable declarations

printf("Hello!"); // a print statement 

sum = findSum (10,20,30); // a function calling statement

//etc

The statements which should not be terminated

Example

#include <stdio.h> //hesder file include section

#define MAX_LEN 1024 //Macro definition statement

int main() //main function defining section
{
	//...
}

//function header with its definition
int findSum(int a, int b, int c) 
{
	return (a+b+c);
}

if(a > b) //if statement 
	large =a;
else
	large =b;

//etc

3) Tokens, identifiers related rules

You must be aware about the C language tokens to differentiate what is keyword, what is identifier etc.

Points to remember

Some of the points that you must know:

  • Keywords are the reserved words in the compilers and you cannot use them for other purposes.
  • C language is cases sensitive programming language. Here, small case identifiers and uppercase identifiers are different. So, if you have declared a variable with name Num then you must have to use Num everywhere. You cannot use num, NUM etc instead of Num.
  • Same as keyword, library functions are also declared in the header files and you must have to include header file in the program. For example, if you are using sqrt() function to find the square root of any number, you will have to include math.h header file.

4) Rules about identifier/variable declarations

  • Lowercase, uppercase alphabets, digits and underscore (_) are allowed to define an identifier (i.e. variable name, constant name, function name etc.).
  • Only an alphabet or underscore (_) can be used as first letter of the identifier.
  • White spaces and other special characters are not allowed in an identifier name, if you have two words in a variables/identifier name, you can use either underscore (_) to separate them or you can also write identifier name in camel case style.
    For example: if you want to declare a variable to store roll number, the variable name should be roll_number or rollNumber or anything like that but you cannot use white spaces.
  • Identifier must be meaningful and descriptive.

Read more: Identifier/variable name conventions in C language

5) Code indentation

I have deal with many programmers who just write the code and they don't care about the indentation, since indentation is not compulsory for the compiler. But it's most important while writing the program. An indented code can read, edit easily.

Example of indented code

#include <stdio.h>

int main() {
  int a;
  int b;
  int large;

  a = 10;
  b = 20;

  if (a > b) {
    large = a;
  } else {
    large = b;
  }

  printf("Largest number is: %d\n", large);

  return 0;
}

Example of un-indented code

#include <stdio.h>

int main()
{
int a;
int b;
int large;

a = 10;
b = 20;

if(a>b)
{
large = a;
}
else
{
large = b;
}

printf("Largest number is: %d\n",large);

return 0;
}

Search for C programming courses for beginners on classpert.com

C Language Tutorial »





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.