{"id":136,"date":"2026-05-21T04:52:51","date_gmt":"2026-05-21T04:52:51","guid":{"rendered":"https:\/\/gigz.pk\/cpp\/?post_type=lesson&#038;p=136"},"modified":"2026-05-22T11:33:18","modified_gmt":"2026-05-22T11:33:18","slug":"unary-operators","status":"publish","type":"lesson","link":"https:\/\/gigz.pk\/cpp\/?lesson=unary-operators","title":{"rendered":"Unary Operators"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Unary operators in C++ are operators that work with only one operand (single value or variable). They are commonly used for incrementing, decrementing, logical operations, and memory-related operations.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What are Unary Operators?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Unary operators perform operations on a single operand.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>++a<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Here:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>a<\/code> is the operand<\/li>\n\n\n\n<li><code>++<\/code> is the unary operator<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Why Use Unary Operators?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Unary operators are useful because they:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Simplify code<\/li>\n\n\n\n<li>Perform quick operations<\/li>\n\n\n\n<li>Reduce extra statements<\/li>\n\n\n\n<li>Improve code readability<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Types of Unary Operators in C++<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Common unary operators include:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Increment Operator (<code>++<\/code>)<\/li>\n\n\n\n<li>Decrement Operator (<code>--<\/code>)<\/li>\n\n\n\n<li>Unary Minus (<code>-<\/code>)<\/li>\n\n\n\n<li>Logical NOT (<code>!<\/code>)<\/li>\n\n\n\n<li>Address Operator (<code>&amp;<\/code>)<\/li>\n\n\n\n<li>Dereference Operator (<code>*<\/code>)<\/li>\n\n\n\n<li>Sizeof Operator (<code>sizeof<\/code>)<\/li>\n<\/ul>\n\n\n\n<h1 class=\"wp-block-heading\">Increment Operator (++)<\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">The increment operator increases a variable value by 1.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Example of Increment Operator<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;<br>using namespace std;<br><br>int main() {<br><br>    int a = 5;<br><br>    ++a;<br><br>    cout &lt;&lt; a;<br><br>    return 0;<br>}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Output<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>6<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Types of Increment<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Pre-Increment<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Value increases before use.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>++a;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Post-Increment<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Value increases after use.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>a++;<\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\">Decrement Operator (&#8211;)<\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">The decrement operator decreases a variable value by 1.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Example of Decrement Operator<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;<br>using namespace std;<br><br>int main() {<br><br>    int a = 10;<br><br>    --a;<br><br>    cout &lt;&lt; a;<br><br>    return 0;<br>}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Output<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>9<\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\">Unary Minus Operator (-)<\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">Changes positive value to negative.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Example<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;<br>using namespace std;<br><br>int main() {<br><br>    int a = 5;<br><br>    cout &lt;&lt; -a;<br><br>    return 0;<br>}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Output<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>-5<\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\">Logical NOT Operator (!)<\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">Reverses the logical value.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Example<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;<br>using namespace std;<br><br>int main() {<br><br>    bool value = true;<br><br>    cout &lt;&lt; !value;<br><br>    return 0;<br>}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Output<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>0<\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\">Address Operator (&amp;)<\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">Returns the memory address of a variable.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Example<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;<br>using namespace std;<br><br>int main() {<br><br>    int a = 10;<br><br>    cout &lt;&lt; &amp;a;<br><br>    return 0;<br>}<\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\">Dereference Operator (*)<\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">Accesses the value stored at a memory address.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Example<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;<br>using namespace std;<br><br>int main() {<br><br>    int a = 10;<br><br>    int *ptr = &amp;a;<br><br>    cout &lt;&lt; *ptr;<br><br>    return 0;<br>}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Output<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>10<\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\">Sizeof Operator<\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">Returns the size of a data type or variable in bytes.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Example<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;<br>using namespace std;<br><br>int main() {<br><br>    int a;<br><br>    cout &lt;&lt; sizeof(a);<br><br>    return 0;<br>}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Output<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>4<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Difference Between Unary and Binary Operators<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Unary Operators<\/th><th>Binary Operators<\/th><\/tr><\/thead><tbody><tr><td>Work on one operand<\/td><td>Work on two operands<\/td><\/tr><tr><td>Example: <code>++a<\/code><\/td><td>Example: <code>a + b<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Real-Life Example<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Think of a light switch:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>ON to OFF<\/li>\n\n\n\n<li>OFF to ON<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Only one item changes state, similar to unary operations.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Why Unary Operators are Important<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Unary operators are important because they:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Simplify operations<\/li>\n\n\n\n<li>Improve programming efficiency<\/li>\n\n\n\n<li>Support pointer handling<\/li>\n\n\n\n<li>Are widely used in loops and conditions<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Applications of Unary Operators<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Unary operators are used in:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Loop control<\/li>\n\n\n\n<li>Pointer operations<\/li>\n\n\n\n<li>Memory management<\/li>\n\n\n\n<li>Logical conditions<\/li>\n\n\n\n<li>Mathematical calculations<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Unary operators in C++ work with a single operand and perform operations like incrementing, decrementing, logical reversal, and memory access. They are essential for efficient programming and are widely used in modern C++ applications.<\/p>\n\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1779449626701\"><strong class=\"schema-faq-question\"><\/strong> <p class=\"schema-faq-answer\"><\/p> <\/div> <\/div>\n\n\n<div class=\"yoast-breadcrumbs\"><span><span><a href=\"https:\/\/gigz.pk\/cpp\">Home<\/a><\/span> \u00bb <span class=\"breadcrumb_last\" aria-current=\"page\">Advanced C++ > Operator Overloading > Unary Operators<\/span><\/span><\/div>\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"menu_order":44,"template":"","class_list":["post-136","lesson","type-lesson","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Unary Operators - Learn C++Language with GiGz.PK<\/title>\n<meta name=\"description\" content=\"Learn C++ unary operators with examples of increment, decrement, logical NOT, address, dereference, and sizeof operators.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/gigz.pk\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Unary Operators - Learn C++Language with GiGz.PK\" \/>\n<meta property=\"og:description\" content=\"Learn C++ unary operators with examples of increment, decrement, logical NOT, address, dereference, and sizeof operators.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/gigz.pk\/\" \/>\n<meta property=\"og:site_name\" content=\"Learn C++Language with GiGz.PK\" \/>\n<meta property=\"article:modified_time\" content=\"2026-05-22T11:33:18+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":[\"WebPage\",\"FAQPage\"],\"@id\":\"https:\\\/\\\/gigz.pk\\\/cpp\\\/?lesson=unary-operators\",\"url\":\"https:\\\/\\\/gigz.pk\\\/\",\"name\":\"Unary Operators - Learn C++Language with GiGz.PK\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/cpp\\\/#website\"},\"datePublished\":\"2026-05-21T04:52:51+00:00\",\"dateModified\":\"2026-05-22T11:33:18+00:00\",\"description\":\"Learn C++ unary operators with examples of increment, decrement, logical NOT, address, dereference, and sizeof operators.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/gigz.pk\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/gigz.pk\\\/cpp\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Advanced C++ > Operator Overloading > Unary Operators\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/cpp\\\/#website\",\"url\":\"https:\\\/\\\/gigz.pk\\\/cpp\\\/\",\"name\":\"Learn C++Language with GiGz.PK\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/gigz.pk\\\/cpp\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Unary Operators - Learn C++Language with GiGz.PK","description":"Learn C++ unary operators with examples of increment, decrement, logical NOT, address, dereference, and sizeof operators.","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:\/\/gigz.pk\/","og_locale":"en_US","og_type":"article","og_title":"Unary Operators - Learn C++Language with GiGz.PK","og_description":"Learn C++ unary operators with examples of increment, decrement, logical NOT, address, dereference, and sizeof operators.","og_url":"https:\/\/gigz.pk\/","og_site_name":"Learn C++Language with GiGz.PK","article_modified_time":"2026-05-22T11:33:18+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":["WebPage","FAQPage"],"@id":"https:\/\/gigz.pk\/cpp\/?lesson=unary-operators","url":"https:\/\/gigz.pk\/","name":"Unary Operators - Learn C++Language with GiGz.PK","isPartOf":{"@id":"https:\/\/gigz.pk\/cpp\/#website"},"datePublished":"2026-05-21T04:52:51+00:00","dateModified":"2026-05-22T11:33:18+00:00","description":"Learn C++ unary operators with examples of increment, decrement, logical NOT, address, dereference, and sizeof operators.","breadcrumb":{"@id":"https:\/\/gigz.pk\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/gigz.pk\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/gigz.pk\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/gigz.pk\/cpp"},{"@type":"ListItem","position":2,"name":"Advanced C++ > Operator Overloading > Unary Operators"}]},{"@type":"WebSite","@id":"https:\/\/gigz.pk\/cpp\/#website","url":"https:\/\/gigz.pk\/cpp\/","name":"Learn C++Language with GiGz.PK","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/gigz.pk\/cpp\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/gigz.pk\/cpp\/index.php?rest_route=\/wp\/v2\/lesson\/136","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gigz.pk\/cpp\/index.php?rest_route=\/wp\/v2\/lesson"}],"about":[{"href":"https:\/\/gigz.pk\/cpp\/index.php?rest_route=\/wp\/v2\/types\/lesson"}],"wp:attachment":[{"href":"https:\/\/gigz.pk\/cpp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=136"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}