{"id":10999,"date":"2022-08-20T06:29:07","date_gmt":"2022-08-20T06:29:07","guid":{"rendered":"http:\/\/fastbitlab.com\/?p=10999"},"modified":"2024-04-12T11:10:03","modified_gmt":"2024-04-12T05:40:03","slug":"microcontroller-embedded-c-programming-lecture-92-conditional-operator","status":"publish","type":"post","link":"https:\/\/fastbitlab.com\/blog\/microcontroller-embedded-c-programming-lecture-92-conditional-operator\/","title":{"rendered":"Microcontroller Embedded C programming Lecture 92| Conditional operator"},"content":{"rendered":"<div class=\"boldgrid-section\" style=\"background-image: linear-gradient(to left, #eeeeee, #eeeeee);\" data-bg-color-1=\"#EEEEEE\" data-bg-color-2=\"#EEEEEE\" data-bg-direction=\"to left\">\n<div class=\"container\">\n<div class=\"row\" style=\"padding-top: 35px; padding-bottom: 0px; background-image: linear-gradient(to left, #eeeeee, #eeeeee);\" data-bg-color-1=\"#EEEEEE\" data-bg-color-2=\"#EEEEEE\" data-bg-direction=\"to left\">\n<div class=\"col-md-1 col-sm-12 col-xs-12 col-lg-1\">\n<div class=\"boldgrid-shortcode\" data-imhwpb-draggable=\"true\">\n\n<\/div>\n<p class=\"\">&nbsp;<\/p>\n<\/div>\n<div class=\"col-md-10 col-sm-12 col-xs-12 col-lg-10\">\n<h1 class=\"\" style=\"text-align: center; font-size: 30px; border-width: 0px; line-height: 46px;\"><strong><span style=\"color: #333399;\">Conditional operator<\/span><\/strong><\/h1>\n<div class=\"row bg-editor-hr-wrap\" style=\"border-width: 0px; margin-top: -25px;\">\n<div class=\"col-lg-12 col-md-12 col-xs-12 col-sm-12\">\n<div>\n<p>&nbsp;<\/p>\n<div class=\"bg-hr bg-hr-10 color2-color\" style=\"border-style: solid; border-width: 0px 0px 3px;\"><\/div>\n<p>&nbsp;<\/p>\n<\/div>\n<\/div>\n<\/div>\n<h2 class=\"\" style=\"font-size: 25px; border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400; line-height: 30px;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\"><span style=\"text-decoration: underline; color: #000080;\"><strong>Conditional operators in C<\/strong><\/span><\/h2>\n<ul class=\"\" style=\"font-size: 17px; border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\">\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400; color: #000000;\">Conditional operator is a ternary operator in C used for conditional evaluation. It is a powerful tools in C programming for making concise and efficient decisions in code.&nbsp;<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"color: #000000;\"><span style=\"font-weight: 400;\">Operator symbol <\/span><span style=\"color: #ff0000;\"><b>?:<\/b><\/span><b>&nbsp;<\/b><\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400; color: #000000;\">It&#8217;s a ternary operator because it operates on 3 operands.&nbsp;<\/span><\/li>\n<\/ul>\n<p class=\"\">&nbsp;<\/p>\n<h3 class=\"\" style=\"font-size: 22px; border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\"><strong><span style=\"text-decoration: underline; color: #000080;\">Syntax of the conditional operator:<\/span><\/strong><\/h3>\n<figure id=\"attachment_11002\" aria-describedby=\"caption-attachment-11002\" style=\"width: 429px\" class=\"wp-caption aligncenter\"><img fetchpriority=\"high\" decoding=\"async\" class=\"wp-image-11002 \" src=\"http:\/\/fastbitlab.com\/wp-content\/uploads\/2022\/08\/Figure-1-4-1024x349.png\" alt=\"Figure 1. Syntax of conditional operator\" width=\"429\" height=\"146\" srcset=\"https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/08\/Figure-1-4-1024x349.png 1024w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/08\/Figure-1-4-300x102.png 300w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/08\/Figure-1-4-768x262.png 768w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/08\/Figure-1-4-600x204.png 600w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/08\/Figure-1-4-1536x523.png 1536w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/08\/Figure-1-4-200x68.png 200w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/08\/Figure-1-4-400x136.png 400w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/08\/Figure-1-4-800x273.png 800w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/08\/Figure-1-4-1200x409.png 1200w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/08\/Figure-1-4.png 1705w\" sizes=\"(max-width: 429px) 100vw, 429px\" \/><figcaption id=\"caption-attachment-11002\" class=\"wp-caption-text\"><span style=\"color: #000000;\">Figure 1. Syntax of conditional operator<\/span><\/figcaption><\/figure>\n<p class=\"\" style=\"padding-left: 40px; font-size: 17px; line-height: 30px; font-family: 'Roboto Slab'; font-weight: 400;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"font-weight: 400; color: #000000;\">A conditional operator is a collection of <span style=\"color: #333399;\"><strong>three operands<\/strong><\/span>. <\/span><\/p>\n<p class=\"\" style=\"padding-left: 40px; font-family: 'Roboto Slab'; font-weight: 400; font-size: 17px; line-height: 28px;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"font-weight: 400; color: #000000;\"><span style=\"color: #333399;\"><strong>expression1 <\/strong><\/span>is the first operand which may be a value or an expression, or it may be any &#8216;C&#8217; statement like a function call or something like that. A Boolean expression that evaluates to either <code>true<\/code> or <code>false<\/code>.<\/span><\/p>\n<p class=\"\" style=\"padding-left: 40px; font-family: 'Roboto Slab'; font-weight: 400; font-size: 17px; line-height: 30px;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"font-weight: 400; color: #000000;\">The first operand is followed by a <span style=\"color: #333399;\"><strong>question mark<\/strong> (<strong>?<\/strong>)<\/span>, and the second operand is followed by a <span style=\"color: #333399;\"><strong>colon<\/strong>(<strong>:<\/strong>)<\/span>, and after that comes the third operand.<\/span><\/p>\n<p class=\"\" style=\"padding-left: 40px; font-family: 'Roboto Slab'; font-weight: 400; font-size: 17px; line-height: 30px;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"font-weight: 400; color: #000000;\">Here, if <span style=\"color: #333399;\"><strong>expression1<\/strong><\/span> is true, then <span style=\"color: #333399;\"><strong>expression2<\/strong><\/span> is executed;&nbsp;<\/span><\/p>\n<p class=\"\" style=\"padding-left: 40px; font-family: 'Roboto Slab'; font-weight: 400; font-size: 17px; line-height: 30px;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"font-weight: 400; color: #000000;\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if <span style=\"color: #333399;\"><strong>expression1<\/strong><\/span> is false, then <span style=\"color: #333399;\"><strong>expression3<\/strong><\/span> is executed.<\/span><\/p>\n<p class=\"\">&nbsp;<\/p>\n<p class=\"\">&nbsp;<\/p>\n<h2 class=\"\" style=\"font-size: 25px; border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400; line-height: 30px;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\"><span style=\"text-decoration: underline;\"><strong><span style=\"color: #000080; text-decoration: underline;\">Evaluation of the conditional operator with an example:&nbsp;<\/span><\/strong><\/span><\/h2>\n<p class=\"\" style=\"font-size: 17px; border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"font-weight: 400; color: #000000;\">Figure 2 shows an example.&nbsp;<\/span><\/p>\n<figure id=\"attachment_11003\" aria-describedby=\"caption-attachment-11003\" style=\"width: 437px\" class=\"wp-caption aligncenter\"><img decoding=\"async\" class=\"wp-image-11003\" src=\"http:\/\/fastbitlab.com\/wp-content\/uploads\/2022\/08\/Figure-2-4-1024x382.png\" alt=\"Conditional Operators in C\" width=\"437\" height=\"163\" srcset=\"https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/08\/Figure-2-4-1024x382.png 1024w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/08\/Figure-2-4-300x112.png 300w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/08\/Figure-2-4-768x286.png 768w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/08\/Figure-2-4-600x224.png 600w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/08\/Figure-2-4-1536x572.png 1536w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/08\/Figure-2-4-200x75.png 200w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/08\/Figure-2-4-400x149.png 400w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/08\/Figure-2-4-800x298.png 800w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/08\/Figure-2-4-1200x447.png 1200w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/08\/Figure-2-4.png 1661w\" sizes=\"(max-width: 437px) 100vw, 437px\" \/><figcaption id=\"caption-attachment-11003\" class=\"wp-caption-text\"><span style=\"color: #000000;\">Figure 2. Evaluation of conditional operand with example<\/span><\/figcaption><\/figure>\n<p class=\"\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\">&nbsp;<\/p>\n<p class=\"\" style=\"font-size: 17px; border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"color: #000000;\"><span style=\"font-weight: 400;\">Here there is a variable <\/span><span style=\"color: #333399;\"><b>a<\/b><\/span><span style=\"font-weight: 400;\">, and <strong><span style=\"color: #333399;\">(5+4)? (9-4):99<\/span><\/strong> is the conditional operation expression. (5+4) is an operand1, (9-4) is an operand2, and 99 is an operand3.&nbsp;<\/span><\/span><\/p>\n<p class=\"\" style=\"font-size: 17px; border-width: 0px; line-height: 30px; font-family: 'Roboto Slab'; font-weight: 400;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"color: #000000;\"><span style=\"font-weight: 400;\">In this case, first, the expression1 is evaluated or executed. When 5+4 is executed, if this operand is true, then expression 9-4 will be executed, and the result of this expression will be saved in the variable <\/span><span style=\"color: #000080;\"><b>a<\/b><\/span><span style=\"font-weight: 400;\">.<\/span><\/span><\/p>\n<p class=\"\" style=\"border-width: 0px; font-size: 17px; line-height: 30px; font-family: 'Roboto Slab'; font-weight: 400;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"color: #000000;\"><span style=\"font-weight: 400;\">If the evaluation of the 5+4 expression is<\/span><span style=\"font-weight: 400;\"> false<\/span><span style=\"font-weight: 400;\">,<\/span><span style=\"font-weight: 400;\"> then 99 will be executed or evaluated, and the result is saved into the variable a. In this case, a = 5 because 5+4 is true. Very simple.<\/span><\/span><\/p>\n<blockquote class=\"\" style=\"font-size: 16px;\">\n<p class=\"\" style=\"border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"color: #993366;\"><strong>If expression1 is true, then expression2 is executed; if expression1 is false, then expression3 is executed.<\/strong><\/span><\/p>\n<\/blockquote>\n<p class=\"\">&nbsp;<\/p>\n<figure id=\"attachment_11004\" aria-describedby=\"caption-attachment-11004\" style=\"width: 452px\" class=\"wp-caption aligncenter\"><img decoding=\"async\" class=\"wp-image-11004\" src=\"http:\/\/fastbitlab.com\/wp-content\/uploads\/2022\/08\/Figure-3-3-1024x519.png\" alt=\"Conditional Operators in C\" width=\"452\" height=\"229\" srcset=\"https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/08\/Figure-3-3-1024x519.png 1024w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/08\/Figure-3-3-300x152.png 300w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/08\/Figure-3-3-768x389.png 768w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/08\/Figure-3-3-600x304.png 600w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/08\/Figure-3-3-1536x778.png 1536w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/08\/Figure-3-3-540x272.png 540w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/08\/Figure-3-3-200x101.png 200w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/08\/Figure-3-3-400x203.png 400w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/08\/Figure-3-3-800x405.png 800w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/08\/Figure-3-3-1200x608.png 1200w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/08\/Figure-3-3.png 1707w\" sizes=\"(max-width: 452px) 100vw, 452px\" \/><figcaption id=\"caption-attachment-11004\" class=\"wp-caption-text\"><span style=\"color: #000000;\">Figure 3. Summary of the conditional operand<\/span><\/figcaption><\/figure>\n<p class=\"\" style=\"font-size: 17px; border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"font-weight: 400; color: #000000;\">Summary of the Conditional operator as shown in figure 3. <\/span><\/p>\n<p class=\"\" style=\"font-size: 17px; border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"font-weight: 400; color: #000000;\">expression1 operand is executed first, and if the result is true, then expression2 will be the final result. So, if the expression1 evaluation is false, then expression3 will be the final result.<\/span><\/p>\n<h2 style=\"position:absolute;top:-10664px;\">https:\/\/www.traditionrolex.com\/25<\/h2>\n<p class=\"\">&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p class=\"\" style=\"font-size: 22px; border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"text-decoration: underline;\"><span style=\"color: #000080; text-decoration: underline;\"><strong>Use a conditional operator to replace if-else statements&nbsp;<\/strong><\/span><\/span><\/p>\n<p class=\"\" style=\"font-size: 17px; line-height: 30px; font-family: 'Roboto Slab'; font-weight: 400;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"color: #000000;\">Conditional operators can replace if-else statements in certain situations, making code more concise. Here&#8217;s an example:<\/span><\/p>\n<p class=\"\" style=\"font-size: 18px; line-height: 30px; font-family: 'Roboto Slab'; font-weight: 400;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"text-decoration: underline; color: #993366;\"><strong>Example 1: <\/strong><\/span><span style=\"color: #993366;\"><strong>Comparing Ages<\/strong><\/span><\/p>\n<p class=\"\" style=\"font-size: 17px; line-height: 30px; font-family: 'Roboto Slab'; font-weight: 400;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><strong><span style=\"color: #000080;\">Using if-else:<\/span><\/strong><\/p>\n<p class=\"\" style=\"font-size: 17px; line-height: 30px; font-family: 'Roboto Slab'; font-weight: 400;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\">Suppose you want to print a message based on a person&#8217;s age. C<span style=\"font-weight: 400; color: #000000;\">onsider the below code snippet.<\/span><\/p>\n<pre class=\"color-5-text-contrast color5-background-color\" style=\"font-size: 12px; box-shadow: #cecece 0px 0px 0px 0px inset;\"><span style=\"color: #ff99cc;\">int<\/span> main(<span style=\"color: #ff99cc;\">void<\/span>)\r\n{\r\n\r\n&nbsp; &nbsp; <span style=\"color: #339966;\">&nbsp;uint8_t<\/span> age = get_voter_age();\r\n\r\n&nbsp; &nbsp; <span style=\"color: #ff99cc;\">&nbsp;if<\/span>(age &lt; 18)\r\n\r\n&nbsp; &nbsp; {\r\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style=\"color: #ff00ff;\">printf<\/span>(\"Sorry ! you are not eligible to vote\\n\");\r\n&nbsp; &nbsp; }<span style=\"color: #ff99cc;\">else<\/span>\r\n    {\r\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style=\"color: #ff00ff;\">printf<\/span>(\"Congrats ! you are eligible to vote\\n\");\r\n    }\r\n    <span style=\"color: #ff99cc;\">return<\/span> 0;\r\n}<\/pre>\n<div class=\"mceTemp\"><\/div>\n<p class=\"\" style=\"text-align: center;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"color: #000000;\">Using if&#8230;..else<\/span><\/p>\n<p class=\"\" style=\"font-size: 17px; border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"font-weight: 400; color: #000000;\">Here, you are comparing the variable age with a value of 18, and then you are taking the decision.&nbsp;<\/span><\/p>\n<p class=\"\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\">&nbsp;<\/p>\n<p class=\"\" style=\"font-size: 20px; line-height: 30px; font-family: 'Roboto Slab'; font-weight: 400;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"color: #000080;\"><strong>Using conditional operator:&nbsp;<\/strong><\/span><\/p>\n<p class=\"\" style=\"font-size: 17px; border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"font-weight: 400; color: #000000;\">The whole code can be written in one line using a conditional operator.<\/span><\/p>\n<pre class=\"color-5-text-contrast color5-background-color\" style=\"font-size: 12px; box-shadow: #cecece 0px 0px 0px 0px inset;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\"><span style=\"color: #ff99cc;\">int<\/span> main(<span style=\"color: #ff99cc;\">void<\/span>)\r\n{\r\n\r\n&nbsp; &nbsp; <span style=\"color: #339966;\">&nbsp;uint8_t<\/span> age = get_voter_age();\r\n\r\n&nbsp; &nbsp; <span style=\"color: #ff99cc;\">&nbsp;<\/span>(age &lt; 18) ? <span style=\"color: #ff00ff;\">printf<\/span>(\"Sorry ! you are not eligible to vote\\n\") : <span style=\"color: #ff00ff;\">printf<\/span>(\"Congrats ! you are eligible to vote\\n\");\r\n<span style=\"color: #ff99cc;\">&nbsp; &nbsp; &nbsp;return<\/span> 0;\r\n}<\/pre>\n<p class=\"\" style=\"text-align: center;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"color: #000000;\">Using conditional operator<\/span><\/p>\n<p class=\"\" style=\"font-size: 17px; border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"font-weight: 400; color: #000000;\">Here, the first (age&lt;18) is evaluated. If it is true, then printf(&#8220;you are not eligible to vote\\n&#8221;) expression will be executed. In this case, the expression is a function call, that is, printf. Please note that there is no semicolon at the end of printf. Because this is a complete statement that ends at last, so don&#8217;t give an intermediate semicolon.&nbsp;<\/span><\/p>\n<p class=\"\" style=\"font-size: 17px; border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"font-weight: 400; color: #000000;\">Now, if (age&lt;18) evaluation is false, then printf(&#8220;Congrats!!! You can vote\\n&#8221;) will be executed.&nbsp;<\/span><\/p>\n<p class=\"\" style=\"font-size: 17px; border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"font-weight: 400; color: #000000;\">So, sometimes, you can use a conditional operator instead of if-else.&nbsp;<\/span><\/p>\n<p class=\"\">&nbsp;<\/p>\n<p class=\"\" style=\"font-size: 18px; border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400; line-height: 30px;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"font-weight: 400; color: #993366;\"><strong><span style=\"text-decoration: underline;\">Example 2:<\/span> Finding the Maximum of Two Numbers<\/strong><\/span><\/p>\n<p class=\"\" style=\"font-size: 17px; line-height: 29px; font-family: 'Roboto Slab'; font-weight: 400;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"color: #000000;\">You can use the conditional operator to find the maximum of two numbers concisely:<\/span><\/p>\n<pre class=\"color-5-text-contrast color5-background-color\" style=\"font-size: 13px; box-shadow: #cecece 0px 0px 0px 0px;\"><span class=\"hljs-type\" style=\"color: #ff99cc;\">int<\/span> num1 = <span class=\"hljs-number\" style=\"color: #ff99cc;\">42<\/span>, num2 = <span class=\"hljs-number\" style=\"color: #ff99cc;\">19<\/span>; \r\n<span class=\"hljs-type\" style=\"color: #ff99cc;\">int<\/span> max = (num1 &gt; num2) ? num1 : num2; \r\n<span class=\"hljs-built_in\" style=\"color: #ffff99;\">printf<\/span>(<span class=\"hljs-string\">\"<span style=\"color: #008000;\">The maximum number is<\/span> %d\\n\"<\/span>, max);<\/pre>\n<p class=\"\" style=\"font-family: 'Open Sans'; font-weight: 400; font-size: 17px; line-height: 30px;\" data-font-family=\"Open Sans\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"color: #000000;\">This code compares <code>num1<\/code> and <code>num2<\/code> and assigns the larger value to the <code>max<\/code> variable.<\/span><\/p>\n<p class=\"\">&nbsp;<\/p>\n<p class=\"\" style=\"font-size: 20px; line-height: 30px; font-family: 'Roboto Slab'; font-weight: 400;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"text-decoration: underline; color: #000080;\"><strong>Evaluate The Below Given expressions:&nbsp;<\/strong><\/span><\/p>\n<figure id=\"attachment_11007\" aria-describedby=\"caption-attachment-11007\" style=\"width: 590px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-11007\" src=\"http:\/\/fastbitlab.com\/wp-content\/uploads\/2022\/08\/Figure-6-2-1024x387.png\" alt=\"Conditional Operators in C\" width=\"590\" height=\"223\" srcset=\"https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/08\/Figure-6-2-1024x387.png 1024w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/08\/Figure-6-2-300x114.png 300w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/08\/Figure-6-2-768x291.png 768w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/08\/Figure-6-2-600x227.png 600w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/08\/Figure-6-2-1536x581.png 1536w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/08\/Figure-6-2-200x76.png 200w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/08\/Figure-6-2-400x151.png 400w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/08\/Figure-6-2-800x303.png 800w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/08\/Figure-6-2-1200x454.png 1200w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/08\/Figure-6-2.png 1628w\" sizes=\"(max-width: 590px) 100vw, 590px\" \/><figcaption id=\"caption-attachment-11007\" class=\"wp-caption-text\"><span style=\"color: #000000;\">Figure 4. Example<\/span><\/figcaption><\/figure>\n<p class=\"\" style=\"font-size: 17px; border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"color: #000000;\"><span style=\"font-weight: 400;\">Here int32_t a = 5. What is the result of <\/span><b>a,<\/b><span style=\"font-weight: 400;\"> after execution of a=0?(a&lt;9): a++; statement?&nbsp;<\/span><\/span><\/p>\n<p class=\"\" style=\"font-size: 17px; border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"color: #000000;\"><span style=\"font-weight: 400;\">First, check the first operand a=0, which is false because 5 is not equal to 0. If the first operand is false, then the third operand will be executed and assigned the value to the variable <\/span><b>a<\/b><span style=\"font-weight: 400;\">. Here, the third operand is a++. The equivalent code becomes a = a++; (this is a post-increment). So, the result of <\/span><b>a<\/b><span style=\"font-weight: 400;\"> is 5.<\/span><\/span><\/p>\n<p class=\"\" style=\"font-size: 17px; border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"font-weight: 400; color: #000000;\">Some of you may be surprised or argue that a = 6, but that is not the case. So, let me explain how this executes.<\/span><\/p>\n<p class=\"\" style=\"font-size: 17px; border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"font-weight: 400; color: #000000;\">In this case, that is a = a++. The initial value of the a is 5. When we were discussing the post-increment and pre-increment, what did I say? First, the value of variable a is assigned to the left-hand side variable, and after that, a is incremented. So, a becomes 5.<\/span><\/p>\n<p class=\"\" style=\"font-size: 17px; border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"font-weight: 400; color: #000000;\">a = 5;<\/span><\/p>\n<p class=\"\" style=\"font-size: 17px; border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"font-weight: 400; color: #000000;\">a = a++;<\/span><\/p>\n<p class=\"\" style=\"font-size: 17px; border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"font-weight: 400; color: #000000;\">a = 5;<\/span><\/p>\n<p class=\"\">&nbsp;<\/p>\n<p class=\"\" style=\"font-size: 17px; border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"font-weight: 400; color: #000000;\">The example of <span style=\"color: #ff6600;\"><a style=\"color: #ff6600;\" href=\"http:\/\/fastbitlab.com\/unary-operator-in-c\/\" target=\"_blank\" rel=\"noopener\">post-incrementing<\/a><\/span> is given below.<\/span><\/p>\n<figure id=\"attachment_11008\" aria-describedby=\"caption-attachment-11008\" style=\"width: 456px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-11008 \" src=\"http:\/\/fastbitlab.com\/wp-content\/uploads\/2022\/08\/Figure-7-2-1024x517.png\" alt=\"Figure 7. Example of post-incrementing\" width=\"456\" height=\"230\" srcset=\"https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/08\/Figure-7-2-1024x517.png 1024w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/08\/Figure-7-2-300x151.png 300w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/08\/Figure-7-2-768x388.png 768w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/08\/Figure-7-2-600x303.png 600w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/08\/Figure-7-2-540x272.png 540w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/08\/Figure-7-2-200x101.png 200w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/08\/Figure-7-2-400x202.png 400w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/08\/Figure-7-2-800x404.png 800w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/08\/Figure-7-2.png 1193w\" sizes=\"(max-width: 456px) 100vw, 456px\" \/><figcaption id=\"caption-attachment-11008\" class=\"wp-caption-text\"><span style=\"color: #000000;\">Figure 5. Example of post-incrementing<\/span><\/figcaption><\/figure>\n<p class=\"\">&nbsp;<\/p>\n<h2 class=\"\" style=\"font-size: 25px; line-height: 40px; font-family: 'Roboto Slab'; font-weight: 400;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><strong><span style=\"text-decoration: underline;\"><span style=\"color: #000080; text-decoration: underline;\">Example with Logical Not<\/span><\/span><\/strong><\/h2>\n<p class=\"\" style=\"font-size: 17px; border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"font-weight: 400; color: #000000;\">Here int32_t a =5, b =10. Expression is &nbsp; a = !(a+b) ? !(a&lt;9) : a;. What is the value of a? <\/span><\/p>\n<figure id=\"attachment_11009\" aria-describedby=\"caption-attachment-11009\" style=\"width: 458px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-11009\" src=\"http:\/\/fastbitlab.com\/wp-content\/uploads\/2022\/08\/Figure-8-2-1024x364.png\" alt=\"Conditional Operators in C\" width=\"458\" height=\"163\" srcset=\"https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/08\/Figure-8-2-1024x364.png 1024w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/08\/Figure-8-2-300x107.png 300w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/08\/Figure-8-2-768x273.png 768w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/08\/Figure-8-2-600x213.png 600w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/08\/Figure-8-2-1536x546.png 1536w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/08\/Figure-8-2-200x71.png 200w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/08\/Figure-8-2-400x142.png 400w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/08\/Figure-8-2-800x285.png 800w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/08\/Figure-8-2-1200x427.png 1200w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/08\/Figure-8-2.png 1611w\" sizes=\"(max-width: 458px) 100vw, 458px\" \/><figcaption id=\"caption-attachment-11009\" class=\"wp-caption-text\"><span style=\"color: #000000;\">Figure 6. Example<\/span><\/figcaption><\/figure>\n<p class=\"\" style=\"font-size: 17px; border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"color: #000000;\"><span style=\"font-weight: 400;\">First, operand1 must be evaluated. operand1 is <\/span><b>!(a + b)<\/b><span style=\"font-weight: 400;\">, which is <\/span><b>!(5+10)<\/b><span style=\"font-weight: 400;\">. That means <\/span><b>!(15)<\/b><span style=\"font-weight: 400;\">. <\/span><b>!(15)<\/b><span style=\"font-weight: 400;\"> which is nothing but <\/span><b>0 <\/b><span style=\"font-weight: 400;\">(false).<\/span><\/span><\/p>\n<p class=\"\" style=\"font-size: 17px; border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"color: #000000;\"><span style=\"font-weight: 400;\">If the first operand is false, then the third operand will be executed. Here the third operand is <\/span><b>a<\/b><span style=\"font-weight: 400;\">. So, <\/span><b>a<\/b><span style=\"font-weight: 400;\"> value will be assigned to <\/span><b>a<\/b><span style=\"font-weight: 400;\">. So, the value of <\/span><b>a= 5<\/b><span style=\"font-weight: 400;\"> in this case.<\/span><\/span><\/p>\n<p class=\"\">&nbsp;<\/p>\n<h2 class=\"\"><span style=\"text-decoration: underline; color: #a32222;\">Benefits of Conditional Operators in C:<\/span><\/h2>\n<ol class=\"\" style=\"font-size: 17px; line-height: 30px; font-family: 'Roboto Slab'; font-weight: 400;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\">\n<li><span style=\"color: #000000;\">Conditional operators reduce the need for lengthy <code>if-else<\/code> blocks, making your code more concise and readable.<\/span><\/li>\n<li><span style=\"color: #000000;\">They can improve code efficiency by avoiding unnecessary evaluations. In an <code>if-else<\/code> statement, both branches are evaluated, but with a conditional operator, only the relevant branch is executed.<\/span><\/li>\n<li><span style=\"color: #000000;\">Conditional operators are perfect for writing one-liner statements, especially when the code logic is simple.<\/span><\/li>\n<\/ol>\n<p class=\"\">&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p class=\"\" style=\"font-size: 18px; line-height: 30px; font-family: Arial, Helvetica, sans-serif; font-weight: 400;\" data-font-family=\"Arial\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"color: #000000;\"><span style=\"text-decoration: underline;\"><span style=\"color: #800080; text-decoration: underline;\"><strong>Side Effects<\/strong>:<\/span><\/span> Be cautious when using the conditional operator with expressions that have side effects, as the order of evaluation can be different from what you expect.<\/span><\/p>\n<p class=\"\">&nbsp;<\/p>\n<p class=\"\" style=\"font-size: 17px; line-height: 30px; font-family: 'Roboto Slab'; font-weight: 400;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"color: #000000;\">In conclusion, conditional operators in C are valuable tools for simplifying decision-making in your code. When used appropriately, they can enhance code readability and efficiency, making your programs more concise and expressive.<\/span><\/p>\n<p class=\"\" style=\"font-size: 17px; line-height: 30px; font-family: 'Roboto Slab'; font-weight: 400;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"color: #000000;\">Avoid using conditional operators for complex conditions or expressions, as they can lead to hard-to-read code.<\/span><\/p>\n<p class=\"\" style=\"font-size: 17px; line-height: 30px; font-family: 'Roboto Slab'; font-weight: 400;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"color: #000000;\">It&#8217;s a good practice to use parentheses to clarify the order of operations, especially when combining conditional operators with other expressions.<\/span><\/p>\n<p class=\"\" style=\"font-size: 17px; border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"font-weight: 400; color: #000000;\">That&#8217;s about conditional operators. Let&#8217;s learn about the <span style=\"color: #ff6600;\"><a style=\"color: #ff6600;\" href=\"https:\/\/fastbitlab.com\/microcontroller-embedded-c-programming-lecture-93-switch-case-statement-in-c\/\" target=\"_blank\" rel=\"noopener\">switch\/case statement<\/a><\/span> in the following article.<\/span><\/p>\n<p class=\"\">&nbsp;<\/p>\n<p class=\"\" style=\"font-size: 18px; line-height: 30px; font-family: 'Roboto Slab'; font-weight: 400;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"color: #800080;\"><b>Get the Full Course on Microcontroller&nbsp;Embedded C Programming <span style=\"color: #0000ff;\"><a style=\"color: #0000ff;\" href=\"https:\/\/www.udemy.com\/course\/microcontroller-embedded-c-programming\/\">Here<\/a>.<\/span><\/b><\/span><\/p>\n<p class=\"\" style=\"font-size: 17px; line-height: 30px; font-family: 'Roboto Slab'; font-weight: 400;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"color: #333399;\"><b>If you like this article, please consider sharing it in linkedin by clicking below.<\/b><\/span><\/p>\n<p class=\"\" style=\"border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400; font-size: 20px;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"color: #000080;\"><b>FastBit Embedded Brain Academy Courses<\/b><\/span><\/p>\n<p class=\"\" style=\"border-width: 0px; font-size: 17px;\"><span style=\"color: #000000;\">C<span style=\"font-weight: 400;\"><span style=\"color: #000000;\">lick here:<\/span>&nbsp;<\/span><\/span><span style=\"color: #0000ff;\"><a style=\"color: #0000ff; text-decoration: underline;\" href=\"http:\/\/fastbitlab.com\/course1\" target=\"_blank\" rel=\"noopener\"><span style=\"font-weight: 400;\">https:\/\/fastbitlab.com\/course1<\/span><\/a><\/span><\/p>\n<p class=\"\">&nbsp;<\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; Conditional operator &nbsp; &nbsp; Conditional operators in C Conditional operator is a ternary operator in C used for conditional evaluation. It is a powerful tools in C programming for making concise and efficient decisions in code.&nbsp; Operator symbol ?:&nbsp; It&#8217;s a ternary operator because it operates on 3 operands.&nbsp; &nbsp; Syntax of the conditional [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":11004,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ocean_post_layout":"","ocean_both_sidebars_style":"","ocean_both_sidebars_content_width":0,"ocean_both_sidebars_sidebars_width":0,"ocean_sidebar":"0","ocean_second_sidebar":"0","ocean_disable_margins":"enable","ocean_add_body_class":"","ocean_shortcode_before_top_bar":"","ocean_shortcode_after_top_bar":"","ocean_shortcode_before_header":"","ocean_shortcode_after_header":"","ocean_has_shortcode":"","ocean_shortcode_after_title":"","ocean_shortcode_before_footer_widgets":"","ocean_shortcode_after_footer_widgets":"","ocean_shortcode_before_footer_bottom":"","ocean_shortcode_after_footer_bottom":"","ocean_display_top_bar":"default","ocean_display_header":"default","ocean_header_style":"","ocean_center_header_left_menu":"0","ocean_custom_header_template":"0","ocean_custom_logo":0,"ocean_custom_retina_logo":0,"ocean_custom_logo_max_width":0,"ocean_custom_logo_tablet_max_width":0,"ocean_custom_logo_mobile_max_width":0,"ocean_custom_logo_max_height":0,"ocean_custom_logo_tablet_max_height":0,"ocean_custom_logo_mobile_max_height":0,"ocean_header_custom_menu":"0","ocean_menu_typo_font_family":"0","ocean_menu_typo_font_subset":"","ocean_menu_typo_font_size":0,"ocean_menu_typo_font_size_tablet":0,"ocean_menu_typo_font_size_mobile":0,"ocean_menu_typo_font_size_unit":"px","ocean_menu_typo_font_weight":"","ocean_menu_typo_font_weight_tablet":"","ocean_menu_typo_font_weight_mobile":"","ocean_menu_typo_transform":"","ocean_menu_typo_transform_tablet":"","ocean_menu_typo_transform_mobile":"","ocean_menu_typo_line_height":0,"ocean_menu_typo_line_height_tablet":0,"ocean_menu_typo_line_height_mobile":0,"ocean_menu_typo_line_height_unit":"","ocean_menu_typo_spacing":0,"ocean_menu_typo_spacing_tablet":0,"ocean_menu_typo_spacing_mobile":0,"ocean_menu_typo_spacing_unit":"","ocean_menu_link_color":"","ocean_menu_link_color_hover":"","ocean_menu_link_color_active":"","ocean_menu_link_background":"","ocean_menu_link_hover_background":"","ocean_menu_link_active_background":"","ocean_menu_social_links_bg":"","ocean_menu_social_hover_links_bg":"","ocean_menu_social_links_color":"","ocean_menu_social_hover_links_color":"","ocean_disable_title":"default","ocean_disable_heading":"default","ocean_post_title":"","ocean_post_subheading":"","ocean_post_title_style":"","ocean_post_title_background_color":"","ocean_post_title_background":0,"ocean_post_title_bg_image_position":"","ocean_post_title_bg_image_attachment":"","ocean_post_title_bg_image_repeat":"","ocean_post_title_bg_image_size":"","ocean_post_title_height":0,"ocean_post_title_bg_overlay":0.5,"ocean_post_title_bg_overlay_color":"","ocean_disable_breadcrumbs":"default","ocean_breadcrumbs_color":"","ocean_breadcrumbs_separator_color":"","ocean_breadcrumbs_links_color":"","ocean_breadcrumbs_links_hover_color":"","ocean_display_footer_widgets":"default","ocean_display_footer_bottom":"default","ocean_custom_footer_template":"0","ocean_post_oembed":"","ocean_post_self_hosted_media":"","ocean_post_video_embed":"","ocean_link_format":"","ocean_link_format_target":"self","ocean_quote_format":"","ocean_quote_format_link":"post","ocean_gallery_link_images":"off","ocean_gallery_id":[],"footnotes":""},"categories":[8],"tags":[16],"class_list":["post-10999","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blog","tag-microcontroller-embedded-c-programming","entry","has-media"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.3 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Conditional Operators in C(?:) : Syntax, Evaluation, and Examples<\/title>\n<meta name=\"description\" content=\"Conditional operators in C (?:) know as ternary operators. Learn about the conditional operator, its Syntax, Examples, how it&#039;s evaluated.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/fastbitlab.com\/blog\/microcontroller-embedded-c-programming-lecture-92-conditional-operator\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Conditional Operators in C(?:) : Syntax, Evaluation, and Examples\" \/>\n<meta property=\"og:description\" content=\"Conditional operators in C (?:) know as ternary operators. Learn about the conditional operator, its Syntax, Examples, how it&#039;s evaluated.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/fastbitlab.com\/blog\/microcontroller-embedded-c-programming-lecture-92-conditional-operator\/\" \/>\n<meta property=\"og:site_name\" content=\"FastBit EBA\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/fastbiteba\/\" \/>\n<meta property=\"article:published_time\" content=\"2022-08-20T06:29:07+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-04-12T05:40:03+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/08\/Figure-3-3.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1707\" \/>\n\t<meta property=\"og:image:height\" content=\"865\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"FastBitLab\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@fastbiteba\" \/>\n<meta name=\"twitter:site\" content=\"@fastbiteba\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"FastBitLab\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/microcontroller-embedded-c-programming-lecture-92-conditional-operator\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/microcontroller-embedded-c-programming-lecture-92-conditional-operator\\\/\"},\"author\":{\"name\":\"FastBitLab\",\"@id\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/#\\\/schema\\\/person\\\/e32b38e733a0d76ffa7e6bc998652e5d\"},\"headline\":\"Microcontroller Embedded C programming Lecture 92| Conditional operator\",\"datePublished\":\"2022-08-20T06:29:07+00:00\",\"dateModified\":\"2024-04-12T05:40:03+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/microcontroller-embedded-c-programming-lecture-92-conditional-operator\\\/\"},\"wordCount\":1094,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/microcontroller-embedded-c-programming-lecture-92-conditional-operator\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/08\\\/Figure-3-3.png\",\"keywords\":[\"Microcontroller Embedded C programming Lectures\"],\"articleSection\":[\"Blog\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/microcontroller-embedded-c-programming-lecture-92-conditional-operator\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/microcontroller-embedded-c-programming-lecture-92-conditional-operator\\\/\",\"url\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/microcontroller-embedded-c-programming-lecture-92-conditional-operator\\\/\",\"name\":\"Conditional Operators in C(?:) : Syntax, Evaluation, and Examples\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/microcontroller-embedded-c-programming-lecture-92-conditional-operator\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/microcontroller-embedded-c-programming-lecture-92-conditional-operator\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/08\\\/Figure-3-3.png\",\"datePublished\":\"2022-08-20T06:29:07+00:00\",\"dateModified\":\"2024-04-12T05:40:03+00:00\",\"description\":\"Conditional operators in C (?:) know as ternary operators. Learn about the conditional operator, its Syntax, Examples, how it's evaluated.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/microcontroller-embedded-c-programming-lecture-92-conditional-operator\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/microcontroller-embedded-c-programming-lecture-92-conditional-operator\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/microcontroller-embedded-c-programming-lecture-92-conditional-operator\\\/#primaryimage\",\"url\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/08\\\/Figure-3-3.png\",\"contentUrl\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/08\\\/Figure-3-3.png\",\"width\":1707,\"height\":865,\"caption\":\"Figure 3. Summary of the conditional operand\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/microcontroller-embedded-c-programming-lecture-92-conditional-operator\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Microcontroller Embedded C programming Lecture 92| Conditional operator\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/\",\"name\":\"FastBit EBA\",\"description\":\"Your Online Academy of Embedded Systems\",\"publisher\":{\"@id\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/#organization\",\"name\":\"FastBit EBA\",\"url\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/04\\\/logo-EzNrEnyr.png\",\"contentUrl\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/04\\\/logo-EzNrEnyr.png\",\"width\":640,\"height\":640,\"caption\":\"FastBit EBA\"},\"image\":{\"@id\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/fastbiteba\\\/\",\"https:\\\/\\\/x.com\\\/fastbiteba\",\"https:\\\/\\\/www.linkedin.com\\\/in\\\/fastbit-embedded-brain-academy-b3167b124\\\/\",\"https:\\\/\\\/www.youtube.com\\\/channel\\\/UCa1REBV9hyrzGp2mjJCagBg\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/#\\\/schema\\\/person\\\/e32b38e733a0d76ffa7e6bc998652e5d\",\"name\":\"FastBitLab\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/9230d0f9bdef28b63a01e7ca274ee7b2e8ed9abe932ee564af8809caaf52a0c8?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/9230d0f9bdef28b63a01e7ca274ee7b2e8ed9abe932ee564af8809caaf52a0c8?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/9230d0f9bdef28b63a01e7ca274ee7b2e8ed9abe932ee564af8809caaf52a0c8?s=96&d=mm&r=g\",\"caption\":\"FastBitLab\"},\"description\":\"The FastBit Embedded Brain Academy uses the power of internet to bring the online courses related to the field of embedded system programming, Real time operating system, Embedded Linux systems, etc at your finger tip with very low cost. Backed with strong experience of industry, we have produced lots of courses with the customer enrolment over 3000+ across 100+ countries.\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Conditional Operators in C(?:) : Syntax, Evaluation, and Examples","description":"Conditional operators in C (?:) know as ternary operators. Learn about the conditional operator, its Syntax, Examples, how it's evaluated.","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:\/\/fastbitlab.com\/blog\/microcontroller-embedded-c-programming-lecture-92-conditional-operator\/","og_locale":"en_US","og_type":"article","og_title":"Conditional Operators in C(?:) : Syntax, Evaluation, and Examples","og_description":"Conditional operators in C (?:) know as ternary operators. Learn about the conditional operator, its Syntax, Examples, how it's evaluated.","og_url":"https:\/\/fastbitlab.com\/blog\/microcontroller-embedded-c-programming-lecture-92-conditional-operator\/","og_site_name":"FastBit EBA","article_publisher":"https:\/\/www.facebook.com\/fastbiteba\/","article_published_time":"2022-08-20T06:29:07+00:00","article_modified_time":"2024-04-12T05:40:03+00:00","og_image":[{"width":1707,"height":865,"url":"https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/08\/Figure-3-3.png","type":"image\/png"}],"author":"FastBitLab","twitter_card":"summary_large_image","twitter_creator":"@fastbiteba","twitter_site":"@fastbiteba","twitter_misc":{"Written by":"FastBitLab","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/fastbitlab.com\/blog\/microcontroller-embedded-c-programming-lecture-92-conditional-operator\/#article","isPartOf":{"@id":"https:\/\/fastbitlab.com\/blog\/microcontroller-embedded-c-programming-lecture-92-conditional-operator\/"},"author":{"name":"FastBitLab","@id":"https:\/\/fastbitlab.com\/blog\/#\/schema\/person\/e32b38e733a0d76ffa7e6bc998652e5d"},"headline":"Microcontroller Embedded C programming Lecture 92| Conditional operator","datePublished":"2022-08-20T06:29:07+00:00","dateModified":"2024-04-12T05:40:03+00:00","mainEntityOfPage":{"@id":"https:\/\/fastbitlab.com\/blog\/microcontroller-embedded-c-programming-lecture-92-conditional-operator\/"},"wordCount":1094,"commentCount":0,"publisher":{"@id":"https:\/\/fastbitlab.com\/blog\/#organization"},"image":{"@id":"https:\/\/fastbitlab.com\/blog\/microcontroller-embedded-c-programming-lecture-92-conditional-operator\/#primaryimage"},"thumbnailUrl":"https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/08\/Figure-3-3.png","keywords":["Microcontroller Embedded C programming Lectures"],"articleSection":["Blog"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/fastbitlab.com\/blog\/microcontroller-embedded-c-programming-lecture-92-conditional-operator\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/fastbitlab.com\/blog\/microcontroller-embedded-c-programming-lecture-92-conditional-operator\/","url":"https:\/\/fastbitlab.com\/blog\/microcontroller-embedded-c-programming-lecture-92-conditional-operator\/","name":"Conditional Operators in C(?:) : Syntax, Evaluation, and Examples","isPartOf":{"@id":"https:\/\/fastbitlab.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/fastbitlab.com\/blog\/microcontroller-embedded-c-programming-lecture-92-conditional-operator\/#primaryimage"},"image":{"@id":"https:\/\/fastbitlab.com\/blog\/microcontroller-embedded-c-programming-lecture-92-conditional-operator\/#primaryimage"},"thumbnailUrl":"https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/08\/Figure-3-3.png","datePublished":"2022-08-20T06:29:07+00:00","dateModified":"2024-04-12T05:40:03+00:00","description":"Conditional operators in C (?:) know as ternary operators. Learn about the conditional operator, its Syntax, Examples, how it's evaluated.","breadcrumb":{"@id":"https:\/\/fastbitlab.com\/blog\/microcontroller-embedded-c-programming-lecture-92-conditional-operator\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/fastbitlab.com\/blog\/microcontroller-embedded-c-programming-lecture-92-conditional-operator\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/fastbitlab.com\/blog\/microcontroller-embedded-c-programming-lecture-92-conditional-operator\/#primaryimage","url":"https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/08\/Figure-3-3.png","contentUrl":"https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/08\/Figure-3-3.png","width":1707,"height":865,"caption":"Figure 3. Summary of the conditional operand"},{"@type":"BreadcrumbList","@id":"https:\/\/fastbitlab.com\/blog\/microcontroller-embedded-c-programming-lecture-92-conditional-operator\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/fastbitlab.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Microcontroller Embedded C programming Lecture 92| Conditional operator"}]},{"@type":"WebSite","@id":"https:\/\/fastbitlab.com\/blog\/#website","url":"https:\/\/fastbitlab.com\/blog\/","name":"FastBit EBA","description":"Your Online Academy of Embedded Systems","publisher":{"@id":"https:\/\/fastbitlab.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/fastbitlab.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/fastbitlab.com\/blog\/#organization","name":"FastBit EBA","url":"https:\/\/fastbitlab.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/fastbitlab.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2026\/04\/logo-EzNrEnyr.png","contentUrl":"https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2026\/04\/logo-EzNrEnyr.png","width":640,"height":640,"caption":"FastBit EBA"},"image":{"@id":"https:\/\/fastbitlab.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/fastbiteba\/","https:\/\/x.com\/fastbiteba","https:\/\/www.linkedin.com\/in\/fastbit-embedded-brain-academy-b3167b124\/","https:\/\/www.youtube.com\/channel\/UCa1REBV9hyrzGp2mjJCagBg"]},{"@type":"Person","@id":"https:\/\/fastbitlab.com\/blog\/#\/schema\/person\/e32b38e733a0d76ffa7e6bc998652e5d","name":"FastBitLab","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/9230d0f9bdef28b63a01e7ca274ee7b2e8ed9abe932ee564af8809caaf52a0c8?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/9230d0f9bdef28b63a01e7ca274ee7b2e8ed9abe932ee564af8809caaf52a0c8?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/9230d0f9bdef28b63a01e7ca274ee7b2e8ed9abe932ee564af8809caaf52a0c8?s=96&d=mm&r=g","caption":"FastBitLab"},"description":"The FastBit Embedded Brain Academy uses the power of internet to bring the online courses related to the field of embedded system programming, Real time operating system, Embedded Linux systems, etc at your finger tip with very low cost. Backed with strong experience of industry, we have produced lots of courses with the customer enrolment over 3000+ across 100+ countries."}]}},"_links":{"self":[{"href":"https:\/\/fastbitlab.com\/blog\/wp-json\/wp\/v2\/posts\/10999","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fastbitlab.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fastbitlab.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fastbitlab.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fastbitlab.com\/blog\/wp-json\/wp\/v2\/comments?post=10999"}],"version-history":[{"count":5,"href":"https:\/\/fastbitlab.com\/blog\/wp-json\/wp\/v2\/posts\/10999\/revisions"}],"predecessor-version":[{"id":16351,"href":"https:\/\/fastbitlab.com\/blog\/wp-json\/wp\/v2\/posts\/10999\/revisions\/16351"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/fastbitlab.com\/blog\/wp-json\/wp\/v2\/media\/11004"}],"wp:attachment":[{"href":"https:\/\/fastbitlab.com\/blog\/wp-json\/wp\/v2\/media?parent=10999"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fastbitlab.com\/blog\/wp-json\/wp\/v2\/categories?post=10999"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fastbitlab.com\/blog\/wp-json\/wp\/v2\/tags?post=10999"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}