Basic Structure of a C program for Beginner

Posted by

Basic Structure of a C program for Beginner

Here in this article of PodinaTutorials.com, we will discuss a basic Structure of a C program for beginner. This article is mainly for new programmers, who want to learn C++ from scratch. So without wasting time, look the below article.

Below we are discussing Basic Structure of a C program for Beginner in detail.

Structure of a C++ program

First have a look to general syntax of a C++ program, which is given below.

Preprocessor directives
int main()
{
Body of the program
}

Each C++ program has three main components. These are: Preprocessor directives, main function, and body of the main function. Now, consider the following simple program example to explain these components.

#include <iostream.h>
#include <conio.h>

int main()
{
cout << “Hello World!”;
getch();
return ;
}

Output of the program

Hello World!

The result of the above program is that it prints “Hello World!” on the screen. It is one of the simplest programs that can be written in C++, but it contains the fundamental components of almost every C++ program.

What is Preprocessor directives (#include, #define)

A preprocessor is a collection of special statements which are executed before the compilation process.

Almost every C++ program contains a preprocessor directive. The #include preprocessor directives is commonly used to insert header files with extension ‘.h’. these header files are already stored in computer in include directory. In the above program, two #include directives have been used, #include<iostream.h> and #include<conio.h>. #include<iostream.h> is used for the C++ object cout and #include<conio.h> for the built-in function getch().

In C++, The other commonly used preprocessor directive is #define which is used to define symbolic constants. Its general format is:

#define identifier value

Forexample

#define PI 3.14159

#define NEWLINE ‘\n’

This defines two new constants: PI and NEWLINE. Once they are defined, they can be used in the rest of the program as if they were any other regular constant, for example:

#include <iostream.h>
#include <conio.h>
#define PI 3.14159
#define NEWLINE ‘\n’

int main()
{
double r=5.0; // radius
double circle;
circle = 2* PI * r;
cout << “Area of the Circle is equal to:”<<circle;
cout << NEWLINE;
getch();
return0;
}

Output of the program

Area of the Circle is equal to: 31.4159

The #define directive is not a C++ statement but a directive for the preprocessor, therefore it assumes the entire line as the directive and does not require a semicolon (;) at its end.

main() Function

the main function is the point by where all C++ programs start their execution. It doesn’t matter whether there are other functions with other names define before or after it, the instructions contained within this function’s definition will always be the first ones to be executed in any C++ program. For that same reason, it is essential that all C++ programs have a main function.

The word main is followed in the code by a pair of parentheses ().

Body of the main() Function

After the parentheses of the main function, the body of main function starts which is enclosed in curly braces {}. When this function is executed, the instructions written within it perform their task. Inside the body of main function, C++ statements are written.

C++ Statements

A C++ statement is a simple or compound expression that can actually produce some effect. It is actually an instruction to the computer for taking an action.

For example cout << “Hello World!”; is a C++ statement. The following are some examples of statements.

cout << “Hello World!”;

In fact, this statement performs the only action of displaying the result “Hello World!” on the screen.

cout is the name of the standard output stream in C++, and the meaning of the entire statement is to insert a sequence of characters (in this case the Hello World! Sequence of characters) into the standard output stream. cout is declared in the <iostream.h> standard header file.

getch();

this is a standard library function defined in the header file <conio.h> and is used to wait for the user to input a character from the keyboard.

return 0;

the return statement causes the main function to return control to the operating system. Return may be followed by a return code. The code 0 return by the return statement tells the operating system that the program terminates normally.

The program has been structured in different lines in order to be more readable, but in C++, we do not have strict rules on how to separate instructions in different lines. For example, the following program can also be written in a single line.

int main()
{
cout << “Hello World!”;
return 0;
}

Its single line version will be:

int main() { cout << “Hello World!”; return 0; }

Also Read  post a comment in c programming language and statement terminator

Conclusion

This article is all about Basic Structure of a C program for Beginner.Here in this article we discussed How C++ structure look alike and this is the easiest way to write your program in C++ programming language.

Basic Structure of a C program for Beginner
Basic Structure of a C program for Beginner

6 comments

  1. I’ve been absent for a while, but now I remember why I used to love this site. Thanks, I will try and check ack mpre frequently.

    How frequently you update your website?

  2. I have been browsing online more than three hours today, yet I never found any interesting article like yours.

Leave a Reply

Your email address will not be published. Required fields are marked *