{"id":2202,"date":"2024-05-10T07:14:44","date_gmt":"2024-05-10T07:14:44","guid":{"rendered":"https:\/\/www.skillvertex.com\/blog\/?p=2202"},"modified":"2024-05-10T07:14:44","modified_gmt":"2024-05-10T07:14:44","slug":"operators-in-c-set-2-relational-and-logical-operators","status":"publish","type":"post","link":"https:\/\/www.skillvertex.com\/blog\/operators-in-c-set-2-relational-and-logical-operators\/","title":{"rendered":"Operators In C | Set 2 (Relational And Logical Operators)"},"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=\"#operators-in-c-set-2-relational-and-logical-operators\">Operators In C | Set 2 (Relational And Logical Operators)<\/a><\/li><li ><a href=\"#relational-operators\">Relational Operators<\/a><\/li><li ><a href=\"#logical-operators\">Logical Operators<\/a><\/li><li ><a href=\"#short-circuiting-in-logical-operators\">Short-Circuiting in Logical Operators<\/a><\/li><li ><a href=\"#faq-operators-in-c-set-2-relational-and-logical-operators\">FAQ- Operators In C | Set 2 (Relational and Logical Operators)<\/a><\/li><\/ul><\/nav><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"operators-in-c-set-2-relational-and-logical-operators\">Operators In C | Set 2 (Relational And Logical Operators)<\/h2>\n\n\n\n<p>In C programming, operators are like tools that help us make decisions and control how our program works. This time, we&#8217;re going to focus on two important types of operators: relational and logical operators. Relational operators help us compare things, like checking if one number is greater than another. Logical operators help us combine conditions and decide what our program should do based on those conditions. Learning how to use these operators is crucial for writing good C code. So, let&#8217;s dive into the world of relational and logical operators and see how they can make our programs smarter and more effective.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"relational-operators\">Relational Operators<\/h2>\n\n\n\n<p>Here are some common comparison operators in C:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Equal to operator (<code>==<\/code>)<\/strong>: This operator checks if two values are the same. If they are equal, it returns true; otherwise, it returns false. For example, <code>5 == 5<\/code> will return true.<\/li>\n\n\n\n<li><strong>Not equal to operator (<code>!=<\/code>)<\/strong>: This operator checks if two values are not the same. If they are not equal, it returns true; otherwise, it returns false. It&#8217;s the opposite of the equal to operator. For example, <code>5 != 5<\/code> will return false.<\/li>\n\n\n\n<li><strong>Greater than operator (<code>&gt;<\/code>)<\/strong>: The greater than operator checks if the first value is larger than the second value. If it is, it returns true; otherwise, it returns false. For example, <code>6 &gt; 5<\/code> will return true.<\/li>\n\n\n\n<li><strong>Less than operator (<code>&lt;<\/code>)<\/strong>: The less than operator checks if the first value is smaller than the second value. If it is, it returns true; otherwise, it returns false. For example, <code>6 &lt; 5<\/code> will return false.<\/li>\n\n\n\n<li><strong>Greater than or equal to operator (<code>&gt;=<\/code>)<\/strong>: This operator checks if the first value is greater than or equal to the second value. If it is, it returns true; otherwise, it returns false. For example, <code>5 &gt;= 5<\/code> will return true.<\/li>\n\n\n\n<li><strong>Less than or equal to operator (<code>&lt;=<\/code>)<\/strong>: This operator checks if the first value is less than or equal to the second value. If it is, it returns true; otherwise, it returns false. For example, <code>5 &lt;= 5<\/code> will also return true.<\/li>\n<\/ol>\n\n\n\n<p>These operators are fundamental for making comparisons and decisions in C programming, allowing you to control the flow of your programs based on different conditions.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\n\/\/ C program to demonstrate working of relational operators\n#include &lt;stdio.h&gt;\n \nint main()\n{\n    int a = 10, b = 4;\n \n    \/\/ greater than example\n    if (a &gt; b)\n        printf(\"a is greater than b\\n\");\n    else\n        printf(\"a is less than or equal to b\\n\");\n \n    \/\/ greater than equal to\n    if (a &gt;= b)\n        printf(\"a is greater than or equal to b\\n\");\n    else\n        printf(\"a is lesser than b\\n\");\n \n    \/\/ less than example\n    if (a &lt; b)\n        printf(\"a is less than b\\n\");\n    else\n        printf(\"a is greater than or equal to b\\n\");\n \n    \/\/ lesser than equal to\n    if (a &lt;= b)\n        printf(\"a is lesser than or equal to b\\n\");\n    else\n        printf(\"a is greater than b\\n\");\n \n    \/\/ equal to\n    if (a == b)\n        printf(\"a is equal to b\\n\");\nelse\n        printf(\"a and b are not equal\\n\");\n \n    \/\/ not equal to\n    if (a != b)\n        printf(\"a is not equal to b\\n\");\n    else\n        printf(\"a is equal b\\n\");\n \n    return 0;\n}<\/code><\/pre>\n\n\n\n<p>Output <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>a is greater than b\na is greater than or equal to b\na is greater than or equal to b\na is greater than b\na and b are not equal\na is not equal to b<\/code><\/pre>\n\n\n\n<p>Time Complexity: 0(1)<\/p>\n\n\n\n<p>Auxiliary Space:&nbsp;O(1)<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"logical-operators\">Logical Operators<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Logical AND operator:<\/strong>&nbsp;The&nbsp;<strong>\u2018&amp;&amp;\u2019<\/strong>&nbsp;operator returns true when both the conditions under consideration are satisfied. Otherwise, it returns false. For example,&nbsp;<strong>a &amp;&amp; b<\/strong>&nbsp;returns true when both a and b are true (i.e. non-zero).<\/li>\n\n\n\n<li><strong>Logical OR operator:<\/strong>&nbsp;The&nbsp;<strong>\u2018||\u2019<\/strong>&nbsp;operator returns true even if one (or both) of the conditions under consideration is satisfied. Otherwise, it returns false. For example,&nbsp;<strong>a || b<\/strong>&nbsp;returns true if one of a or b, or both are true (i.e. non-zero). Of course, it returns true when both a and b are true.<\/li>\n\n\n\n<li><strong>Logical NOT operator:<\/strong>&nbsp;The&nbsp;<strong>\u2018!\u2019<\/strong>&nbsp;operator returns true the condition in consideration is not satisfied. Otherwise, it returns false. For example,&nbsp;<strong>!a<\/strong>&nbsp;returns true if a is false, i.e. when a=0<\/li>\n<\/ol>\n\n\n\n<p>Exampe<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ C program to demonstrate working of logical operators\n#include &lt;stdio.h&gt;\n \nint main()\n{\n    int a = 10, b = 4, c = 10, d = 20;\n \n    \/\/ logical operators\n \n    \/\/ logical AND example\n    if (a &gt; b &amp;&amp; c == d)\n        printf(\"a is greater than b AND c is equal to d\\n\");\n    else\n        printf(\"AND condition not satisfied\\n\");\n \n    \/\/ logical OR example\n    if (a &gt; b || c == d)\n        printf(\"a is greater than b OR c is equal to d\\n\");\n    else\n        printf(\"Neither a is greater than b nor c is equal \"\n               \" to d\\n\");\n \n    \/\/ logical NOT example\n    if (!a)\n        printf(\"a is zero\\n\");\n    else\n        printf(\"a is not zero\");\n \n    return 0;\n}<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>AND condition not satisfied\na is greater than b OR c is equal to d\na is not zero<\/code><\/pre>\n\n\n\n<p><strong>Time Complexity:<\/strong>&nbsp;O(1)<\/p>\n\n\n\n<p><strong>Auxiliary Space:&nbsp;<\/strong>O(1)<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"short-circuiting-in-logical-operators\">Short-Circuiting in Logical Operators<\/h2>\n\n\n\n<p>In many programming languages, including C and C++, the logical AND operator (<code>&amp;&amp;<\/code>) performs short-circuit evaluation. This means that if the first operand of a logical AND is <code>false<\/code>, the second operand is not evaluated because the overall result will always be <code>false<\/code>. This can be particularly useful for improving efficiency and preventing errors in situations where evaluating the second operand might lead to issues.<\/p>\n\n\n\n<p>For example, In program 1 below doesn\u2019t print \u201cskill Vertex Quiz\u201d as the first operand of logical AND itself is false.&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;stdbool.h&gt;\n#include &lt;stdio.h&gt;\nint main()\n{\n    int a = 10, b = 4;\n    bool res = ((a == b) &amp;&amp; printf(\"Skill Vertex Quiz\"));\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>No Output<\/code><\/pre>\n\n\n\n<p>Whereas, the program below prints Skill Vertex Quiz as the First Operand of Logical AND is true<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;stdbool.h&gt;\n#include &lt;stdio.h&gt;\nint main()\n{\n    int a = 10, b = 4;\n    bool res = ((a != b) &amp;&amp; printf(\"Skill Vertex Quiz\"));\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Skill Vertex Quiz<\/code><\/pre>\n\n\n\n<p><strong>Time Complexity:&nbsp;<\/strong>O(1)<\/p>\n\n\n\n<p><strong>Auxiliary Space:&nbsp;<\/strong>O(1)<\/p>\n\n\n\n<p>In the logical OR operator (<code>||<\/code>) also performs short-circuit evaluation. This means that if the first operand of a logical OR is <code>true<\/code>, the second operand is not evaluated because the overall result will always be <code>true<\/code>. For example, program 1 below won&#8217;t print &#8220;SkillVertexQuiz\u201d as the first operand of logical OR itself is true.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;stdbool.h&gt;\n#include &lt;stdio.h&gt;\nint main()\n{\n    int a = 10, b = 4;\n    bool res = ((a != b) || printf(\"Skill Vertex Quiz\"));\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p>Output <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>No Output <\/code><\/pre>\n\n\n\n<p><strong>Time Complexity:<\/strong>&nbsp;O(1)<\/p>\n\n\n\n<p><strong>Auxiliary Space:<\/strong>O(1)<\/p>\n\n\n\n<p>Whereas, below program below will print \u201cSkill Vertex Quiz\u201d as the first operand of logical OR is false.&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;stdbool.h&gt;\n#include &lt;stdio.h&gt;\nint main()\n{\n    int a = 10, b = 4;\n    bool res = ((a == b) || printf(\"Skill Vertex Quiz\"));\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Skill Vertex Quiz<\/code><\/pre>\n\n\n\n<p><strong>Time Complexity:&nbsp;<\/strong>O(1)<\/p>\n\n\n\n<p><strong>Auxiliary Space:&nbsp;<\/strong>O(1)<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"faq-operators-in-c-set-2-relational-and-logical-operators\">FAQ- Operators In C | Set 2 (Relational and Logical Operators)<\/h2>\n\n\n<div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-question-1695885058011\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q1. What is relational operator and logical operator?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. Relational, equality, and logical operators are essential tools in programming.<br \/><strong>Relational operators<\/strong> compare values to determine relationships (e.g., greater than or less than).<br \/><strong>Equality operators<\/strong> check if values are equal or not equal.<br \/><strong>Logical operators<\/strong> combine true and false values to create more complex conditions (e.g., AND and OR).<br \/>These operators help in decision-making, controlling program flow, and forming logical expressions.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1695885075804\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q2. What are the 2 operators in C?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. In C programming, the increment (<code>++<\/code>) and decrement (<code>--<\/code>) operators are used to modify the value of an operand, whether it&#8217;s a constant or a variable. The increment operator (<code>++<\/code>) increases the value by 1, while the decrement operator (<code>--<\/code>) decreases the value by 1. These operators are considered unary operators because they operate on a single operand.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1695885086377\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q3. What is %d in C?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. <strong>Format specifier<\/strong>: It is denoted by a <code>%<\/code> symbol followed by a letter, and it instructs the program on how to format and interpret data for input and output.<br \/><strong>%d<\/strong>: This format specifier is used to represent a signed decimal integer. It tells the program that the associated variable should be treated as an integer and formatted accordingly.<br \/><strong>%i<\/strong>: Similarly, <code>%i<\/code> is also a format specifier used for integers. In C, <code>%d<\/code> and <code>%i<\/code> are often interchangeable when reading or printing integers, as both are used for the same purpose<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>Operators In C | Set 2 (Relational And Logical Operators) In C programming, operators are like tools that help us make decisions and control how our program works. This time, we&#8217;re going to focus on two important types of operators: relational and logical operators. Relational operators help us compare things, like checking if one number &#8230; <a title=\"Operators In C | Set 2 (Relational And Logical Operators)\" class=\"read-more\" href=\"https:\/\/www.skillvertex.com\/blog\/operators-in-c-set-2-relational-and-logical-operators\/\" aria-label=\"More on Operators In C | Set 2 (Relational And Logical Operators)\">Read more<\/a><\/p>\n","protected":false},"author":4,"featured_media":2203,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27],"tags":[390],"class_list":["post-2202","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-programming","tag-operators-in-c-set-2-relational-and-logical-operators","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\/2202","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=2202"}],"version-history":[{"count":9,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/2202\/revisions"}],"predecessor-version":[{"id":10636,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/2202\/revisions\/10636"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media\/2203"}],"wp:attachment":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media?parent=2202"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/categories?post=2202"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/tags?post=2202"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}