A Look into Sample C Program

A sample C program is taken in this lesson, to discuss the different steps.

The Sample C program can be divided into six prominent steps, they include,

  • Problem Description
  • Pre-processor
  • Definition
  • Global Declaration
  • Main Function and
  • Function Definition

Problem Description

The first step of sample C program includes the problem description or giving a name to the program, and here, we explain the purpose of writing the program by providing the program’s title. Ex: In this program, the descriptive part is titled, “Addition of Two Numbers and Calculate the Area of a Circle”

Sample C Program

/* Addition of Two Numbers and Calculate the Area of a Circle */

In C, the problem description is usually written as a comment at the top. However, the compiler ignores comments.


/* 1. PROBLEM DESCRIPTION
   Program to add two numbers and calculate
   the area of a circle.
*/


/* 2. PREPROCESSOR DIRECTIVE */
#include <stdio.h>


/* 3. DEFINITION */
#define PIE 3.14


/* 4. GLOBAL DECLARATION */
int add(int x, int y);


/* 5. MAIN FUNCTION */
int main()
{
    int num1, num2, sum;
    float radius, area;

    printf("Enter two numbers: ");
    scanf("%d %d", &num1, &num2);

    sum = add(num1, num2);

    printf("Sum = %d\n", sum);

    printf("Enter the radius of the circle: ");
    scanf("%f", &radius);

    area = PIE * radius * radius;

    printf("Area of the circle = %.2f\n", area);

    return 0;
}


/* 6. FUNCTION DEFINITION */
int add(int x, int y)
{
    return x + y;
}

Output

Enter two numbers: 20 30
Sum = 50
Enter the radius of the circle: 10
Area of the circle = 314.00

Pre-processor

#include <stdio.h>

In this second session, the pre-processor section, we include the header file using #include <stdio.h>. This is known as a pre-processor directive.

Definition

This section is optional. It uses #define to create a symbolic constant. For example, #define PI 3.14 makes the preprocessor replace every PI in the code with 3.14 before compilation. In addition, the define section allows one to use constants inside the code while replacing the constant with its value. Where “#” in C language denotes the symbol for the pre-processor directive. By including #define, we can use constants within the code.

Global Declaration

int add(int x, int y);

In the global declaration section, the programmer can include all the variables used in the program. By declaring the variables in this section, those variables are made accessible across every program function.

Here, int specifies the return type; add is the function name, and int x and int y are arguments. Thus, global declaration allows the compiler to know about the type of the function before it is called.

The Main Method: An Explanation in Sample C Program

The main method is the standard from where the program starts executing, irrespective of any programming language like C, Java, dot net, or any other language. As a result, main() is a function that will appear in the C programming language every time, and from here, the program starts to execute. The main method also helps allocate space in the memory, which is helpful during program execution.

Importantly, the main() function consists of either void or main as a return type, but not necessarily int alone. There are different ways to express the main method. Some basic types are void main() and int main(). While void main() is non-standard in hosted C, int main() declares main() with an integer return type. The main() function returns an integer status to the operating system. Explicitly including return 0; indicates successful program termination, although reaching the closing brace of main() in modern C has the same effect.


#include <stdio.h>

int main()
{
    int a, b, c;

    printf("Enter the values of b and c: ");
    scanf("%d %d", &b, &c);

    a = add(b, c);  // calling

    printf("Sum of the given numbers is %d", a);

    return 0;
}

Function Definition

While in the function definition, we don’t use a semicolon, in the function declaration, we use a semicolon.

 In the function definition section, we give the opening and closing of braces {} instead of a semicolon.

int add(int x, int y)
{
return (x + y);
}

Leave a Comment