In C++, punctuators do not define an operation that produces a value; rather, they provide syntactic and semantic meaning to the compiler. Certain punctuators may also be important to the preprocessor or C++ operators when used singly or in combination.
The basic C++ punctuation is as follows.
- Semicolon (;): The comment ends this.
- comma (,): Use a comma (,) to separate items in a list or sequence.
- Colon (:): The case statement of a switch statement is used in various situations, including component separations and conditional operators (?:).
- Double colon (::): This colon is used to access members of a namespace or class when using the scope resolution operator.
- Parentheses (): It is used to include parameters in function declarations and calls, and to set the order of actions in expressions.
- Braces ({}): It is used to begin arrays, strucks, and other sets, and to specify the size of functions, classes, and other blocks of code.
- Square brackets (): It declares arrays and is used for array subscripting.
- Angle brackets (<> or \ and >) are used in conjunction with header files and template declarations.
- Period (.): It is used to use objects or pointers to access members of a class or system.
- Arrow (->): It is a pointer-based method for accessing members of a class or procedure.
- Preprocessor instructions use hashes (#).
- Double hash (##): It is a token pasting macro used in macro definitions.
Here is a simple example for every C++ punctuator:
- Semicolon (;): This punctuator is used to define that the statements are terminated.
program:
Example
#include <iostream>
int main() {
std::cout << "Hello, world!" << std::endl;
return 0;
}
Output:
- Comma (,): It is used to separates the items in a List.
Program:
Example
#include <iostream>
int main() {
int a = 1, b = 2, c = 3;
std::cout << a << ", " << b << ", " << c << std::endl;
return 0;
}
Output:
- Colon (:): It is used in many situations with the conditional operator (?:) to divide the switch statement into the parts of the case statement.
Program:
Example
#include <iostream>
int main() {
int num = 1;
switch(num) {
case 1:
std::cout << "One";
break;
case 2:
std::cout << "Two";
break;
default:
std::cout << "Other";
}
return 0;
}
Output:
- Double Colon (::): It defines the operator for scope resolution.
Program:
Example
#include <iostream>
namespace NS {
int value = 9;
}
int main() {
std::cout << NS::value << std::endl;
return 0;
}
Output:
- Parentheses (): It is used to specify the order of operations in statements, function calls, and declarations.
Program:
Example
#include <iostream>
int add(int a, int b) {
return a + b;
}
int main() {
int sum = add(4, 8);
std::cout << "Sum: " << sum << std::endl;
return 0;
}
Output:
- Braces ({}): It is used to begin arrays, strucks, and other sets, and to specify the size of functions, classes, and other blocks of code.
Program:
Example
#include <iostream>
int main() {
{
int x = 5;
std::cout << "x: " << x << std::endl;
}
// x is out of scope here
return 0;
}
Output:
- Square brackets (): It declare array types and be used for array subscripting.
Program:
Example
#include <iostream>
int main() {
int arr[3] = {1, 2, 3};
std::cout << "Element at index 1: " << arr[1] << std::endl;
return 0;
}
Output
- Angle brackets (<> or \and >): It is used to enclose header files and template declarations.
Program:
Example
#include <iostream>
template <typename T>
T add(T a, T b) {
return a + b;
}
int main() {
int sum = add<int>(3, 4);
std::cout << "Sum: " << sum << std::endl;
return 0;
}
Output:
- Period(.): It can be used to access members of a class or structure by an object.
Program:
Example
#include <iostream>
struct Point {
int x;
int y;
};
int main() {
Point p;
p.x = 5;
p.y = 10;
std::cout << "X: " << p.x << ", Y: " << p.y << std::endl;
return 0;
}
Output:
- Arrow (->): It is a pointer-based method for accessing members of a class or procedure.
Program:
Example
#include <iostream>
struct Point {
int x;
int y;
};
int main() {
Point* p = new Point();
p->x = 5;
p->y = 10;
std::cout << "X: " << p->x << ", Y: " << p->y << std::endl;
delete p;
return 0;
}
Output:
- Hash (#): Preprocessor instructions use hash (#).
Program:
Example
#include <iostream>
#define VALUE 42
int main() {
std::cout << "Value: " << VALUE << std::endl;
return 0;
}
Output:
- Double hash (##): It is a token pasting macro used in macro definitions.
Program:
Example
#include <iostream>
#define CONCAT(a, b) a##b
int main() {
int ab = CONCAT(3, 4);
std::cout << "Concatenated value: " << ab << std::endl;
return 0;
}
Output: