A Look into Sample C Program

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

There are six steps in the Sample C Program

Sample C Program Structure

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

1) Problem Description    2) Pre-processor

3) Definition                       4) Global Declaration

5) Main Function and       6) 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.”

Giving a name to C Program

Pre-processor

What is preprocessor directive in C Program?

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

Definition

It is an optional section. One can create any constant in the definition section and inherit its value into the program. For example, if the programmer wants to use the constant pi (pronounced as pi), for which 3.14 is its value, it can be included here. In addition, the define section allows one to use constants inside the code while replacing the constant with its value. We use the #define in C programming language to create the constants. Where “#” in C language denotes the symbol for the pre-processor directive. By including #define, we can use constants within the code.

C Program Basics

Global Declaration

Global Declaration in C Program

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.

The Main Method

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() does not necessitate returning a value, int main() guides the function to return the value in integer form. Hence, during int declaration, it is mandatory to include return 0.

Main Method in C Program

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 flower brackets instead of a semicolon.

Discover more from BerylSoft

Subscribe now to keep reading and get access to the full archive.

Continue reading