{"id":2097,"date":"2024-01-24T06:59:41","date_gmt":"2024-01-24T06:59:41","guid":{"rendered":"https:\/\/www.skillvertex.com\/blog\/?p=2097"},"modified":"2024-01-24T06:59:41","modified_gmt":"2024-01-24T06:59:41","slug":"unary-operators-in-c-cpp","status":"publish","type":"post","link":"https:\/\/www.skillvertex.com\/blog\/unary-operators-in-c-cpp\/","title":{"rendered":"Unary Operators In C\/C++"},"content":{"rendered":"\n<div class=\"wp-block-rank-math-toc-block\" id=\"rank-math-toc\" id=\"rank-math-toc\"><p>Table of Contents<\/p><nav><ul><li ><a href=\"#unary-operators-in-c-c\">Unary Operators In C\/C++<\/a><ul><li ><a href=\"#types-of-unary-operators\">Types of Unary Operators<\/a><ul><li ><a href=\"#1-unary-minus\">1. Unary Minus<\/a><\/li><li ><a href=\"#2-increment\">2. Increment<\/a><\/li><li ><a href=\"#3-decrement\">3. Decrement<\/a><\/li><li ><a href=\"#4-not\">4. NOT ( ! )<\/a><\/li><li ><a href=\"#5-addressof-operator\">5. Addressof operator ( &amp; )<\/a><\/li><li ><a href=\"#6-size-of\">6. Size of()<\/a><\/li><\/ul><\/li><li ><a href=\"#faq-unary-operators-in-c-c\">FAQ &#8211; Unary Operators In C\/C++<\/a><ul><\/ul><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"unary-operators-in-c-c\">Unary Operators In C\/C++<\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">Unary operators in C and C++ are special tools used in programming that work with a single piece of data at a time. They help programmers change values, perform actions like counting, and make decisions based on data. In this article, we&#8217;ll take a closer look at these unary operators in C and C++, explaining what they are, what they do, and how they can be useful in programming.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"types-of-unary-operators\">Types of Unary Operators<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Unary minus ( \u2013 )<\/li>\n\n\n\n<li>Increment ( ++ )<\/li>\n\n\n\n<li>Decrement ( \u2014 )<\/li>\n\n\n\n<li>NOT ( ! )<\/li>\n\n\n\n<li>Addressof operator ( &amp; )<\/li>\n\n\n\n<li>Sizeof()<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"1-unary-minus\">1. Unary Minus<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The minus operator (-) is a simple but powerful tool in programming. It does one thing: it changes the sign of a number. If you have a positive number, it turns it into a negative number, and if you have a negative number, it makes it positive. This operator is handy for flipping the direction of values in your code when needed.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code> int a = 10;\n int b = -a;  \/\/ b = -10<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>&nbsp;The implementation of the&nbsp;unary minus (-)&nbsp;operator:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ C++ program to demonstrate the use of 'unary minus'\n\/\/ operator\n \n#include &lt;iostream&gt;\nusing namespace std;\n \nint main()\n{\n    int positiveInteger = 100;\n    int negativeInteger = -positiveInteger;\n \n    cout &lt;&lt; \"Positive Integer: \" &lt;&lt; positiveInteger &lt;&lt; endl;\n    cout &lt;&lt; \"Negative Integer: \" &lt;&lt; negativeInteger &lt;&lt; endl;\n \n    return 0;\n}\n\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Output <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Positive Integer: 100\nNegative Integer: -100<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"2-increment\">2. Increment<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The increment operator (++), a useful tool in programming, serves one primary purpose: to increase the value of a variable by 1. This operator provides two ways to perform the increment operation: prefix and postfix. These options offer flexibility in how you manipulate variable values in your code.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>2.1 prefix increment<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In the prefix method, the operator comes before the operand, like this: ++a. When you use the prefix increment operator, the value of the operand is changed before it is used in any other operations. This means that if you have a variable a with a value of 5 and you do ++a, a will become 6, and this new value will be used in any subsequent calculations or assignments.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int a = 1;\n  int b = ++a;  \/\/ b = 2<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>2.2 postfix increment<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In the postfix method, the operator follows the operand, like this: a++. When you use the postfix increment operator, the current value of the operand is used in any ongoing operations or assignments, and then the operand&#8217;s value is increased by 1. So, if you have a variable a with a value of 5 and you do a++, a will first be used as 5 in any calculations, and then it will be incremented to 6 afterward.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int a = 1;\n int b = a++;   \/\/ b = 1\n int c = a;     \/\/ c = 2<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>&nbsp;The implementation of the&nbsp;increment ( ++ )<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ C++ Program to illustrate increment operator\n#include &lt;iostream&gt;\nusing namespace std;\n \nint main()\n{\n \n    int a = 5;\n    int b = 5;\n    cout &lt;&lt; \"Pre-Incrementing a = \" &lt;&lt; ++a &lt;&lt; endl;\n    cout &lt;&lt; \"Post-Incrementing b = \" &lt;&lt; b++ &lt;&lt; endl;\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Output <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Pre-Incrementing a = 6\nPost-Incrementing b = 5<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"3-decrement\">3. Decrement<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The decrement operator (&#8211;), a handy tool in programming, has a straightforward purpose: it decreases the value of a variable by 1. Similar to the increment operator, the decrement operation can be performed in two ways, providing flexibility in how you manipulate variable values in your code.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>3.1 prefix decrement<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In the prefix method, the decrement operator comes before the operand, like this: &#8211;a. When you use the prefix decrement operator<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int a = 1;\n  int b = --a;  \/\/ b = 0<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>3.2 postfix decrement<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In the postfix method, the decrement operator follows the operand, like this: a&#8211;. When you use the postfix decrement operator, the current value of the operand is used in any ongoing operations or assignments<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int a = 1;\n int b = a--;   \/\/ b = 1\n int c = a;     \/\/ c = 0<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>&nbsp;The implementation of the&nbsp;increment ( ++ ):<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ C++ Program to illustrate decrement operator\n#include &lt;iostream&gt;\nusing namespace std;\n \nint main()\n{\n \n    int a = 5;\n    int b = 5;\n    cout &lt;&lt; \"Pre-Decrementing a = \" &lt;&lt; --a &lt;&lt; endl;\n    cout &lt;&lt; \"Post-Decrementing b = \" &lt;&lt; b-- &lt;&lt; endl;\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Pre-Decrementing a = 4\nPost-Decrementing b = 5<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"4-not\">4. NOT ( ! )<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The logical NOT operator (!) is a valuable tool in programming used to flip the logical state of its operand. When you apply the logical NOT operator to a condition, it changes it from true to false, and vice versa. This operator is essential for altering the truth value of expressions, which is crucial for decision-making in programming.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>If x is true, then !x is false\n   If x is false, then !x is true<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>&nbsp;The implementation of the&nbsp;NOT (!)&nbsp;operator<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ C++ program to demonstrate the use of '!(NOT) operator'\n \n#include &lt;iostream&gt;\nusing namespace std;\n \nint main()\n{\n \n    int a = 10;\n    int b = 5;\n \n    if (!(a &gt; b))\n        cout &lt;&lt; \"b is greater than a\" &lt;&lt; endl;\n    else\n        cout &lt;&lt; \"a is greater than b\" &lt;&lt; endl;\n \n    return 0;\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>a is greater than b<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"5-addressof-operator\">5. Addressof operator ( &amp; )<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The address-of-operator (&amp;) in programming is like a tool that helps us find where a variable is stored in the computer&#8217;s memory. When we use this operator, it gives us the exact memory address of a variable. These memory addresses are often called &#8220;pointers&#8221; because they act as signposts, pointing to where the variable is kept in the computer&#8217;s memory. Pointers are important because they allow us to work with variables indirectly, which is useful in various programming tasks.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&amp; gives an address on variable n\n    int a;\n    int *ptr;\n    ptr = &amp;a; \/\/ address of a is copied to the location ptr. <\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>The implementation of the&nbsp;Address of operator(&amp;)<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\n\/\/ C++ program to demonstrate the use of 'address-of(&amp;)'\n\/\/ operator\n#include &lt;iostream&gt;\nusing namespace std;\n \nint main()\n{\n \n    int a = 10;\n    int* ptr = &amp;a; \/\/&amp; will give the address of a\n    cout &lt;&lt; \"Address of a = \" &lt;&lt; ptr;\n \n    return 0;\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Output <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Address of a = 0x7ffcbffd5b24\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"6-size-of\">6. Size of()<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The size of the () operator is a handy tool in programming that tells us the size of its operand in terms of bytes. This operator always comes before the operand, which can be an expression or a cast. Essentially, it helps us figure out how much memory space a particular piece of data occupies in bytes.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>The implementation of&nbsp;the size of the ()&nbsp;operator<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/\/ C++ program to illustrate the sizeof operator\n#include &lt;iostream&gt;\nusing namespace std;\n \nint main()\n{\n    cout &lt;&lt; \"Size of double: \" &lt;&lt; sizeof(double) &lt;&lt; endl;\n    cout &lt;&lt; \"Size of int: \" &lt;&lt; sizeof(int) &lt;&lt; endl;\n \n    return 0;\n}<\/code><\/pre>\n\n\n<div class=\"gb-container gb-container-01b92cff\">\n\n<p class=\"wp-block-paragraph\">Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Size of double: 8\nSize of int: 4<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"faq-unary-operators-in-c-c\">FAQ &#8211; Unary Operators In C\/C++<\/h2>\n\n\n<div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-question-1695644406198\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q1. What is a unary operator in C?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. Unary operator is\u00a0<strong>operators that act upon a single operand to produce a new value<\/strong>. The unary operators are as follows. It operates on a pointer variable and returns an l-value equivalent to the value at the pointer address. This is called &#8220;dereferencing&#8221; the pointer<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1695644411781\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q2. What is unary and binary operators in C++?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans.  Arithmetic operators in programming perform mathematical operations, like addition and subtraction. There are two types: unary (operate on one value) and binary (combine two values). They are crucial for calculations in programming.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1695644419979\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q3. Is unary or binary in C++?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. The plus sign (+) in programming serves as both a unary and binary operator. In its unary form (+a), it enforces the evaluation of the operand as a number or a pointer. In its binary form (a + b), it performs addition between two values.<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n\n\n<h3 class=\"wp-block-heading\"><br><\/h3>\n\n<\/div>","protected":false},"excerpt":{"rendered":"<p>Unary Operators In C\/C++ Unary operators in C and C++ are special tools used in programming that work with a single piece of data at a time. They help programmers change values, perform actions like counting, and make decisions based on data. In this article, we&#8217;ll take a closer look at these unary operators in &#8230; <a title=\"Unary Operators In C\/C++\" class=\"read-more\" href=\"https:\/\/www.skillvertex.com\/blog\/unary-operators-in-c-cpp\/\" aria-label=\"More on Unary Operators In C\/C++\">Read more<\/a><\/p>\n","protected":false},"author":4,"featured_media":2105,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27],"tags":[371],"class_list":["post-2097","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-programming","tag-unary-operators-in-c-c","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-33"],"_links":{"self":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/2097","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/comments?post=2097"}],"version-history":[{"count":11,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/2097\/revisions"}],"predecessor-version":[{"id":6443,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/2097\/revisions\/6443"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media\/2105"}],"wp:attachment":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media?parent=2097"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/categories?post=2097"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/tags?post=2097"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}