C++ Basic Syntax

Before writing bigger C++ programs, it is important to understand the C++ basic syntax.

Don’t worry if the code looks unfamiliar. We will look at each part one by one.

Basic Structure of a C++ Program

Here is a simple C++ program:

#include <iostream>
using namespace std;

int main() {
    cout << "Hello, World!";
    return 0;
}

Output:

Hello, World!

Now let’s understand what each line does.

#include <iostream>

#include <iostream>

iostream is a standard C++ library used for input and output.

It gives us access to tools such as cout and cin.

For example, we need iostream when we want to display something on the screen using cout.

using namespace std

using namespace std;

C++ has a standard namespace called std.

Writing using namespace std; allows us to use common features such as cout and cin directly.

Without it, we can write:

std::cout << "Hello, World!";

With using namespace std;, we can simply write:

cout << "Hello, World!";

You will see both styles in C++ programs.

The main() Function

int main() {
    
}

The main() function is the starting point of a C++ program.

When you run a C++ program, execution starts from main().

The { } braces show where the function begins and ends.

For example:

#include <iostream>
using namespace std;

int main() {
    cout << "C++ is fun!";
    return 0;
}

The program starts running from:

int main()

and executes the statements inside the braces.

The cout Statement

cout is used to display output on the screen.

Example:

#include <iostream>
using namespace std;

int main() {
    cout << "Welcome to C++";
    return 0;
}

Output:

Welcome to C++

You can also display numbers:

#include <iostream>
using namespace std;

int main() {
    cout << 100;
    return 0;
}

Output:

100

Printing Multiple Things

You can use multiple cout statements:

#include <iostream>
using namespace std;

int main() {
    cout << "My name is Alex.";
    cout << " I am learning C++.";
    return 0;
}

Output:

My name is Alex. I am learning C++.

You can also use multiple insertion operators:

#include <iostream>
using namespace std;

int main() {
    cout << "My age is " << 14;
    return 0;
}

Output:

My age is 14

We will learn how variables work in the next chapters.

What is a Statement?

A statement is an instruction given to the computer.

For example:

cout << "Hello";

This tells the computer to display Hello.

Most C++ statements end with a semicolon ;.

cout << "Hello";
cout << "C++";

Forgetting the semicolon can cause a compilation error.

Semicolon in C++

The semicolon ; tells the compiler that a statement has ended.

Correct:

cout << "Hello";
cout << "Welcome";

Incorrect:

cout << "Hello"
cout << "Welcome";

The first statement is missing a semicolon.

Curly Braces { }

Curly braces are used to group statements together.

For example:

int main() {
    cout << "Hello";
    cout << "Welcome";
}

Everything between { and } belongs to the main() function.

You will see curly braces frequently when working with functions, conditions, loops, classes, and other C++ concepts.

return 0

At the end of the main() function, you will often see:

return 0;

It indicates that the program has finished successfully.

For beginner programs, you can think of it simply as:

The program ended successfully.

Example:

#include <iostream>
using namespace std;

int main() {
    cout << "Program finished.";
    return 0;
}

C++ Comments

Comments are notes written inside the code for humans. The compiler ignores them.

Comments are useful for explaining code and making programs easier to understand.

C++ has two common types of comments:

  1. Single-line comments
  2. Multi-line comments

Single-Line Comments

A single-line comment starts with //.

#include <iostream>
using namespace std;

int main() {
    // Display a message
    cout << "Hello, C++!";
    
    return 0;
}

The compiler ignores:

// Display a message

You can also place a comment after a statement:

cout << "Hello"; // Display Hello

Multi-Line Comments

A multi-line comment starts with /* and ends with */.

#include <iostream>
using namespace std;

int main() {
    /*
       This is a multi-line comment.
       It can contain multiple lines.
    */

    cout << "Hello, C++!";

    return 0;
}

Everything between /* and */ is treated as a comment.

Complete Example

Let’s put some of these concepts together:

#include <iostream>
using namespace std;

int main() {
    // Display the student's information
    cout << "Name: Rahul";
    cout << "\nAge: 14";
    cout << "\nLearning C++";

    return 0;
}

Output:

Name: Rahul
Age: 14
Learning C++

Here, \n moves the output to a new line. You will learn more about special characters later.

Key Points to Remember

  • #include <iostream> allows us to use input and output features.
  • main() is the starting point of a C++ program.
  • cout displays output on the screen.
  • Most C++ statements end with ;.
  • { } are used to group code.
  • return 0; indicates successful program completion.
  • // creates a single-line comment.
  • /* */ creates a multi-line comment.

What’s Next?

Now that you understand the basic structure of C++ programs, the next chapter will introduce C++ Variables & Data Types and show you how to store and use information inside a program.


Frequently Asked Questions (FAQs)

Q1. What is C++ basic syntax?

C++ basic syntax defines the structure and rules used to write a C++ program, including main(), statements, semicolons, braces, and comments.

Q2. What are the basic syntax rules in C++?

Important C++ syntax rules include using semicolons after most statements, matching curly braces, following the correct structure of functions, and using valid keywords and identifiers.

Q3. What is the basic structure of a C++ program?

A basic C++ program usually includes required libraries, the main() function, statements inside curly braces, and a return statement.

Q4. What is a C++ statement?

A C++ statement is an instruction given to the computer. For example, cout << "Hello"; is a statement that displays text on the screen.

Q5. How do you write comments in C++?

C++ supports single-line comments with // and multi-line comments with /* */. Comments help explain code without affecting program execution.

Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

Scroll to Top