Punctuators In C++ - C++ Programming Tutorial
C++ Course / Miscellaneous / Punctuators In C++

Punctuators In C++

BLUF: Mastering Punctuators In C++ is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Punctuators In C++

C++ is renowned for its efficiency. Learn how Punctuators In C++ enables low-level control and high-performance computing in the tutorial below.

In C++, punctuators do not carry out an action that results in a value; instead, they impart syntactic and semantic significance to the compiler. Some punctuators can also play a crucial role for the preprocessor or C++ operators whether used individually or in conjunction.

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.

Semicolon (;): This punctuation mark signifies the end of a statement in C++.

program:

Example

#include <iostream>
int main() {
    std::cout << "Hello, world!" << std::endl;
    return 0;
}
  1. The comma symbol (,) is utilized to separate the elements within a list.
  2. Program:

Example

#include <iostream>
int main() {
    int a = 1, b = 2, c = 3;
    std::cout << a << ", " << b << ", " << c << std::endl;
    return 0;
}
  1. Colon (:): This symbol is commonly employed alongside the conditional operator (?:) to segment the switch statement into different sections of the case statement.
  2. 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;
}
  1. The Double Colon (::) symbol specifies the operator responsible for resolving scope.
  2. Program:

Example

#include <iostream>
namespace NS {
    int value = 9;
}
int main() {
    std::cout << NS::value << std::endl;
    return 0;
}
  1. Parentheses (): They are employed to indicate the sequence of operations in statements, function invocations, and definitions.
  2. 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;
}
  1. Curly Braces ({}): These are employed to initiate arrays, structures, and other collections, as well as to indicate the dimensions of functions, classes, and other code segments.
  2. Program:

Example

#include <iostream>
int main() {
    {
        int x = 5;
        std::cout << "x: " << x << std::endl;
    }
    // x is out of scope here
    return 0;
}
  1. Square brackets (): They are employed to define array types and are utilized for accessing elements within arrays.
  2. Program:

Example

#include <iostream>
int main() {
    int arr[3] = {1, 2, 3};
    std::cout << "Element at index 1: " << arr[1] << std::endl;
    return 0;
}
  1. Angle brackets (<> or \and >): They are employed to wrap header files and template declarations.
  2. 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;
}
  1. A full stop (.) can serve to retrieve properties of a class or structure through an instance.
  2. 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;
}
  1. Arrow (->): This operator serves as a pointer-based approach to access members within a class or function.
  2. 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;
}
  1. The hash symbol (#) is employed in preprocessor directives.
  2. Program:

Example

#include <iostream>
#define VALUE 42
int main() {
    std::cout << "Value: " << VALUE << std::endl;
    return 0;
}
  1. The double hash symbol (##) functions as a token pasting macro that is employed within macro definitions.
  2. 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:

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience