C++ Syntax

C++ syntax refers to the rules that define how a C++ program is written and interpreted by the compiler. Understanding basic syntax is essential for writing correct and readable code.

Basic Structure of a C++ Program


  #include <iostream>
  using namespace std;
  
  int main() {
      cout << "Hello, World!";
      return 0;
  }
          

Explanation

  • #include <iostream>: Includes the standard input-output stream library.
  • using namespace std;: Lets you use standard C++ classes/functions without the std:: prefix.
  • int main(): The entry point of every C++ program.
  • cout: Used for printing output to the console.
  • return 0;: Ends the program and returns 0 to the system.

Semicolons and Braces

Each statement in C++ ends with a semicolon (;). Code blocks are enclosed in curly braces {}.

Comments

// This is a single-line comment
  
  /*
    This is a
    multi-line comment
  */

Case Sensitivity

C++ is case-sensitive. For example, main and Main are treated as different identifiers.

Whitespace

Whitespace (spaces, tabs, newlines) is ignored by the compiler but should be used to improve code readability.