How the answer of this for loop is 5?












3















I was practicing my C skills online. And I got a question like:



What is the output of this program?



void main(){
int i;
for(i=1;i++<=1;i++)
i++;
printf("%d",i);
}


The answer was 5. But I thought the for loop would execute endlessly. As the i will be incremented on each iteration and i will be never less than or equal to 1. how come 5 will be the output of this program?










share|improve this question























  • Possible duplicate of Undefined behavior and sequence points

    – Ken White
    Nov 22 '18 at 4:47











  • The proper declarations for main are int main (void) and int main (int argc, char **argv) (which you will see written with the equivalent char *argv). note: main is a function of type int and it returns a value. See: C11 Standard §5.1.2.2.1 Program startup p1 (draft n1570). See also: See What should main() return in C and C++?

    – David C. Rankin
    Nov 22 '18 at 4:51











  • @DavidC.Rankin What makes you think the OP is using a hosted system compiler? See What should main() return in C and C++?

    – Lundin
    Nov 22 '18 at 7:44











  • Why should it execute endlessly if you assume the condition will never be true? If your assumption was correct, it should not execute at all.

    – Gerhardh
    Nov 22 '18 at 8:24













  • @Lundin - I'm just a simple man, all I can do is read the standard unless the OP provides specifics. Perhaps we need to drop you same comment below each referencing What should main() return in C and C++??

    – David C. Rankin
    Nov 22 '18 at 18:29
















3















I was practicing my C skills online. And I got a question like:



What is the output of this program?



void main(){
int i;
for(i=1;i++<=1;i++)
i++;
printf("%d",i);
}


The answer was 5. But I thought the for loop would execute endlessly. As the i will be incremented on each iteration and i will be never less than or equal to 1. how come 5 will be the output of this program?










share|improve this question























  • Possible duplicate of Undefined behavior and sequence points

    – Ken White
    Nov 22 '18 at 4:47











  • The proper declarations for main are int main (void) and int main (int argc, char **argv) (which you will see written with the equivalent char *argv). note: main is a function of type int and it returns a value. See: C11 Standard §5.1.2.2.1 Program startup p1 (draft n1570). See also: See What should main() return in C and C++?

    – David C. Rankin
    Nov 22 '18 at 4:51











  • @DavidC.Rankin What makes you think the OP is using a hosted system compiler? See What should main() return in C and C++?

    – Lundin
    Nov 22 '18 at 7:44











  • Why should it execute endlessly if you assume the condition will never be true? If your assumption was correct, it should not execute at all.

    – Gerhardh
    Nov 22 '18 at 8:24













  • @Lundin - I'm just a simple man, all I can do is read the standard unless the OP provides specifics. Perhaps we need to drop you same comment below each referencing What should main() return in C and C++??

    – David C. Rankin
    Nov 22 '18 at 18:29














3












3








3


1






I was practicing my C skills online. And I got a question like:



What is the output of this program?



void main(){
int i;
for(i=1;i++<=1;i++)
i++;
printf("%d",i);
}


The answer was 5. But I thought the for loop would execute endlessly. As the i will be incremented on each iteration and i will be never less than or equal to 1. how come 5 will be the output of this program?










share|improve this question














I was practicing my C skills online. And I got a question like:



What is the output of this program?



void main(){
int i;
for(i=1;i++<=1;i++)
i++;
printf("%d",i);
}


The answer was 5. But I thought the for loop would execute endlessly. As the i will be incremented on each iteration and i will be never less than or equal to 1. how come 5 will be the output of this program?







c






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 22 '18 at 4:45









MrAlphaMrAlpha

4619




