C++ Punctuators

The following characters are used as punctuators (also known as separators) in C++ :

[ ]   ( )   { }   ,   ;   :   *   ...   =   #

Let's discuss all the above separators one by one :

  • Brackets [ ] - Opening and closing brackets indicate single and multidimensional array subscripts.
  • Braces { } - Opening and closing braces indicate the start and end of a compound statement (block of code containing more than one executable statements).
  • Parentheses ( ) - Opening and closing parentheses indicate function calls and function parameters. Parentheses also group expressions and isolate conditional expressions
  • Comma , - It is used as a separator in a function argument list.
  • Asterisk * - It is used for pointer declaration. The asterisk is a variable declaration denotes the creation of a pointer to a data type. The asterisk is also used as an operator to either pointer or as the multiplication operator.
  • Semicolon ; - It is used as a statement terminator. Every executable statement is terminated by a semicolon ( ; ).
  • Colon : - It indicates a labelled statement.
  • Equal to sign = - It is used for variable initialisation and as an assignment operator in expressions.
  • Pound sign # - The pound sign (#) is used for preprocessor directive and double pound sign (##) is also used as operators to perform token replacement and merging during the preprocessor scanning phase.
  • Ellipsis ... - Ellipsis (...) are used in the formal argument lists of function prototypes to indicate a variable number of argument (which means the argument list may contain any number of arguments and of any data type).

C++ Punctuators Example

Following is the example program of C++ punctuators :

/* C++ Punctuators Example */

#include<iostream.h>
#include<conio.h>

int add(int, int);

void main()
{
   clrscr();

   char name[20];    // uses Brackets [] Punctuator
   int num;

   cout<<"Enter your name :";
   cin>>name;
   cout<<"Your name is "<<name<<"\n";

   int num1, num2, sum;

   cout<<"Enter two number :";
   cin>>num1>>num2;

   sum = add(num1, num2);    // uses Parentheses() Punctuator

   cout<<"Sum of "<<num1<<" and "<<num2<<" is "<<sum<<"\n";

   cout<<"Enter a Number : ";
   cin>>num;

   /* following uses Braces {} Punctuator */
   if(num>50)
   {
      if(num>500)
      {
         cout<<"Its too high!!";
      }
      else
      {
         cout<<"Its high!!";
      }
   }
   else
   {
      cout<<"Its too low";
   }

   getch();
}
int add(int a, int b)
{
   return a+b;
}

When the above C++ program is compile and executed, it will produce the following output:

c++ punctuators separators

More Examples

Here are some more C++ programs listed, using some C++ punctuators, that you can go for:

C++ Online Test


« Previous Tutorial Next Tutorial »



Like/Share Us on Facebook 😋