codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
⚡

C++ Programming

3 / 87 topics
1Getting Started with C++2Your First C++ Program3C++ Syntax4C++ Comments5Basic Input / Output
Tutorials/C++ Programming/C++ Syntax
⚡C++ Programming

C++ Syntax

Updated 2026-05-12
15 min read

C++ Syntax

Welcome to the third tutorial in our C++ journey! After creating your first program, it's essential to understand the basic syntax rules that make up the language. Proper syntax is the backbone of any programming language and ensures that your code is interpreted correctly by the compiler.

In this tutorial, we will cover fundamental elements such as statements, semicolons, whitespace, curly braces, case sensitivity, and tokens. Understanding these concepts will help you write clear, error-free C++ code.

Statements

A statement in C++ is a basic unit of execution. It can be an instruction to perform a specific task, like printing text to the screen or performing a calculation. Every statement must end with a semicolon (;). The semicolon acts as a delimiter that tells the compiler where one statement ends and another begins.

Example

Let's look at a simple example of statements in C++:

statements.cpp
1#include <iostream>
2
3int main() {
4 std::cout << "Hello, World!" << std::endl;
5 return 0;
6}

In this code:

  • std::cout << "Hello, World!" << std::endl; is a statement that prints "Hello, World!" to the console.
  • return 0; is a statement that indicates the program has executed successfully.
Output
Hello, World!

Semicolons

As mentioned earlier, every statement in C++ must end with a semicolon. The semicolon tells the compiler where one instruction ends and another begins. Forgetting to include a semicolon at the end of a statement will result in a compilation error.

Example

Here's an example that demonstrates the importance of semicolons:

semicolon_error.cpp
1#include <iostream>
2
3int main() {
4 std::cout << "Hello, World!"
5 return 0;
6}

If you try to compile this code, you will encounter an error because the std::cout statement is missing a semicolon.

Terminal
Output
case_sensitivity.cpp: In function 'int main()':
case_sensitivity.cpp:6:31: error: 'MyVariable' was not declared in this scope
  6 |     std::cout << "MyVariable: " << MyVariable << std::endl; // Error: MyVariable is not defined
    |                               ^

Tokens

Tokens are the smallest individual units of a program. They are the building blocks of C++ syntax and include keywords, identifiers, operators, literals, and punctuation symbols.

Example

Here's an example that demonstrates different types of tokens:

tokens.cpp
1#include <iostream>
2
3int main() {
4 int number = 42; // 'int' is a keyword, 'number' is an identifier, '=' is an operator, '42' is a literal
5 std::cout << "The answer is: " << number << std::endl; // '<<' is an operator, ';' is punctuation
6 return 0;
7}

In this code:

  • int is a keyword.
  • number is an identifier.
  • = is an assignment operator.
  • 42 is an integer literal.
  • << is the insertion operator for output streams.
  • ; is punctuation.

Practical Example

Now, let's put all these concepts together in a practical example. We'll create a simple program that calculates and displays the area of a rectangle.

rectangle_area.cpp
1#include <iostream>
2
3int main() {
4 int length = 5;
5 int width = 3;
6 int area = length * width;
7
8 std::cout << "Length: " << length << std::endl;
9 std::cout << "Width: " << width << std::endl;
10 std::cout << "Area: " << area << std::endl;
11
12 return 0;
13}

In this program:

  • We declare three integer variables: length, width, and area.
  • We calculate the area by multiplying length and width.
  • We use std::cout to print the values of length, width, and area.
Output
Length: 5
Width: 3
Area: 15

Summary

In this tutorial, we covered the fundamental syntax rules of C++:

  • Statements: Instructions that perform specific tasks.
  • Semicolons: Delimiters that mark the end of a statement.
  • Whitespace: Spaces, tabs, and newlines that improve code readability.
  • Curly Braces: Used to define blocks of code.
  • Case Sensitivity: Identifiers are treated differently based on their capitalization.
  • Tokens: The smallest individual units of a program.

Understanding these concepts is crucial for writing clear and error-free C++ code. In the next tutorial, we will explore how to add comments to your code to make it more understandable and maintainable.

What's Next?

In the next tutorial, we will learn about different types of comments in C++ and how to use them effectively. Comments help explain your code and improve its readability, making it easier for others (and yourself) to understand what the code does.

Stay tuned!


PreviousYour First C++ ProgramNext C++ Comments

Recommended Gear

Your First C++ ProgramC++ Comments