4619













  • Possible duplicate of Undefined behavior and sequence points

    – Ken White
    Nov 22 '18 at 4:47











  • The proper declarations for main are int main (void) and int main (int argc, char **argv) (which you will see written with the equivalent char *argv). note: main is a function of type int and it returns a value. See: C11 Standard §5.1.2.2.1 Program startup p1 (draft n1570). See also: See What should main() return in C and C++?

    – David C. Rankin
    Nov 22 '18 at 4:51











  • @DavidC.Rankin What makes you think the OP is using a hosted system compiler? See What should main() return in C and C++?

    – Lundin
    Nov 22 '18 at 7:44











  • Why should it execute endlessly if you assume the condition will never be true? If your assumption was correct, it should not execute at all.

    – Gerhardh
    Nov 22 '18 at 8:24













  • @Lundin - I'm just a simple man, all I can do is read the standard unless the OP provides specifics. Perhaps we need to drop you same comment below each referencing What should main() return in C and C++??

    – David C. Rankin
    Nov 22 '18 at 18:29



















  • Possible duplicate of Undefined behavior and sequence points

    – Ken White
    Nov 22 '18 at 4:47











  • The proper declarations for main are int main (void) and int main (int argc, char **argv) (which you will see written with the equivalent char *argv). note: main is a function of type int and it returns a value. See: C11 Standard §5.1.2.2.1 Program startup p1 (draft n1570). See also: See What should main() return in C and C++?

    – David C. Rankin
    Nov 22 '18 at 4:51











  • @DavidC.Rankin What makes you think the OP is using a hosted system compiler? See What should main() return in C and C++?

    – Lundin
    Nov 22 '18 at 7:44











  • Why should it execute endlessly if you assume the condition will never be true? If your assumption was correct, it should not execute at all.

    – Gerhardh
    Nov 22 '18 at 8:24













  • @Lundin - I'm just a simple man, all I can do is read the standard unless the OP provides specifics. Perhaps we need to drop you same comment below each referencing What should main() return in C and C++??

    – David C. Rankin
    Nov 22 '18 at 18:29

















Possible duplicate of Undefined behavior and sequence points

– Ken White
Nov 22 '18 at 4:47





Possible duplicate of Undefined behavior and sequence points

– Ken White
Nov 22 '18 at 4:47













The proper declarations for main are int main (void) and int main (int argc, char **argv) (which you will see written with the equivalent char *argv). note: main is a function of type int and it returns a value. See: C11 Standard §5.1.2.2.1 Program startup p1 (draft n1570). See also: See What should main() return in C and C++?

– David C. Rankin
Nov 22 '18 at 4:51





The proper declarations for main are int main (void) and int main (int argc, char **argv) (which you will see written with the equivalent char *argv). note: main is a function of type int and it returns a value. See: C11 Standard §5.1.2.2.1 Program startup p1 (draft n1570). See also: See What should main() return in C and C++?

– David C. Rankin
Nov 22 '18 at 4:51













@DavidC.Rankin What makes you think the OP is using a hosted system compiler? See What should main() return in C and C++?

– Lundin
Nov 22 '18 at 7:44





@DavidC.Rankin What makes you think the OP is using a hosted system compiler? See What should main() return in C and C++?

– Lundin
Nov 22 '18 at 7:44













Why should it execute endlessly if you assume the condition will never be true? If your assumption was correct, it should not execute at all.

– Gerhardh
Nov 22 '18 at 8:24







Why should it execute endlessly if you assume the condition will never be true? If your assumption was correct, it should not execute at all.

– Gerhardh
Nov 22 '18 at 8:24















@Lundin - I'm just a simple man, all I can do is read the standard unless the OP provides specifics. Perhaps we need to drop you same comment below each referencing What should main() return in C and C++??

– David C. Rankin
Nov 22 '18 at 18:29





@Lundin - I'm just a simple man, all I can do is read the standard unless the OP provides specifics. Perhaps we need to drop you same comment below each referencing What should main() return in C and C++??

– David C. Rankin
Nov 22 '18 at 18:29












3 Answers
3






active

oldest

votes


















7














This is explained by the sequence of events:



i = 1; // for init
i++ <= 1 ? true // for condition, i evaluates as 1 but is made into i = 2 after the expression
i++; // inside for body, makes i = 3
i++; // for increment, makes i = 4
i++ <= 1 ? false // for condition again, i evaluates as 4 but is made into i = 5 after the expression
// condition is false, for loop ends, i = 5


Perhaps you are forgetting that the for condition, although false, is still executed to verify that before the program decides the loop is terminated.






