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.
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.
#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.
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.
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.
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";
}
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.
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.
The #include directive is a preprocessor directive used to include a header.
#include <iostream>
This makes declarations from the selected header available during compilation.
Keywords are reserved words that have special meaning in the C++ language.
Examples include:
int age = 20;
Here, int is a C++ keyword.
Identifiers are names given to program elements such as variables, functions, classes, and objects.
int studentAge = 20;
Here, studentAge is an identifier.
Common rules for C++ identifiers include:
studentName
student_name
age2
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.
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.
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
*/
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.
Multi-line comments begin with /* and end with */.
/*
This program
displays a message.
*/
They can span multiple lines.
Text is commonly written inside double quotation marks.
std::cout << "Hello C++";
The text Hello C++ is a string literal.
A character literal is commonly written inside single quotation marks.
char grade = 'A';
Here, 'A' is a character literal.
Numbers can be written directly in C++ source code.
int age = 20;
double price = 99.50;
Here, 20 and 99.50 are numeric literals.
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.
Operators are symbols used to perform operations on values and variables.
int sum = a + b;
The + operator performs addition in this expression.
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.
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.
A for loop contains an initialization, condition, and update expression.
for (int i = 1; i <= 5; i++) {
std::cout << i;
}
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.
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.
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.
Common beginner syntax mistakes include:
std::cout << "Hello" // Missing ;
#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.
| 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.
Question: Which symbol is commonly used to end a C++ statement?