C++ Basic Syntax


C++ Basic Syntax

In this tutorial, we will learn the basic syntax of C++ language. We will go through the key components of a simple C++ program.

C++ Program

#include <iostream>

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

Output

Hello, World!

Basic Syntax of a C++ Program

  1. #include <iostream>
    This line includes the input-output stream library, which contains functions for input and output operations, such as std::cout.
  2. int main()
    This line defines the main function where the program execution begins. The int keyword indicates that the function returns an integer value.
  3. {
    This opening brace marks the beginning of the main function's body.
  4. std::cout << "Hello, World!" << std::endl;
    This line prints the string "Hello, World!" to the standard output (usually the screen). The << operator is used for output, and std::endl is used to add a new line after the text.
  5. return 0;
    This line ends the main function and returns the value 0 to the calling process. In C++, returning 0 typically indicates that the program executed successfully.
  6. }
    This closing brace marks the end of the main function's body.

Key Points to Remember

  • All C++ statements must end with a semicolon (;).
  • The main function is the entry point of a C++ program.
  • Comments can be added using // for single-line comments or /* ... */ for multi-line comments.
  • Code blocks are enclosed in curly braces {}.
  • Standard C++ libraries, like <iostream>, provide essential functionality for input and output operations.