How to write a C program: Step by step guideline

In this article, you will learn about how to write a C program with our step by step guidelines. Till now we have learned various concepts of C programming and are ready to write programs of modest complexity or develop projects.

This is our comprehensive step by step guidelines about how to write a C program.

How to write a C program?

how to write a c program : step by step guidelines

Martin Fowler once said:

“ Any fool can write code that a computer can understand. Good programmers write code that humans can understand. ”

A programmer can write complex codes to solve the task but only a brilliant programmer write programs that can be interpreted by others and can be maintained if any errors come down the line.

Therefore, before writing complex programs, it is a good idea to follow some programming techniques because well-written programs possess several traits.

  • Well-written and indented programs are easier to understand, debug or maintain.
  • Well-written and indented programs are designed to efficient, flexible and robust.

So, how should I begin to write code and what steps should I follow then?

Don’t worry!

In this article, We will guide you through important stages of program development that will possess above characteristics.

The major steps are:

  • Program Design
  • Program Coding
  • Program testing and debugging

How to write C program

Program Design

Program design is the foundation that is at the heart of program development cycle.

Before getting your hands on the source code, it is crucial to understand the program from all sides and develop program development strategy.

You can break down program design into four steps:

  1. Problem Analysis
  2. Generating the program structure or blue print
  3. Algorithm development of the program
  4. Proper selection of control statements

problem analysis

Problem Analysis

The first step is to get the clear idea of the problem and what you want the program to do. So ask yourself the following questions:

  • What will be the input of your program?
  • What will be the output?
  • What are the different conditions that will be applied during the program?

Generating the program structure or blue print

Once you have got the answers to the above questions, you should decide how to accomplish the task.

How should the program be organized?

Since C is a structured language it follows the top-down approach that means break the whole problem into smaller tasks. Now it is easier to code smaller subtasks.

This will produce a readable and modular code which is very important for others to understand your code.

Algorithm development of the program

This is the important step of the program design. Once you have got the overall idea and broke the problem into smaller subtasks, it’s time to scratch details and step by step procedure, that is known as Algorithm in programming.

This is also known as pseudocode.

After the development of the algorithm, you should draw flowchart, pictorial representation of the algorithm.

I already mentioned this as an important step because there are several ways to solve the problem and it’s up to you which way you are going.

For example, for sorting an array you can use bubble sort, insertion sort or selection sort.

Proper selection of control statements

In a complex C program or project, you will have to use a huge number of control statements such as if ... else, while or for to direct the flow of execution.

Any algorithm or decision making can be accomplished using sequence structure, selection structure, and looping structure. Use proper control statements that will make your code easy and error-free.

Choosing a good way to represent the information can often make designing the program easy and simple.

Try avoiding the use of goto statements.

Program Coding

Once you have developed the full blue print of the program, it’s time to write source code according to the algorithm. That means be ready to translate your flowchart into a set of instruction.

It’s time to use your knowledge of C programming to work and try to write your code as simple as you can.

Following are the elements of program coding:

  • Proper documentation or commenting
  • Proper way for construction of statements
  • Clear input/output formats
  • Generality of the program

c program coding

Proper documentation or commenting

The main goal of this step is to make code readable and understandable to others.

Selection of meaningful variable name and proper use of comments are an integral part of this step. Comments are necessary, but they must be good comments.

Let us see the example:

int main(){
int i,j,a;
printf("enter two values:");
return 0;

Although above program is syntactically correct, it can be written with proper variable names which is more meaningful.

int main()
   int length, breadth;
   int area;
   printf("Enter length and breadth: ");
   scanf("%d %d", &length, &breadth);
   //calculation of area
   area = length * breadth;
   printf("Area: %d", area);
   return 0;

Proper way for construction of statements

While writing code, your statement should be simple and direct with proper indentation.

  • Try to use one statement per line.
  • Use parentheses and proper indentation
  • Use simple loopings and avoid heavy nesting of loops

Observe following coding styles:


Although above program is syntactically correct, it can be written with proper indentation and commenting.

if( mark >= 40)
   result = pass;
   result = fail;

Both codes are syntactically right but consider proper way of statement construction.

Clear input/output formats

The user should understand what to input and output of the program.

Always mention input requests clearly and label all outputs.

printf("Enter length of the square: ");
scanf("%d", &length);

Area = length * length;

printf("Area of the square: %d", area);

Generality of the program

We should always try to minimize the dependence of the program on particular data.

For example: While defining the size of an array use macro so that it’s easy for the programmer to change the size later in the program.

#define SIZE 10
int array[ SIZE ]:

Program testing and debugging

In this phase, you will review your source code for error checking.

Errors may be:

  • Syntax errors
  • Run-time errors
  • Logical errors
  • Latent errors

types of errors

Compiler helps to detect syntax and semantic errors. It is a computer program that converts source code into executable code or machine language.

Please check out the typical development of C program for detail understanding.

The compiler is unable to detect run-time and logical errors, so there is need of human testing as well.

Debugging means correcting the programming errors. There are different methods of debugging a source code.

  • Place printf statements at various steps to see the values of variable or output. This helps to identify an error.
  • Another way is the process of deduction. Eliminate possible error code from the program.
  • Many coder use method called backtrack for debugging. In this method source code is tracked backward until the error is detected.

Program Efficiency

Program efficiency relates to the execution time of the program and memory consumption.

If you follow all of the above-mentioned techniques, your program will be efficient as well as effective.

Last but not least, following are some of the tips to improve program efficiency.

  • Be wise to select a simple and effective algorithm, arithmetic and logical expressions.
  • Declare arrays and string with appropriate size and if possible, avoid the use of the multidimensional array.
  • Keep pointer in your priority list while dealing with arrays and string.