Iterative Fibonacci sequence












3














I implemented a function that prints all the numbers from the Fibonacci sequence until max_num. The minimum value allowed is 0 (so, fib(0) prints 1). It works until 92, and I want to know how to improve the code, in general.



void fib(unsigned int max_num)
{
unsigned long fib_num = 1;
unsigned long fib_temp = 0;
size_t count = 0;

if (max_num < 0)
{
fprintf(stderr, "Please, enter a non-negative numbern");
return;
}

for (; count <= max_num; count++)
{
printf("%lun", fib_num);
fib_num += fib_temp;
fib_temp = fib_num - fib_temp;
}
}









share|improve this question



























    3














    I implemented a function that prints all the numbers from the Fibonacci sequence until max_num. The minimum value allowed is 0 (so, fib(0) prints 1). It works until 92, and I want to know how to improve the code, in general.



    void fib(unsigned int max_num)
    {
    unsigned long fib_num = 1;
    unsigned long fib_temp = 0;
    size_t count = 0;

    if (max_num < 0)
    {
    fprintf(stderr, "Please, enter a non-negative numbern");
    return;
    }

    for (; count <= max_num; count++)
    {
    printf("%lun", fib_num);
    fib_num += fib_temp;
    fib_temp = fib_num - fib_temp;
    }
    }









    share|improve this question

























      3












      3








      3







      I implemented a function that prints all the numbers from the Fibonacci sequence until max_num. The minimum value allowed is 0 (so, fib(0) prints 1). It works until 92, and I want to know how to improve the code, in general.



      void fib(unsigned int max_num)
      {
      unsigned long fib_num = 1;
      unsigned long fib_temp = 0;
      size_t count = 0;

      if (max_num < 0)
      {
      fprintf(stderr, "Please, enter a non-negative numbern");
      return;
      }

      for (; count <= max_num; count++)
      {
      printf("%lun", fib_num);
      fib_num += fib_temp;
      fib_temp = fib_num - fib_temp;
      }
      }









      share|improve this question













      I implemented a function that prints all the numbers from the Fibonacci sequence until max_num. The minimum value allowed is 0 (so, fib(0) prints 1). It works until 92, and I want to know how to improve the code, in general.



      void fib(unsigned int max_num)
      {
      unsigned long fib_num = 1;
      unsigned long fib_temp = 0;
      size_t count = 0;

      if (max_num < 0)
      {
      fprintf(stderr, "Please, enter a non-negative numbern");
      return;
      }

      for (; count <= max_num; count++)
      {
      printf("%lun", fib_num);
      fib_num += fib_temp;
      fib_temp = fib_num - fib_temp;
      }
      }






      beginner c fibonacci-sequence






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Aug 22 '16 at 21:40









      Lúcio Cardoso

      447314




      447314






















          1 Answer
          1






          active

          oldest

          votes


















          6














          Consider documenting this function with something like doxygen. At a quick glance, your function can be interpreted two different ways: Print out fib(0), fib(1), ..., or print out all Fibonacci numbers less than or equal to max_num. Documentation will clarify that.



          max_num will never be negative. It is defined as an unsigned int, which means it can never hold a negative value. So this:



          if (max_num < 0)
          {
          fprintf(stderr, "Please, enter a non-negative numbern");
          return;
          }


          will never happen. (Try calling fib(-2), see what happens)



          (Optional) Put size_t count = 0 inside the loop if you can? (You may have to add -std=c99 or something like that to make it work)



          for (size_t count = 0; count <= max_num; count++)
          {
          printf("%lun", fib_num);
          fib_num += fib_temp;
          fib_temp = fib_num - fib_temp;
          }





          share|improve this answer























          • Thanks a lot for the answer! I heard some people saying I should stick to the c89 mode. In your opinion, which one is the best?
            – Lúcio Cardoso
            Aug 23 '16 at 0:33






          • 1




            It has a lot to do with how much of the market supports the language... It may have been at the time a better idea to use C89 over C99 because very few compilers supported C99. This isn't true anymore. It might even be better to use C11. (Which I believe is -std=c11). A large part is do you need to support C89? C99? If not, I would just do C99, the for-loop syntax for C89 is particularly annoying to me.
            – Dair
            Aug 23 '16 at 0:50












          • For reference there are answers here: stackoverflow.com/a/11926008/667648 That coincide with my comment.
            – Dair
            Aug 23 '16 at 0:53










          • @LúcioCardoso The only more or less relevant compiler not supporting C99 is VS C compiler afaik. But C is mostly broken under Windows, similar to OpenGL. It works, with a few quirks, but it's not for serious development.
            – larkey
            Aug 24 '16 at 10:23











          Your Answer





          StackExchange.ifUsing("editor", function () {
          return StackExchange.using("mathjaxEditing", function () {
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          });
          });
          }, "mathjax-editing");

          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: "196"
          };
          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: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          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%2fcodereview.stackexchange.com%2fquestions%2f139400%2fiterative-fibonacci-sequence%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          6














          Consider documenting this function with something like doxygen. At a quick glance, your function can be interpreted two different ways: Print out fib(0), fib(1), ..., or print out all Fibonacci numbers less than or equal to max_num. Documentation will clarify that.



          max_num will never be negative. It is defined as an unsigned int, which means it can never hold a negative value. So this:



          if (max_num < 0)
          {
          fprintf(stderr, "Please, enter a non-negative numbern");
          return;
          }


          will never happen. (Try calling fib(-2), see what happens)



          (Optional) Put size_t count = 0 inside the loop if you can? (You may have to add -std=c99 or something like that to make it work)



          for (size_t count = 0; count <= max_num; count++)
          {
          printf("%lun", fib_num);
          fib_num += fib_temp;
          fib_temp = fib_num - fib_temp;
          }





          share|improve this answer























          • Thanks a lot for the answer! I heard some people saying I should stick to the c89 mode. In your opinion, which one is the best?
            – Lúcio Cardoso
            Aug 23 '16 at 0:33






          • 1




            It has a lot to do with how much of the market supports the language... It may have been at the time a better idea to use C89 over C99 because very few compilers supported C99. This isn't true anymore. It might even be better to use C11. (Which I believe is -std=c11). A large part is do you need to support C89? C99? If not, I would just do C99, the for-loop syntax for C89 is particularly annoying to me.
            – Dair
            Aug 23 '16 at 0:50












          • For reference there are answers here: stackoverflow.com/a/11926008/667648 That coincide with my comment.
            – Dair
            Aug 23 '16 at 0:53










          • @LúcioCardoso The only more or less relevant compiler not supporting C99 is VS C compiler afaik. But C is mostly broken under Windows, similar to OpenGL. It works, with a few quirks, but it's not for serious development.
            – larkey
            Aug 24 '16 at 10:23
















          6














          Consider documenting this function with something like doxygen. At a quick glance, your function can be interpreted two different ways: Print out fib(0), fib(1), ..., or print out all Fibonacci numbers less than or equal to max_num. Documentation will clarify that.



          max_num will never be negative. It is defined as an unsigned int, which means it can never hold a negative value. So this:



          if (max_num < 0)
          {
          fprintf(stderr, "Please, enter a non-negative numbern");
          return;
          }


          will never happen. (Try calling fib(-2), see what happens)



          (Optional) Put size_t count = 0 inside the loop if you can? (You may have to add -std=c99 or something like that to make it work)



          for (size_t count = 0; count <= max_num; count++)
          {
          printf("%lun", fib_num);
          fib_num += fib_temp;
          fib_temp = fib_num - fib_temp;
          }





          share|improve this answer























          • Thanks a lot for the answer! I heard some people saying I should stick to the c89 mode. In your opinion, which one is the best?
            – Lúcio Cardoso
            Aug 23 '16 at 0:33






          • 1




            It has a lot to do with how much of the market supports the language... It may have been at the time a better idea to use C89 over C99 because very few compilers supported C99. This isn't true anymore. It might even be better to use C11. (Which I believe is -std=c11). A large part is do you need to support C89? C99? If not, I would just do C99, the for-loop syntax for C89 is particularly annoying to me.
            – Dair
            Aug 23 '16 at 0:50












          • For reference there are answers here: stackoverflow.com/a/11926008/667648 That coincide with my comment.
            – Dair
            Aug 23 '16 at 0:53










          • @LúcioCardoso The only more or less relevant compiler not supporting C99 is VS C compiler afaik. But C is mostly broken under Windows, similar to OpenGL. It works, with a few quirks, but it's not for serious development.
            – larkey
            Aug 24 '16 at 10:23














          6












          6








          6






          Consider documenting this function with something like doxygen. At a quick glance, your function can be interpreted two different ways: Print out fib(0), fib(1), ..., or print out all Fibonacci numbers less than or equal to max_num. Documentation will clarify that.



          max_num will never be negative. It is defined as an unsigned int, which means it can never hold a negative value. So this:



          if (max_num < 0)
          {
          fprintf(stderr, "Please, enter a non-negative numbern");
          return;
          }


          will never happen. (Try calling fib(-2), see what happens)



          (Optional) Put size_t count = 0 inside the loop if you can? (You may have to add -std=c99 or something like that to make it work)



          for (size_t count = 0; count <= max_num; count++)
          {
          printf("%lun", fib_num);
          fib_num += fib_temp;
          fib_temp = fib_num - fib_temp;
          }





          share|improve this answer














          Consider documenting this function with something like doxygen. At a quick glance, your function can be interpreted two different ways: Print out fib(0), fib(1), ..., or print out all Fibonacci numbers less than or equal to max_num. Documentation will clarify that.



          max_num will never be negative. It is defined as an unsigned int, which means it can never hold a negative value. So this:



          if (max_num < 0)
          {
          fprintf(stderr, "Please, enter a non-negative numbern");
          return;
          }


          will never happen. (Try calling fib(-2), see what happens)



          (Optional) Put size_t count = 0 inside the loop if you can? (You may have to add -std=c99 or something like that to make it work)



          for (size_t count = 0; count <= max_num; count++)
          {
          printf("%lun", fib_num);
          fib_num += fib_temp;
          fib_temp = fib_num - fib_temp;
          }






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Dec 16 at 13:23









          albert

          1372




          1372










          answered Aug 22 '16 at 23:18









          Dair

          4,442729




          4,442729












          • Thanks a lot for the answer! I heard some people saying I should stick to the c89 mode. In your opinion, which one is the best?
            – Lúcio Cardoso
            Aug 23 '16 at 0:33






          • 1




            It has a lot to do with how much of the market supports the language... It may have been at the time a better idea to use C89 over C99 because very few compilers supported C99. This isn't true anymore. It might even be better to use C11. (Which I believe is -std=c11). A large part is do you need to support C89? C99? If not, I would just do C99, the for-loop syntax for C89 is particularly annoying to me.
            – Dair
            Aug 23 '16 at 0:50












          • For reference there are answers here: stackoverflow.com/a/11926008/667648 That coincide with my comment.
            – Dair
            Aug 23 '16 at 0:53










          • @LúcioCardoso The only more or less relevant compiler not supporting C99 is VS C compiler afaik. But C is mostly broken under Windows, similar to OpenGL. It works, with a few quirks, but it's not for serious development.
            – larkey
            Aug 24 '16 at 10:23


















          • Thanks a lot for the answer! I heard some people saying I should stick to the c89 mode. In your opinion, which one is the best?
            – Lúcio Cardoso
            Aug 23 '16 at 0:33






          • 1




            It has a lot to do with how much of the market supports the language... It may have been at the time a better idea to use C89 over C99 because very few compilers supported C99. This isn't true anymore. It might even be better to use C11. (Which I believe is -std=c11). A large part is do you need to support C89? C99? If not, I would just do C99, the for-loop syntax for C89 is particularly annoying to me.
            – Dair
            Aug 23 '16 at 0:50












          • For reference there are answers here: stackoverflow.com/a/11926008/667648 That coincide with my comment.
            – Dair
            Aug 23 '16 at 0:53










          • @LúcioCardoso The only more or less relevant compiler not supporting C99 is VS C compiler afaik. But C is mostly broken under Windows, similar to OpenGL. It works, with a few quirks, but it's not for serious development.
            – larkey
            Aug 24 '16 at 10:23
















          Thanks a lot for the answer! I heard some people saying I should stick to the c89 mode. In your opinion, which one is the best?
          – Lúcio Cardoso
          Aug 23 '16 at 0:33




          Thanks a lot for the answer! I heard some people saying I should stick to the c89 mode. In your opinion, which one is the best?
          – Lúcio Cardoso
          Aug 23 '16 at 0:33




          1




          1




          It has a lot to do with how much of the market supports the language... It may have been at the time a better idea to use C89 over C99 because very few compilers supported C99. This isn't true anymore. It might even be better to use C11. (Which I believe is -std=c11). A large part is do you need to support C89? C99? If not, I would just do C99, the for-loop syntax for C89 is particularly annoying to me.
          – Dair
          Aug 23 '16 at 0:50






          It has a lot to do with how much of the market supports the language... It may have been at the time a better idea to use C89 over C99 because very few compilers supported C99. This isn't true anymore. It might even be better to use C11. (Which I believe is -std=c11). A large part is do you need to support C89? C99? If not, I would just do C99, the for-loop syntax for C89 is particularly annoying to me.
          – Dair
          Aug 23 '16 at 0:50














          For reference there are answers here: stackoverflow.com/a/11926008/667648 That coincide with my comment.
          – Dair
          Aug 23 '16 at 0:53




          For reference there are answers here: stackoverflow.com/a/11926008/667648 That coincide with my comment.
          – Dair
          Aug 23 '16 at 0:53












          @LúcioCardoso The only more or less relevant compiler not supporting C99 is VS C compiler afaik. But C is mostly broken under Windows, similar to OpenGL. It works, with a few quirks, but it's not for serious development.
          – larkey
          Aug 24 '16 at 10:23




          @LúcioCardoso The only more or less relevant compiler not supporting C99 is VS C compiler afaik. But C is mostly broken under Windows, similar to OpenGL. It works, with a few quirks, but it's not for serious development.
          – larkey
          Aug 24 '16 at 10:23


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Code Review Stack Exchange!


          • 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.


          Use MathJax to format equations. MathJax reference.


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





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • 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%2fcodereview.stackexchange.com%2fquestions%2f139400%2fiterative-fibonacci-sequence%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

          Сан-Квентин

          8-я гвардейская общевойсковая армия

          Алькесар