cppAcademy
← Learning PathLESSON 2 / 30 · FUNDAMENTALS · 25 MIN✕

C++ Basics and Program Structure

A C++ program is source text that a compiler translates into an executable. For standard contest problems, execution starts in main(), reads values, computes an answer and prints it. Learning this small structure makes compiler messages less mysterious and gives every later algorithm a reliable place to run. We will compute the perimeter of a rectangle from two entered side lengths.

Prerequisites: Introduction to Competitive Programming.

Build the idea

  1. 1. Include the declarations you use

    The directive #include <iostream> supplies declarations for standard streams. It is handled before ordinary compilation and does not end with a semicolon. std::cin and std::cout belong to the std namespace. Writing std:: explicitly avoids unexplained namespace shortcuts. Include standard headers rather than a nonstandard all-in-one header if you want code to compile across common C++17 environments.

  2. 2. Locate main and its statements

    int main() defines the program entry function. Braces group its body. Most statements inside end with a semicolon. return 0 reports successful completion to the environment; it is not the answer printed to the judge. The compiler ignores text after // on the same line, so comments explain choices without changing execution.

  3. 3. Declare variables before use

    long long width and height reserve named integer objects; cin>>width>>height stores input into them. Names are case-sensitive. perimeter is another variable initialized from the expression 2*(width+height). Parentheses make addition occur before multiplication. A declaration introduces a name, while a later assignment replaces the value held by an existing name.

  4. 4. Follow execution in order

    For input 3 5, width becomes 3 and height becomes 5. Their sum is 8, multiplied by 2 to produce 16. cout<<perimeter sends its value to output; the character \n starts a new line. Statements run top to bottom here. Nothing in the example reads from a file automatically or requests input through a pop-up; the judge or terminal supplies standard input.

  5. 5. Compile, run and read errors

    Save as main.cpp, compile with g++ -std=c++17 main.cpp -o main (or an equivalent C++17 compiler), then run the produced executable with the sample on standard input. A missing semicolon often makes the next line look erroneous; inspect the preceding statement too. The executable must be rebuilt after source changes. An IDE’s run command should perform the same compile-and-execute steps.

C++17 implementation

This is a complete program. Compile with C++17 and enter the input described below.

#include<iostream>
int main(){
    long long width,height;
    std::cin>>width>>height;
    long long perimeter=2*(width+height); // Add adjacent sides, then double.
    std::cout<<perimeter<<'\n';
    return 0;
}

Complexity analysis

O(1) time and O(1) auxiliary space: a fixed number of variables and arithmetic operations independent of the side values. This assumes bounded integer arithmetic, as used throughout these introductory programs.

Common mistakes and edge cases. Do not put a semicolon after the #include directive, omit braces or use a variable before declaring it. Character and string literals require quotes; a variable name does not. A missing std:: prefix can be a name-lookup error rather than an algorithm error. The input limits keep arithmetic within long long; units must match.

Example input and output

Two integer side lengths width height, each 0..10^9. Print the rectangle perimeter.

Input

3 5

Output

16

There are two sides of length 3 and two of length 5: 3+5+3+5=16.

Practical applications

Use this structure as a starting point for ordinary batch-input contest programs. Separate input, computation and output in your thinking even when the code is short. As programs grow, functions will organize computation while main continues to coordinate the workflow.

Check your understanding

Which action turns source into an executable?

Put it into practice

Change the program to compute rectangle area.

Show a hint

Replace the computation with width*height and keep long long.