{"id":81018,"date":"2021-06-25T09:00:19","date_gmt":"2021-06-25T03:30:19","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=81018"},"modified":"2021-06-25T09:00:19","modified_gmt":"2021-06-25T03:30:19","slug":"operators-in-cpp","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/operators-in-cpp\/","title":{"rendered":"Operators in C++ &#8211; Types and Examples"},"content":{"rendered":"<p>In this article, we will discuss operators in C++.<\/p>\n<p>Operators form the basis of a programming language. Operators are symbols that operate on operands. These operands can be variables or values. Operators help us to perform mathematical and logical computations.<\/p>\n<h3>Types of Operators in C++<\/h3>\n<p>In C++, operators are classified into 6 types:<\/p>\n<h4>1. Arithmetic Operators in C++<\/h4>\n<p>We use arithmetic operators to perform mathematical operations.<\/p>\n<table>\n<tbody>\n<tr>\n<td><b>Operator<\/b><\/td>\n<td><b>Operation<\/b><\/td>\n<td><b>Description<\/b><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">+<\/span><\/td>\n<td><span style=\"font-weight: 400\">Addition<\/span><\/td>\n<td><span style=\"font-weight: 400\">Adds two values<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">&#8211;<\/span><\/td>\n<td><span style=\"font-weight: 400\">Subtraction<\/span><\/td>\n<td><span style=\"font-weight: 400\">Subtracts one value from another<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">*<\/span><\/td>\n<td><span style=\"font-weight: 400\">Multiplication<\/span><\/td>\n<td><span style=\"font-weight: 400\">Multiplies two values<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">\/<\/span><\/td>\n<td><span style=\"font-weight: 400\">Division<\/span><\/td>\n<td><span style=\"font-weight: 400\">Divides one value by another<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">%<\/span><\/td>\n<td><span style=\"font-weight: 400\">Modulus<\/span><\/td>\n<td><span style=\"font-weight: 400\">Computes the remainder<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">++<\/span><\/td>\n<td><span style=\"font-weight: 400\">Increment<\/span><\/td>\n<td><span style=\"font-weight: 400\">Increases value of a variable by 1<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">&#8212;<\/span><\/td>\n<td><span style=\"font-weight: 400\">Decrement<\/span><\/td>\n<td><span style=\"font-weight: 400\">Decreases value of a variable by 1<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Here, Increment (++) and decrement (&#8211;) are unary operators, which means these work with a single operand. Rest are binary operators.<\/p>\n<p><strong>Example of C++ Arithmetic Operators<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include &lt;iostream&gt;\nusing namespace std;\n\nint main() \n{\n    int a, b;\n    a = 8;\n    b = 3;\n\n    \/\/ Addition\n    cout &lt;&lt; \"a + b = \" &lt;&lt; (a + b) &lt;&lt; endl;\n\n    \/\/ Subtraction\n    cout &lt;&lt; \"a - b = \" &lt;&lt; (a - b) &lt;&lt; endl;\n\n    \/\/ Multiplication\n    cout &lt;&lt; \"a * b = \" &lt;&lt; (a * b) &lt;&lt; endl;\n\n    \/\/ Division\n    cout &lt;&lt; \"a \/ b = \" &lt;&lt; (a \/ b) &lt;&lt; endl;\n\n    \/\/ Modulus\n    cout &lt;&lt; \"a % b = \" &lt;&lt; (a % b) &lt;&lt; endl;\n    \n    \/\/ Increment\n    cout &lt;&lt; \"++a = \" &lt;&lt; ++a &lt;&lt; endl;\n    \n    \/\/Decrement\n    cout&lt;&lt; \"--b = \" &lt;&lt; --b &lt;&lt;endl;\n\n    return 0;\n}\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">a + b = 11<br \/>\na &#8211; b = 5<br \/>\na * b = 24<br \/>\na \/ b = 2<br \/>\na % b = 2<br \/>\n++a = 9<br \/>\n&#8211;b = 2<\/div>\n<h4>2. Assignment Operators in C++<\/h4>\n<p>To assign value to a variable, we use assignment operators. Both the operands should be of same data type.<\/p>\n<table>\n<tbody>\n<tr>\n<td><b>Operator<\/b><\/td>\n<td><b>Example<\/b><\/td>\n<td><b>Equivalent to<\/b><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">=<\/span><\/td>\n<td><span style=\"font-weight: 400\">x = y<\/span><\/td>\n<td><span style=\"font-weight: 400\">x = y<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">+=<\/span><\/td>\n<td><span style=\"font-weight: 400\">x += y<\/span><\/td>\n<td><span style=\"font-weight: 400\">x = x + y<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">-=<\/span><\/td>\n<td><span style=\"font-weight: 400\">x -= y<\/span><\/td>\n<td><span style=\"font-weight: 400\">x = x &#8211; y<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">*=<\/span><\/td>\n<td><span style=\"font-weight: 400\">x *= y<\/span><\/td>\n<td><span style=\"font-weight: 400\">x = x * y<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">\/=<\/span><\/td>\n<td><span style=\"font-weight: 400\">x \/= y<\/span><\/td>\n<td><span style=\"font-weight: 400\">x = x \/ y<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">%=<\/span><\/td>\n<td><span style=\"font-weight: 400\">x %= y<\/span><\/td>\n<td><span style=\"font-weight: 400\">x = x % y<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>Example of C++ Assignment Operators<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include &lt;iostream&gt;\nusing namespace std;\n\nint main() \n{\n    int x, y;\n    x = 8;\n    y = 3;\n\n    cout &lt;&lt; \"x = \" &lt;&lt; x &lt;&lt; endl;\n    cout &lt;&lt; \"y = \" &lt;&lt; y &lt;&lt; endl;\n    \n    x \/= y;\n    cout &lt;&lt; \"x = \" &lt;&lt; x;\n\n    return 0;\n}\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">x = 8<br \/>\ny = 3<br \/>\nx = 2<\/div>\n<h4>3. Relational Operators in C++<\/h4>\n<p>Relational operators are used to compare two operands. The result of comparison is either true or false. In case of true, it returns 1 and 0 when false.<\/p>\n<table>\n<tbody>\n<tr>\n<td><b>Operator<\/b><\/td>\n<td><b>Operation<\/b><\/td>\n<td><b>Example<\/b><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">==<\/span><\/td>\n<td><span style=\"font-weight: 400\">Equal to<\/span><\/td>\n<td><span style=\"font-weight: 400\">5==7 returns 0<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">!=<\/span><\/td>\n<td><span style=\"font-weight: 400\">Not equal to<\/span><\/td>\n<td><span style=\"font-weight: 400\">5!=7 returns 1<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">&gt;<\/span><\/td>\n<td><span style=\"font-weight: 400\">Greater than<\/span><\/td>\n<td><span style=\"font-weight: 400\">7&gt;5 returns 1<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">&lt;<\/span><\/td>\n<td><span style=\"font-weight: 400\">Less than<\/span><\/td>\n<td><span style=\"font-weight: 400\">7&lt;5 returns 0<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">&gt;=<\/span><\/td>\n<td><span style=\"font-weight: 400\">Greater than or equal to<\/span><\/td>\n<td><span style=\"font-weight: 400\">5&gt;=7 returns 0<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">&lt;=<\/span><\/td>\n<td><span style=\"font-weight: 400\">Less than or equal to<\/span><\/td>\n<td><span style=\"font-weight: 400\">5&lt;=7 returns 1<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>Example of C++ Relational Operators<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include &lt;iostream&gt;\nusing namespace std;\n\nint main() \n{\n   int x, y;\n   x=5;\n   y=9;\n   bool result;\n   \n   result = (x!=y);\n   cout&lt;&lt;\"5!=9 is \"&lt;&lt;result&lt;&lt;endl;\n   result = (x&gt;y);\n   cout&lt;&lt;\"5&gt;9 is \"&lt;&lt;result;\n    \n    return 0;\n}\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">5!=9 is 1<br \/>\n5&gt;9 is 0<\/div>\n<h4>4. Logical Operators in C++<\/h4>\n<p>Logical operators determine the logic between operands and check if expressions are true or false.<\/p>\n<table>\n<tbody>\n<tr>\n<td><b>Operator<\/b><\/td>\n<td><b>Operation<\/b><\/td>\n<td><b>Description<\/b><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">&amp;&amp;<\/span><\/td>\n<td><span style=\"font-weight: 400\">Logical AND<\/span><\/td>\n<td><span style=\"font-weight: 400\">Returns true(1) only if both operands are true<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">||<\/span><\/td>\n<td><span style=\"font-weight: 400\">Logical OR<\/span><\/td>\n<td><span style=\"font-weight: 400\">Returns true(1) if atleast one of the operands is true<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">!<\/span><\/td>\n<td><span style=\"font-weight: 400\">Logical NOT<\/span><\/td>\n<td><span style=\"font-weight: 400\">Returns true(1) if the operand is false and false(0) when the operand is true.<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><span style=\"font-weight: 400\">Logical NOT (!) is a unary operator.<\/span><\/p>\n<p><strong>Example of C++ Logical Operators<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include &lt;iostream&gt;\nusing namespace std;\n\nint main() \n{\n   int x=3, y=9;\n   bool result;\n   \n   result = (x&gt;4) &amp;&amp; (y&gt;6);        \/\/false\n   cout&lt;&lt;\"(x&gt;4) &amp;&amp; (y&gt;6) is \"&lt;&lt;result&lt;&lt;endl;\n   result = (x&gt;4) || (y&gt;6);            \/\/true\n   cout&lt;&lt;\"(x&gt;4) || (y&gt;6) is \"&lt;&lt;result&lt;&lt;endl;\n   result = !(x&lt;4);                       \/\/false\n   cout&lt;&lt;\"!(x&lt;4) is \"&lt;&lt;result;\n    \n    return 0;\n}\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">(x&gt;4) &amp;&amp; (y&gt;6) is 0<br \/>\n(x&gt;4) || (y&gt;6) is 1<br \/>\n!(x&lt;4) is 0<\/div>\n<h4>5. Bitwise Operators in C++<\/h4>\n<p><span style=\"font-weight: 400\">We use bitwise operators to perform bit-level operations. Values of both operands are first converted into binary before operating on them.\u00a0<\/span><\/p>\n<table>\n<tbody>\n<tr>\n<td><b>Operator<\/b><\/td>\n<td><b>Operation<\/b><\/td>\n<td><b>Description<\/b><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">&amp;<\/span><\/td>\n<td><span style=\"font-weight: 400\">Bitwise AND<\/span><\/td>\n<td><span style=\"font-weight: 400\">AND operation bit by bit.\u00a0<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">|<\/span><\/td>\n<td><span style=\"font-weight: 400\">Bitwise OR<\/span><\/td>\n<td><span style=\"font-weight: 400\">OR operation bit by bit.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">^<\/span><\/td>\n<td><span style=\"font-weight: 400\">Bitwise XOR<\/span><\/td>\n<td><span style=\"font-weight: 400\">Exclusive OR operation bit by bit.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">~<\/span><\/td>\n<td><span style=\"font-weight: 400\">Bitwise One\u2019s Complement<\/span><\/td>\n<td><span style=\"font-weight: 400\">Converts operand into its one\u2019s complement form. <\/span><span style=\"font-weight: 400\">It is a unary operator.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">&lt;&lt;<\/span><\/td>\n<td><span style=\"font-weight: 400\">Bitwise left shift<\/span><\/td>\n<td><span style=\"font-weight: 400\">Shifts all bits by a specified number of bits to the left.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">&gt;&gt;<\/span><\/td>\n<td><span style=\"font-weight: 400\">Bitwise right shift<\/span><\/td>\n<td><span style=\"font-weight: 400\">Shifts all bits by a specified number of bits to the right.<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><span style=\"font-weight: 400\">The right hand side operand of &lt;&lt; and &gt;&gt; operators specifies the number of bits to be shifted.<\/span><\/p>\n<p><strong>Example of C++ Bitwise Operators<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include &lt;iostream&gt;\nusing namespace std;\n\nint main() \n{\n   int x=14, y=20;\n   \n   cout&lt;&lt;\"x&amp;y = \"&lt;&lt; (x&amp;y) &lt;&lt;endl;\n   cout&lt;&lt;\"x|y = \"&lt;&lt; (x|y) &lt;&lt;endl;\n   cout&lt;&lt;\"x^y = \"&lt;&lt; (x^y) &lt;&lt;endl;\n   cout&lt;&lt;\"1's Complement of x is \"&lt;&lt; (~x) &lt;&lt;endl;\n   cout&lt;&lt;\"Left shift x by 2 bits, result = \"&lt;&lt; (x&lt;&lt;2) &lt;&lt;endl;\n   cout&lt;&lt;\"Right shift y by 1 bit, result = \"&lt;&lt; (y&gt;&gt;1);\n    \n    return 0;\n}\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">x&amp;y = 4<br \/>\nx|y = 30<br \/>\nx^y = 26<br \/>\n1&#8217;s Complement of x is -15<br \/>\nLeft shift x by 2 bits, result = 56<br \/>\nRight shift y by 1 bit, result = 10<\/div>\n<h4>6. Other Operators in C++<\/h4>\n<p>Some other operators in C++ are:<\/p>\n<table>\n<tbody>\n<tr>\n<td><b>Operator<\/b><\/td>\n<td><b>Description<\/b><\/td>\n<td><b>Example<\/b><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">sizeof()<\/span><\/td>\n<td><span style=\"font-weight: 400\">Computes size of its operand in bytes<\/span><\/td>\n<td><span style=\"font-weight: 400\">sizeof(int);\u00a0 \/\/4<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">Condition ? exp1 : exp2\u00a0<\/span><\/td>\n<td><span style=\"font-weight: 400\">It is the ternary conditional operator. If the condition is true, it returns exp1, else exp2.<\/span><\/td>\n<td><span style=\"font-weight: 400\">(9&gt;5) ? cout&lt;&lt;\u201ctrue\u201d : cout&lt;&lt;\u201cfalse\u201d;\u00a0 \/\/true<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">, (Comma)<\/span><\/td>\n<td><span style=\"font-weight: 400\">It first evaluates the first operand followed by the second and then returns the result of the second operand.<\/span><\/td>\n<td><span style=\"font-weight: 400\">int i = (5, 10);\u00a0 \/\/i=10<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">. (dot)<\/span><\/td>\n<td><span style=\"font-weight: 400\">Used to access members of struct and class.<\/span><\/td>\n<td><span style=\"font-weight: 400\">obj.age=25;<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">-&gt; (arrow)<\/span><\/td>\n<td><span style=\"font-weight: 400\">Used with pointers to access members of struct and class.<\/span><\/td>\n<td><span style=\"font-weight: 400\">ptr-&gt;age=25;<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">&amp; (\u2018Address of\u2019 Pointer operator)<\/span><\/td>\n<td><span style=\"font-weight: 400\">Returns the memory address.<\/span><\/td>\n<td><span style=\"font-weight: 400\">a = 100;<\/span><\/p>\n<p><span style=\"font-weight: 400\">p = &amp;a;\u00a0 \/\/p=some memory address<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">* (\u2018Indirection\u2019 Pointer operator)<\/span><\/td>\n<td><span style=\"font-weight: 400\">Pointer to a variable.<\/span><\/td>\n<td><span style=\"font-weight: 400\">a = 100;<\/span><\/p>\n<p><span style=\"font-weight: 400\">p = &amp;a;<\/span><\/p>\n<p><span style=\"font-weight: 400\">v = *p;\u00a0 \/\/v=100<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">(type) Cast<\/span><\/td>\n<td><span style=\"font-weight: 400\">Converts one data type to another.<\/span><\/td>\n<td><span style=\"font-weight: 400\">double a = 12.85467;<\/span><\/p>\n<p><span style=\"font-weight: 400\">int b;<\/span><\/p>\n<p><span style=\"font-weight: 400\">b = (int) a;\u00a0 \/\/b=12<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>Precedence of Operators in C++<\/h3>\n<p>In the table given below, the precedence of operators decreases from top to bottom.<\/p>\n<table>\n<tbody>\n<tr>\n<td><b>Operator<\/b><\/td>\n<td><b>Description<\/b><\/td>\n<td><b>Associativity<\/b><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">::<\/span><\/td>\n<td><span style=\"font-weight: 400\">Scope resolution<\/span><\/td>\n<td rowspan=\"2\"><span style=\"font-weight: 400\">Left to right<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">Postfix Unary<\/span><\/p>\n<p><span style=\"font-weight: 400\">++ &#8212;<\/span><\/p>\n<p><span style=\"font-weight: 400\">()<\/span><\/p>\n<p><span style=\"font-weight: 400\">[]<\/span><\/p>\n<p><span style=\"font-weight: 400\">. -&gt;<\/span><\/td>\n<td><span style=\"font-weight: 400\">Increment Decrement<\/span><span style=\"font-weight: 400\">Function call<\/span><\/p>\n<p><span style=\"font-weight: 400\">Subscript<\/span><\/p>\n<p><span style=\"font-weight: 400\">Member access<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">Prefix Unary<\/span><\/p>\n<p><span style=\"font-weight: 400\">++ &#8212;<\/span><\/p>\n<p><span style=\"font-weight: 400\">+ &#8211;<\/span><\/p>\n<p><span style=\"font-weight: 400\">! ~<\/span><\/p>\n<p><span style=\"font-weight: 400\">(type)<\/span><\/p>\n<p><span style=\"font-weight: 400\">*<\/span><\/p>\n<p><span style=\"font-weight: 400\">&amp;<\/span><\/p>\n<p><span style=\"font-weight: 400\">sizeof()<\/span><\/td>\n<td><span style=\"font-weight: 400\">Increment Decrement<\/span><span style=\"font-weight: 400\">Unary plus minus<\/span><\/p>\n<p><span style=\"font-weight: 400\">Logical NOT, Bitwise complement<\/span><\/p>\n<p><span style=\"font-weight: 400\">Cast<\/span><\/p>\n<p><span style=\"font-weight: 400\">Dereference<\/span><\/p>\n<p><span style=\"font-weight: 400\">Address of\/reference<\/span><\/p>\n<p><span style=\"font-weight: 400\">size of operand in bytes<\/span><\/td>\n<td><span style=\"font-weight: 400\">Right to left<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">.* .-&gt;<\/span><\/td>\n<td><span style=\"font-weight: 400\">Pointer to member<\/span><\/td>\n<td rowspan=\"6\"><span style=\"font-weight: 400\">Left to right<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">* \/ %<\/span><\/p>\n<p><span style=\"font-weight: 400\">+ &#8211;<\/span><\/td>\n<td><span style=\"font-weight: 400\">Arithmetic<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">&lt;&lt; &gt;&gt;<\/span><\/td>\n<td><span style=\"font-weight: 400\">Bitwise left shift, Bitwise right shift<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">&lt; &lt;=<\/span><\/p>\n<p><span style=\"font-weight: 400\">&gt; &gt;=<\/span><\/p>\n<p><span style=\"font-weight: 400\">== !=<\/span><\/td>\n<td><span style=\"font-weight: 400\">Relational\u00a0<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">&amp;<\/span><\/p>\n<p><span style=\"font-weight: 400\">^<\/span><\/p>\n<p><span style=\"font-weight: 400\">|<\/span><\/td>\n<td><span style=\"font-weight: 400\">Bitwise AND<\/span><\/p>\n<p><span style=\"font-weight: 400\">Bitwise XOR<\/span><\/p>\n<p><span style=\"font-weight: 400\">Bitwise OR<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">&amp;&amp;<\/span><\/p>\n<p><span style=\"font-weight: 400\">||<\/span><\/td>\n<td><span style=\"font-weight: 400\">Logical AND<\/span><\/p>\n<p><span style=\"font-weight: 400\">Logical OR<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">?:<\/span><\/td>\n<td><span style=\"font-weight: 400\">Ternary Conditional<\/span><\/td>\n<td rowspan=\"2\"><span style=\"font-weight: 400\">Right to left<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">=\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400\">+= -=\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400\">*= \/= %=\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400\">&lt;&lt;= &gt;&gt;=\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400\">&amp;= ^= |=\u00a0<\/span><\/td>\n<td><span style=\"font-weight: 400\">Assignment<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">,<\/span><\/td>\n<td><span style=\"font-weight: 400\">Comma\u00a0<\/span><\/td>\n<td><span style=\"font-weight: 400\">Left to right<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>Example to illustrate operator precedence in C++<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include &lt;iostream&gt;\nusing namespace std;\n\nint main() {\n  int result1 = 5 + 10 \/ 3;\n  int result2 = (5 + 10) \/ 3;\n  cout&lt;&lt;result1&lt;&lt;endl;\n  cout&lt;&lt;result2&lt;&lt;endl;\n  result1==result2 ? cout&lt;&lt;\"Equal\" : cout&lt;&lt;\"Unequal\";\n  return 0;\n}<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">8<br \/>\n5<br \/>\nUnequal<\/div>\n<p>Observe that we obtained different results for both the expressions because of operator precedence.<\/p>\n<h3>Summary<\/h3>\n<p>From this article, we learnt operators in C++. In C++, operators are mainly divided into 6 categories. We covered operators of each category with examples followed by the precedence of these operators.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article, we will discuss operators in C++. Operators form the basis of a programming language. Operators are symbols that operate on operands. These operands can be variables or values. Operators help us&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":81382,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3405],"tags":[3617,3618,3600,3603,3604,3605,3619],"class_list":["post-81018","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-cpp","tag-arithmetic-operators-in-c","tag-assignment-operators-in-c","tag-bitwise-operator-in-c","tag-logical-operator-in-c","tag-operator-precedence-in-c","tag-operators-in-c","tag-relational-operators-in-c"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Operators in C++ - Types and Examples - TechVidvan<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/techvidvan.com\/tutorials\/operators-in-cpp\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Operators in C++ - Types and Examples - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"In this article, we will discuss operators in C++. Operators form the basis of a programming language. Operators are symbols that operate on operands. These operands can be variables or values. Operators help us&#046;&#046;&#046;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/operators-in-cpp\/\" \/>\n<meta property=\"og:site_name\" content=\"TechVidvan\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/TechVidvan\/\" \/>\n<meta property=\"article:published_time\" content=\"2021-06-25T03:30:19+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/06\/Operators-in-C-1.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"TechVidvan Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:site\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"TechVidvan Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Operators in C++ - Types and Examples - TechVidvan","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/techvidvan.com\/tutorials\/operators-in-cpp\/","og_locale":"en_US","og_type":"article","og_title":"Operators in C++ - Types and Examples - TechVidvan","og_description":"In this article, we will discuss operators in C++. Operators form the basis of a programming language. Operators are symbols that operate on operands. These operands can be variables or values. Operators help us&#46;&#46;&#46;","og_url":"https:\/\/techvidvan.com\/tutorials\/operators-in-cpp\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2021-06-25T03:30:19+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/06\/Operators-in-C-1.jpg","type":"image\/jpeg"}],"author":"TechVidvan Team","twitter_card":"summary_large_image","twitter_creator":"@vidvantech","twitter_site":"@vidvantech","twitter_misc":{"Written by":"TechVidvan Team","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/techvidvan.com\/tutorials\/operators-in-cpp\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/operators-in-cpp\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22"},"headline":"Operators in C++ &#8211; Types and Examples","datePublished":"2021-06-25T03:30:19+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/operators-in-cpp\/"},"wordCount":864,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/operators-in-cpp\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/06\/Operators-in-C-1.jpg","keywords":["Arithmetic\u00a0Operators in C","Assignment\u00a0Operators in C","Bitwise Operator in C","Logical Operator in C","Operator Precedence in C","Operators in C","Relational\u00a0Operators in C"],"articleSection":["C++ Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techvidvan.com\/tutorials\/operators-in-cpp\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/operators-in-cpp\/","url":"https:\/\/techvidvan.com\/tutorials\/operators-in-cpp\/","name":"Operators in C++ - Types and Examples - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/operators-in-cpp\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/operators-in-cpp\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/06\/Operators-in-C-1.jpg","datePublished":"2021-06-25T03:30:19+00:00","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/operators-in-cpp\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/operators-in-cpp\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/operators-in-cpp\/#primaryimage","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/06\/Operators-in-C-1.jpg","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/06\/Operators-in-C-1.jpg","width":1200,"height":628,"caption":"Operators in C++"},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/operators-in-cpp\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"Operators in C++ &#8211; Types and Examples"}]},{"@type":"WebSite","@id":"https:\/\/techvidvan.com\/tutorials\/#website","url":"https:\/\/techvidvan.com\/tutorials\/","name":"TechVidvan Blogs","description":"","publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/techvidvan.com\/tutorials\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/techvidvan.com\/tutorials\/#organization","name":"TechVidvan","url":"https:\/\/techvidvan.com\/tutorials\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","width":200,"height":50,"caption":"TechVidvan"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/TechVidvan\/","https:\/\/x.com\/vidvantech"]},{"@type":"Person","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22","name":"TechVidvan Team","description":"The TechVidvan Team delivers practical, beginner-friendly tutorials on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our experts are here to help you upskill and excel in today\u2019s tech industry."}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/81018","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/comments?post=81018"}],"version-history":[{"count":0,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/81018\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media\/81382"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=81018"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=81018"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=81018"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}