C++ Main Function and Basic Syntax
The structure of a C++ program: the main function, statements, types, and the semicolon rule.
Every C++ program has exactly one main function. The operating system calls main when the program starts and expects an integer value in return. Returning 0 means the program finished successfully. Returning a non-zero value means some kind of error happened. In modern C++, you can skip the return 0; statement at the end of main, because reaching the end of main automatically returns 0. However, it is generally considered good practice to return a value explicitly.
int main() {
return 0;
}
C++ is a case-sensitive programming language, which means that uppercase and lowercase letters are treated as different. For example, age, Age, and AGE are three different names.
Statements usually end with a semicolon. The semicolon shows where one instruction ends. It helps the compiler separate one statement from the next one.
Variables must be declared before use. This means you need to tell the compiler a variable’s name and type before you use it. You can also use auto, which tells the compiler to figure out the type for you automatically.
Curly braces { and } define blocks of code. For example, the body of a function, an if statement, or a loop is placed inside curly braces. A block groups several instructions together and creates its own scope, which affects where variables can be used.
To add extra explanation to the code, a programmer can write comments using // or /* ... */ notation. Anything written inside a comment is ignored by the compiler and is not included in the compiled program. Comments are used to provide additional information whenever it is needed.
#include <iostream>
int main() // main is the starting point of the program
{
/* C++ is case-sensitive:
price, Price, and PRICE would be different names */
double price = 100.0; // Variables must be declared before use
double discountPercent = 10.0; // Each statement ends with a semicolon
auto ordersNumber = 463; // 'auto' lets the compiler determine the type
if (ordersNumber > 50)
{ // Curly braces define a block of code
double extraDiscount = 5.0; // visible only inside this block
discountPercent = discountPercent + extraDiscount;
}
// extraDiscount is not visible here, because it belonged to the if block
auto finalPrice = price - price * discountPercent / 100.0;
std::cout << "Final Price: " << finalPrice; // the calculation result output
return 0;
}
In the example above, we use an if statement to apply a condition in our algorithm. It usually has the following structure:
if (<condition>) {
<statements>
}
Here, <condition> is an expression that is checked as either true or false. If the condition is true, the program executes the statements inside the if block. If the condition is false, that block is skipped.
In our example, the condition is ordersNumber > 50. This checks whether the number of orders is greater than 50. If it is, the customer gets an extra discount, and the program runs these statements:
double extraDiscount = 5.0;
discountPercent = discountPercent + extraDiscount;