c++ main example

C++ Main Function Example

By
AD

Welcome to a comprehensive guide on the C++ main function, the entry point of any C++ program. Let's delve into understanding and demonstrating its usage with examples.

The main function in C++ is a special function that marks the beginning of program execution. It is the first function to be called when your C++ program starts running.

Understanding the Main Function

Every C++ program has a main function, which serves as the entry point for the program. It is the first function that is executed when you run your program.

AD
an image of the website page with different features
an image of the website page with different features
Difference between B2B & B2C segmentation
Difference between B2B & B2C segmentation
B2B vs B2C Email Marketing: Key Differences Explained
B2B vs B2C Email Marketing: Key Differences Explained

The main function can have different forms, but it should always have a return type of int, followed by its name main, and it can take two forms: with no arguments, or with arguments preceded by a void keyword.

Main Function with No Arguments

The most common form of the main function is one that does not accept any arguments. The syntax is as follows:

eCommerce Email Template Design
eCommerce Email Template Design
homepage hierarchy - Google Search
homepage hierarchy - Google Search
C Programming Series - Part 2 : Comments in C
C Programming Series - Part 2 : Comments in C

int main() { // code goes here }

Main Function with Arguments

The main function can also accept arguments passed from the command line. The syntax for this is:

AD

int main(int argc, char* argv[]) { // code goes here }

the anatomy of a perfect b2b service page
the anatomy of a perfect b2b service page
a white and black web page with different lines on the bottom, one line above it
a white and black web page with different lines on the bottom, one line above it
Review Section Ui Design, Testimonials On Website, Reviews Page Design, Testimonial Card Design, Review Section Ui, Testimonial Slide Design, Testimonial Card, Website Testimonials Design, Testimonials Page Design
Review Section Ui Design, Testimonials On Website, Reviews Page Design, Testimonial Card Design, Review Section Ui, Testimonial Slide Design, Testimonial Card, Website Testimonials Design, Testimonials Page Design
a diagram showing how to use the landing page for an article or webpage,
a diagram showing how to use the landing page for an article or webpage,
Aesthetic: Printable Life Planner for Digital Minimalism Lifestyle 🦋 [#ky8w]
Aesthetic: Printable Life Planner for Digital Minimalism Lifestyle 🦋 [#ky8w]
Facebook | Types of web page layout, Page layout design website, Uiux
Facebook | Types of web page layout, Page layout design website, Uiux

Here, argc represents the number of command-line arguments, and argv[] is an array of pointers to the actual arguments.

Executing the Main Function

The main function can be followed by a condition that returns a value to the operating system. This value indicates how the program has finished executing:

AD

int main() { // code return 0; }

C Programming Series - Part 2 : Structure of a C Program
C Programming Series - Part 2 : Structure of a C Program
The Purpose of Content Marketing for B2B vs. B2C Brands
The Purpose of Content Marketing for B2B vs. B2C Brands
CORE principles
CORE principles
Designing an e-commerce site for a toy store - UX case study
Designing an e-commerce site for a toy store - UX case study
Web online visual
Web online visual
modern landing page
modern landing page
Modern Portfolio Website Design | Creative Developer Portfolio Inspiration
Modern Portfolio Website Design | Creative Developer Portfolio Inspiration
an image of a website page with the same colors and size as it appears to be
an image of a website page with the same colors and size as it appears to be
Trucos para crear una web que convierta con estilo
Trucos para crear una web que convierta con estilo
Frame work
Frame work
Website Architecture Breakdown: All Essential Web Page Elements
Website Architecture Breakdown: All Essential Web Page Elements
Triangle Properties and Formulas Explained | Easy Geometry Guide for Students
Triangle Properties and Formulas Explained | Easy Geometry Guide for Students
Types of Chemical Reactions: Combination, Decomposition, Redox | NEET 2026 Class 10-12
Types of Chemical Reactions: Combination, Decomposition, Redox | NEET 2026 Class 10-12
an info sheet showing the different types of aircrafts that can be seen in this image
an info sheet showing the different types of aircrafts that can be seen in this image

The 0 in this context means the program ended normally. Any other value (like 1, -1, etc.) indicates that the program ended abnormally or had an error.

Including Libraries in the Main Function

The C++ main function often includes certain libraries to facilitate programming tasks. The most common is the standard library, which is included via the line:

AD

#include <iostream>

This library allows for basic data input and output operations, and is often used when demonstrating the main function as it enables cout, a simple output command.

Here's a simple example using the main function with the iostream library included:

#include <iostream> int main() { std::cout << "Hello, World!"; return 0; }

This simple program prints "Hello, World!" to the console and then ends, signifying a normal exit with return value 0.

Now, that you have understood the C++ main function, its role, and its various forms, you can start using it in your own programs. Happy coding!

AD