Page Content

Tutorials

What Is The Basic Structure Of C++ Program?

You must understand C++’s main() function, #include directives, and comments to develop a functioning application. These components are necessary for C++ program development and administration.

Structure of C++ Program

Include files, class declarations, class function definitions, and the main() function program are common components that make up a C++ program.

The Function

Any C++ program’s entry point is the main() function. When you run a C++ program, the operating system calls main() to start the program. There must be exactly one global main() function in every C++ application that complies with standards.

A return type, a function name, a (potentially empty) parameter list contained in parenthesis, and a function body are the four standard components of a function definition, which includes main(). Curly braces {}, which indicate a code block, enclose the function body.

Usually, the operating system receives an int value from the main() function. Any non-zero return value denotes a failure, whereas a return value of 0 typically indicates a successful program completion. In C++, an implicit return 0; is automatically added once control reaches the end of main(), and a value of 0 is presumed if main() does not explicitly return a value.

Two parameters, int argc and char* argv[], can also be used to pass command-line arguments to the main() method. The number of arguments, including the program’s name, which is always argv, is specified by argc (argument count). Because user code cannot call main(), it is unique in C++ and cannot be recursive either directly or indirectly.

Example of main() Function:

#include <iostream> // Includes declarations for standard I/O facilities 
int main() { // main function, program execution begins here
    std::cout << "Hello, world!" << std::endl; // Output statement 
    return 0; // Indicates successful execution 
}

Output:

Hello, world!

Directives

Hash signs (#) are used to indicate preprocessor directives. These directives are processed by the compiler’s preprocessor prior to the compilation process starting.

The preprocessor is told to transfer the contents of the specified file into the source file at that place via the #include directive. This is important because header files usually contain declarations that your program needs, such function prototypes, class definitions, and macros.

#include can be used to specify files in two main ways:

Angle brackets (<>): Utilized for common library headers, such as . Usually an include folder included with the C++ compiler, the compiler looks for these files in standard folders. Typically, standard library headers lack a file extension.

Double quotes (“”): Used for header files that are user-defined, like “myheader.h” Usually, the compiler looks for these files in standard directories after first looking in the directory of the current source file. The.h,.hpp, or.hxx extensions are typically used for user-defined header files.

#include directives are often found at the start of the source file, outside of any function.

Example of #include Directives:

// main.cpp
#include <iostream>  // Includes standard I/O library declarations
#include "my_utility.h" // Includes a user-defined header file
int main() {
    // ... program logic using functions/declarations from included files
    return 0;
}

C++ Comments ( and )

The compiler ignores comments, which are illustrative statements appended to the source code. They serve to clarify the logic and aim of the program for human readers.

There are two primary comment syntax types supported by C++:

Single-line comments (//): The double slash (//) marks the beginning of these remarks, which go on until the line’s end. On the queue, they can start wherever. When a // comment line ends with a backslash (), the comment will continue on the following line.

Multi-line comments (/* */): C-style comments, also referred to as multi-line comments (/* /), start with / and finish with /. Line breaks and everything else between these two symbols are regarded as comments. It is crucial to remember that / / comments do not nest; compiler errors may result from an unclosed / inside another /* */ comment.

It’s standard practice to use // for one-line observations and /* */ for multi-line explanations or temporarily commenting out code blocks. It is advised to keep a consistent style, utilize indentation to represent program structure, and include lots of comments that go beyond simply summarizing the function of the code to describe its purpose.

Example of C++ Comments:

/*
 * This is a multi-line C-style comment 
 * It explains the purpose of the program.
 */
#include <iostream> // This line includes the iostream header 
int main() {
    // This is a single-line C++ comment 
    std::cout << "Hello, World!" << std::endl; /* This comment is on the same line as code */
    return 0; // The program exits successfully 
}

Output:

Hello, World!

You can also read What Is The Compilation And Linking Process In C++?

Agarapu Geetha
Agarapu Geetha
My name is Agarapu Geetha, a B.Com graduate with a strong passion for technology and innovation. I work as a content writer at Govindhtech, where I dedicate myself to exploring and publishing the latest updates in the world of tech.
Index