why c is called as the top down approach?

C programming uses top down approach to solve a problem.

Top down approach starts with high-level design and ends with the low-level implementation.


In top down approach, we use following approach to solve any problem.

1.First we will make a high level design of a problem statement.

2.After that we will write the main function.

3.From the main function we will call the sub functions.

Generally, the large program is divided into small small sub functions(each function will do a specific task) which improves the modularity of a program.

Using that we can easily debug the program.

4.Later we will implement(code) all the sub functions based on requirements. Thats why c is called the top down approach.

Example

Write a c program to implement a simple calculator.




High-Level Design

declare two integers

scan the values from the user

call addition

call subtraction

call multiplication

call division

addition, subtraction, multiplication, division are sub functions.




write main function and call sub functions

int main()
{
    int a,b;

    scanf("%d%d", &a, &b);

    add(a,b);
    sub(a,b);
    mul(a,b);
    div(a,b);

    return 0;
}



Implement the sub functions

void add(int a, int b)
{
    printf("%d + %d = %d", a+b);
}

void sub(int a, int b)
{
    printf("%d - %d = %d", a-b);
}

void mul(int a, int b)
{
    printf("%d * %d = %d", a*b);
}

void div(int a, int b)
{
    if(b != 0) //division by zero is undefined.
        printf("%d / %d = %d", a/b);
}

Topics You Might Like