share|improve this answer































    4














    i is incremented four times before the for loop exits.




    • Twice in the for condition check (formally cond_expression). Since you are using postfix increment, the first time i will be 1 when the condition is checked. It is only on the second check, i will be 2.

    • Once in the increment (formally iteration_expression)

    • Once more in the body (formally loop_statement) of the for loop.


    As the initial value of i is 1, this brings its value to 5.



    Note:

    - cond_expression is evaluated before the loop body. If the result of the expression is zero, the loop statement is exited immediately.

    - iteration_expression is evaluated after the loop body and its result is discarded. After evaluating iteration_expression, control is transferred to cond_expression.






    share|improve this answer





















    • 1





      @MrAlpha: I update the answer. Please see if this clarifies.

      – P.W
      Nov 22 '18 at 5:04



















    1















    As the i will be incremented on each iteration and i will be never less than or equal to 1. how come 5 will be the output of this program?




    How can i never be less than or equal to 1 when it's initialised with a value of 1?



    Did you intend to pre-increment i?



    That would be:



    for (i=1; ++1 <= 1; i++)


    But you seem to misunderstand the nature of the conditional expression here, with a for loop you are repeating a block of code until a condition is met, in this case do code while i++ <= 1.



    If you do



    for (i=1; ++i <= 1; i++)


    i will be > 1 on the first iteration and nothing will happen.






    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53424031%2fhow-the-answer-of-this-for-loop-is-5%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      7














      This is explained by the sequence of events:



      i = 1; // for init
      i++ <= 1 ? true // for condition, i evaluates as 1 but is made into i = 2 after the expression
      i++; // inside for body, makes i = 3
      i++; // for increment, makes i = 4
      i++ <= 1 ? false // for condition again, i evaluates as 4 but is made into i = 5 after the expression
      // condition is false, for loop ends, i = 5


      Perhaps you are forgetting that the for condition, although false, is still executed to verify that before the program decides the loop is terminated.






      share|improve this answer




























        7














        This is explained by the sequence of events:



        i = 1; // for init
        i++ <= 1 ? true // for condition, i evaluates as 1 but is made into i = 2 after the expression
        i++; // inside for body, makes i = 3
        i++; // for increment, makes i = 4
        i++ <= 1 ? false // for condition again, i evaluates as 4 but is made into i = 5 after the expression
        // condition is false, for loop ends, i = 5


        Perhaps you are forgetting that the for condition, although false, is still executed to verify that before the program decides the loop is terminated.






        share|improve this answer


























          7












          7








          7







          This is explained by the sequence of events:



          i = 1; // for init
          i++ <= 1 ? true // for condition, i evaluates as 1 but is made into i = 2 after the expression
          i++; // inside for body, makes i = 3
          i++; // for increment, makes i = 4
          i++ <= 1 ? false // for condition again, i evaluates as 4 but is made into i = 5 after the expression
          // condition is false, for loop ends, i = 5


          Perhaps you are forgetting that the for condition, although false, is still executed to verify that before the program decides the loop is terminated.






          share|improve this answer













          This is explained by the sequence of events:



          i = 1; // for init
          i++ <= 1 ? true // for condition, i evaluates as 1 but is made into i = 2 after the expression
          i++; // inside for body, makes i = 3
          i++; // for increment, makes i = 4
          i++ <= 1 ? false // for condition again, i evaluates as 4 but is made into i = 5 after the expression
          // condition is false, for loop ends, i = 5


          Perhaps you are forgetting that the for condition, although false, is still executed to verify that before the program decides the loop is terminated.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 22 '18 at 4:56









          HavenardHavenard

          17.9k22646




          17.9k22646

























              4














              i is incremented four times before the for loop exits.




              • Twice in the for condition check (formally cond_expression). Since you are using postfix increment, the first time i will be 1 when the condition is checked. It is only on the second check, i will be 2.

              • Once in the increment (formally iteration_expression)

              • Once more in the body (formally loop_statement) of the for loop.


              As the initial value of i is 1, this brings its value to 5.



              Note:

              - cond_expression is evaluated before the loop body. If the result of the expression is zero, the loop statement is exited immediately.

              - iteration_expression is evaluated after the loop body and its result is discarded. After evaluating iteration_expression, control is transferred to cond_expression.






              share|improve this answer





















              • 1





                @MrAlpha: I update the answer. Please see if this clarifies.

                – P.W
                Nov 22 '18 at 5:04
















              4














              i is incremented four times before the for loop exits.




              • Twice in the for condition check (formally cond_expression). Since you are using postfix increment, the first time i will be 1 when the condition is checked. It is only on the second check, i will be 2.

              • Once in the increment (formally iteration_expression)

              • Once more in the body (formally loop_statement) of the for loop.


              As the initial value of i is 1, this brings its value to 5.



              Note:

              - cond_expression is evaluated before the loop body. If the result of the expression is zero, the loop statement is exited immediately.

              - iteration_expression is evaluated after the loop body and its result is discarded. After evaluating iteration_expression, control is transferred to cond_expression.






              share|improve this answer





















              • 1





                @MrAlpha: I update the answer. Please see if this clarifies.

                – P.W
                Nov 22 '18 at 5:04














              4












              4








              4







              i is incremented four times before the for loop exits.




              • Twice in the for condition check (formally cond_expression). Since you are using postfix increment, the first time i will be 1 when the condition is checked. It is only on the second check, i will be 2.

              • Once in the increment (formally iteration_expression)

              • Once more in the body (formally loop_statement) of the for loop.


              As the initial value of i is 1, this brings its value to 5.



              Note:

              - cond_expression is evaluated before the loop body. If the result of the expression is zero, the loop statement is exited immediately.

              - iteration_expression is evaluated after the loop body and its result is discarded. After evaluating iteration_expression, control is transferred to cond_expression.






              share|improve this answer















              i is incremented four times before the for loop exits.




              • Twice in the for condition check (formally cond_expression). Since you are using postfix increment, the first time i will be 1 when the condition is checked. It is only on the second check, i will be 2.

              • Once in the increment (formally iteration_expression)

              • Once more in the body (formally loop_statement) of the for loop.


              As the initial value of i is 1, this brings its value to 5.



              Note:

              - cond_expression is evaluated before the loop body. If the result of the expression is zero, the loop statement is exited immediately.

              - iteration_expression is evaluated after the loop body and its result is discarded. After evaluating iteration_expression, control is transferred to cond_expression.







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Nov 22 '18 at 5:11

























              answered Nov 22 '18 at 4:49









              P.WP.W

              12.7k3944




              12.7k3944








              • 1





                @MrAlpha: I update the answer. Please see if this clarifies.

                – P.W
                Nov 22 '18 at 5:04














              • 1





                @MrAlpha: I update the answer. Please see if this clarifies.

                – P.W
                Nov 22 '18 at 5:04








              1




              1





              @MrAlpha: I update the answer. Please see if this clarifies.

              – P.W
              Nov 22 '18 at 5:04





              @MrAlpha: I update the answer. Please see if this clarifies.

              – P.W
              Nov 22 '18 at 5:04











              1















              As the i will be incremented on each iteration and i will be never less than or equal to 1. how come 5 will be the output of this program?




              How can i never be less than or equal to 1 when it's initialised with a value of 1?



              Did you intend to pre-increment i?



              That would be:



              for (i=1; ++1 <= 1; i++)


              But you seem to misunderstand the nature of the conditional expression here, with a for loop you are repeating a block of code until a condition is met, in this case do code while i++ <= 1.



              If you do



              for (i=1; ++i <= 1; i++)


              i will be > 1 on the first iteration and nothing will happen.






              share|improve this answer




























                1















                As the i will be incremented on each iteration and i will be never less than or equal to 1. how come 5 will be the output of this program?




                How can i never be less than or equal to 1 when it's initialised with a value of 1?



                Did you intend to pre-increment i?



                That would be:



                for (i=1; ++1 <= 1; i++)


                But you seem to misunderstand the nature of the conditional expression here, with a for loop you are repeating a block of code until a condition is met, in this case do code while i++ <= 1.



                If you do



                for (i=1; ++i <= 1; i++)


                i will be > 1 on the first iteration and nothing will happen.






                share|improve this answer


























                  1












                  1








                  1








                  As the i will be incremented on each iteration and i will be never less than or equal to 1. how come 5 will be the output of this program?




                  How can i never be less than or equal to 1 when it's initialised with a value of 1?



                  Did you intend to pre-increment i?



                  That would be:



                  for (i=1; ++1 <= 1; i++)


                  But you seem to misunderstand the nature of the conditional expression here, with a for loop you are repeating a block of code until a condition is met, in this case do code while i++ <= 1.



                  If you do



                  for (i=1; ++i <= 1; i++)


                  i will be > 1 on the first iteration and nothing will happen.






                  share|improve this answer














                  As the i will be incremented on each iteration and i will be never less than or equal to 1. how come 5 will be the output of this program?




                  How can i never be less than or equal to 1 when it's initialised with a value of 1?



                  Did you intend to pre-increment i?



                  That would be:



                  for (i=1; ++1 <= 1; i++)


                  But you seem to misunderstand the nature of the conditional expression here, with a for loop you are repeating a block of code until a condition is met, in this case do code while i++ <= 1.



                  If you do



                  for (i=1; ++i <= 1; i++)


                  i will be > 1 on the first iteration and nothing will happen.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 22 '18 at 4:58









                  NunchyNunchy

                  815411




                  815411






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53424031%2fhow-the-answer-of-this-for-loop-is-5%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Costa Masnaga

                      Fotorealismo

                      Sidney Franklin