{"id":6878,"date":"2025-03-22T00:01:34","date_gmt":"2025-03-22T07:01:34","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=6878"},"modified":"2025-03-15T11:33:00","modified_gmt":"2025-03-15T18:33:00","slug":"to-prefix-or-postfix","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=6878","title":{"rendered":"To Prefix or Postfix"},"content":{"rendered":"<p>Being curious, I asked ChatGPT which C programming questions it gets asked most frequently. Some of the topics are complex, such as back peddling through a linked list. I cover double-linked lists <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=2905\">on this blog<\/a>, though I don&#8217;t demonstrate how to work backwards through one.<br \/>\n<!--more--><br \/>\nAccording to ChatGPT, one of the frequent questions is about the increment and decrement operators, specifically prefixing and postfixing. This topic is more inline with an issue a beginning C programmer would face.<\/p>\n<p>When prefixed, the increment\/decrement operators affect the operand (variable) before its value is accessed.<\/p>\n<p>For example, if <code>x<\/code> is set to zero, <code>++x<\/code> sets the value to 1, then the value is used.<\/p>\n<p>If <code>x<\/code> is set to zero, <code>x++<\/code> results in the value zero when <code>x<\/code> is accessed, but 1 afterwards.<\/p>\n<p>The confusion probably arises from the fact that as a statement by itself it makes no difference whether you use <code>++x<\/code> or <code>x++<\/code>. The result is always going to be one greater than the value of <code>x<\/code>. But when used in an expression, such as part of a looping condition, the prefix and postfix positions definitely play a role.<\/p>\n<p>The following code runs two <em>while<\/em> loops. Each outputs values of variable <code>x<\/code> initialized to zero. The first uses a prefix operator in the looping condition, the second uses a postfix.<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2025_03_22-Lesson.c\" rel=\"noopener\" target=\"_blank\">2025_03_22-Lesson.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    const int count = 10;\r\n    int x;\r\n\r\n    <span class=\"comments\">\/* prefixed *\/<\/span>\r\n    x = 0;\r\n    puts(\"Prefixed: ++x\");\r\n    while( ++x&lt;count )\r\n        printf(\"%d\\n\",x);\r\n\r\n    <span class=\"comments\">\/* postfixed *\/<\/span>\r\n    puts(\"Postfixed: x++\");\r\n    x = 0;\r\n    while( x++&lt;count )\r\n        printf(\"%d\\n\",x);\r\n\r\n    return 0;\r\n}<\/pre>\n<p>As a prefix operator in the first <em>while<\/em> loop uses the expression (<code>++x&lt;count<\/code>), the value of <code>x<\/code> is incremented before the comparison is made with constant <code>count<\/code>.<\/p>\n<p>When used as a postfix operator in the second <em>while<\/em> loop (<code>x++&lt;count<\/code>), the value of <code>x<\/code> is incremented <em>after<\/em> the expression is evaluated: It&#8217;s compared with variable <code>count<\/code>, then it&#8217;s incremented.<\/p>\n<p>Here is the output:<\/p>\n<p><code>Prefixed: ++x<br \/>\n1<br \/>\n2<br \/>\n3<br \/>\n4<br \/>\n5<br \/>\n6<br \/>\n7<br \/>\n8<br \/>\n9<br \/>\nPostfixed: x++<br \/>\n1<br \/>\n2<br \/>\n3<br \/>\n4<br \/>\n5<br \/>\n6<br \/>\n7<br \/>\n8<br \/>\n9<br \/>\n10<\/code><\/p>\n<p>The first loop outputs nine values, meaning that the loop repeats only nine times, not ten. The second loop outputs ten values. In both cases, the first value output is 1 because variable <code>x<\/code> is already incremented.<\/p>\n<p>The moral of this story is to always use the postfix operator in a looping expression if you want the loop to repeat the full count. In fact, I see the postfix operator used more often than the prefix operator in this situation. This confusion is probably one of the reasons ChatGPT gets asked this question often. Comparatively, the <code>++x<\/code> operation just looks odd.<\/p>\n<p>Because after-loop operations may rely upon the looping variable to be one greater than the count, it&#8217;s important to know which operation comes first. For example, when using a loop to process a string, the final looping value can be used to cap the null character on the string.<\/p>\n<p>I&#8217;m curious whether any situation exists when the prefix operator is necessary in an expression. It&#8217;s available, but I don&#8217;t recall seeing any instance where it&#8217;s required. As someone who likes to write readable code, I often don&#8217;t bunch up operations as was done in this Lesson&#8217;s sample code. I prefer to use separate statements to avoid such confusion.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Understanding the difference between <code>++x<\/code> and <code>x++<\/code> is necessary when you want to code things correctly. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6878\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-6878","post","type-post","status-publish","format-standard","hentry","category-main"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6878","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6878"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6878\/revisions"}],"predecessor-version":[{"id":6891,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6878\/revisions\/6891"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6878"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6878"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6878"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}