Lesson 7 of 60 – C++ Syntax
12%

C++ Syntax

C++ syntax refers to the rules and structure that must be followed when writing a C++ program. Understanding syntax helps you write programs that the compiler can correctly understand and compile.

Note: C++ is case-sensitive. Small details such as semicolons, braces, quotation marks, and correct spelling of keywords are important when writing C++ programs.

1. What is C++ Syntax?

Syntax is the set of rules that defines how C++ statements, expressions, functions, classes, and other language elements must be written.

int age = 20;

The above statement follows valid C++ syntax.

2. Basic Structure of a C++ Program

#include <iostream>

int main() {

    std::cout << "Hello C++";

    return 0;
}

A simple program can contain a header, the main function, statements, and a return statement.

3. C++ Statements

A statement is an instruction that tells the program to perform an operation.

int age = 20;
std::cout << age;

Many C++ statements end with a semicolon.

4. Semicolon

The semicolon ; is used to terminate many C++ statements.

int number = 10;
std::cout << number;

Forgetting a required semicolon can result in a compilation error.

5. Curly Braces

Curly braces { } are used to define blocks of code. Functions, loops, conditional statements, classes, and other constructs can contain blocks.

int main() {

    std::cout << "Hello";

}

6. The main() Function

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

int main() {

    return 0;
}

Execution of a normal C++ program begins from main.

7. Header Files

Header files provide declarations and facilities that can be used by a C++ program.

#include <iostream>

The iostream header is commonly used for standard input and output streams.

8. #include Directive

The #include directive is a preprocessor directive used to include a header.

#include <iostream>

This makes declarations from the selected header available during compilation.

9. Keywords

Keywords are reserved words that have special meaning in the C++ language.

Examples include:

  • int
  • if
  • else
  • for
  • while
  • class
  • return
int age = 20;

Here, int is a C++ keyword.

10. Identifiers

Identifiers are names given to program elements such as variables, functions, classes, and objects.

int studentAge = 20;

Here, studentAge is an identifier.

11. Rules for Identifiers

Common rules for C++ identifiers include:

  • They can contain letters, digits, and underscores.
  • They cannot begin with a digit.
  • They cannot contain spaces.
  • They cannot be C++ keywords.
  • They are case-sensitive.
studentName
student_name
age2

12. Valid and Invalid Identifiers

Valid identifiers:

age
studentName
student_name
number1

Invalid examples:

2number
student name
class

The first two invalid examples violate identifier rules, while class is a reserved keyword.

13. Case Sensitivity

C++ is case-sensitive. Uppercase and lowercase letters are treated as different characters.

int age = 20;
int Age = 30;
int AGE = 40;

Here, age, Age, and AGE are different identifiers.

14. Comments

Comments are notes written in source code. They are ignored as executable code.

Single-line comment:

// This is a comment

Multi-line comment:

/*
   This is a
   multi-line comment
*/

15. Single-Line Comments

A single-line comment starts with // and continues until the end of the line.

int age = 20; // Student age

The text after // on that line is treated as a comment.

16. Multi-Line Comments

Multi-line comments begin with /* and end with */.

/*
   This program
   displays a message.
*/

They can span multiple lines.

17. String Literals

Text is commonly written inside double quotation marks.

std::cout << "Hello C++";

The text Hello C++ is a string literal.

18. Character Literals

A character literal is commonly written inside single quotation marks.

char grade = 'A';

Here, 'A' is a character literal.

19. Numeric Literals

Numbers can be written directly in C++ source code.

int age = 20;
double price = 99.50;

Here, 20 and 99.50 are numeric literals.

20. Variables and Assignment

A variable can be declared and assigned a value using valid C++ syntax.

int age = 20;

Here, int specifies the type, age is the variable name, and 20 is the initial value.

21. Operators in Syntax

Operators are symbols used to perform operations on values and variables.

int sum = a + b;

The + operator performs addition in this expression.

22. Expressions

An expression is a combination of values, variables, operators, and other elements that produces a value.

int result = a + b * 2;

The right side of the assignment contains an expression.

23. Conditional Syntax

The if statement is used to execute code when a condition is true.

if (age >= 18) {

    std::cout << "Adult";

}

The condition is written inside parentheses.

24. Loop Syntax

A for loop contains an initialization, condition, and update expression.

for (int i = 1; i <= 5; i++) {

    std::cout << i;

}

25. Function Syntax

A function declaration or definition includes a return type, function name, parameter list, and function body.

int add(int a, int b) {

    return a + b;

}

Here, int is the return type and add is the function name.

26. Namespace Syntax

C++ uses namespaces to organize names and reduce naming conflicts. The standard library uses the std namespace.

std::cout << "Hello";
std::string name = "Rahul";

The std:: prefix specifies the standard namespace.

27. Whitespace and Indentation

Spaces, tabs, and line breaks can improve readability. Proper indentation makes the structure of a program easier to understand.

if (age >= 18) {

    std::cout << "Adult";

}

Consistent formatting is especially useful when programs become large.

28. Common Syntax Errors

Common beginner syntax mistakes include:

  • Missing semicolons
  • Missing braces
  • Missing quotation marks
  • Misspelled keywords
  • Incorrect capitalization
  • Incorrect parentheses
  • Using an invalid identifier
std::cout << "Hello"   // Missing ;

29. Complete Syntax Example

#include <iostream>

int main() {

    int age = 20;

    if (age >= 18) {

        std::cout << "You are an adult.";

    } else {

        std::cout << "You are a minor.";

    }

    return 0;
}

This example demonstrates headers, the main function, variables, conditions, braces, statements, operators, and output.

30. Basic C++ Syntax Checklist

Element Example
Header #include <iostream>
Function int main()
Block { }
Statement int age = 20;
Output std::cout << "Hello";
Input std::cin >> age;
Comment // Comment
Condition if (age >= 18)
Loop for (...)
Return return 0;

Learning these basic syntax rules will make it easier to understand the remaining C++ lessons.

📌 Key Points

  • C++ syntax defines the rules for writing valid C++ programs.
  • C++ is case-sensitive.
  • Many C++ statements end with a semicolon.
  • Curly braces define blocks of code.
  • The main() function is the normal entry point of a program.
  • Keywords have special meanings in C++.
  • Identifiers are names given to program elements.
  • Comments can be written using // or /* */.
  • Strings are commonly written inside double quotation marks.
  • Character literals are commonly written inside single quotation marks.
  • Conditions are written inside parentheses.
  • Proper indentation makes code easier to read.

🧠 Quick Quiz

Question: Which symbol is commonly used to end a C++ statement?