Fibonacci in C

Welcome to the first post of the Fibonacci project. In the following weeks, I am going to write this simple program in as many languages as possible, to highlight the similarities and differences between them. Today I will start with C, a classic language that is still in use today, and a good foundation for everything else that we will see. Rather than writing a lengthy post with each language, I’ll try to comment the code to point out the important things. Hopefully, this learning experiment will teach me something, and hopefully it will provide a handy resource for people to compare languages, even if it’s in a limited capacity.

So before I started, I gave myself the following “spec sheet” for writing the code:

The purpose of the Fibonacci project is to create a simple programming example in as many languages as possible, to provide a clear demonstration of the differences and similarities between them.

The sample code should:

  • Accept an input “x” (A value to progress up to)
  • Starting at 0 and 1, output the Fibonacci sequence up to “x” permutations.
  • There should be two functions, for clarity.
    • (main) Accepts the input and calls the fibonacci function.
    • (fibonacci) One that accepts the value of x and outputs the sequence.

The code should accept input from the console (stdin), with a prompt, and output to the console (stdout). This is so we keep everything simple. All of the code should be thoroughly commented.

So there’s my version of the Fibonacci sequence generator in C. I’m sure there are some problems with it, but the above code works. What would you change to make it better? Stay tuned for the next installment … PHP.

22 Responses to “Fibonacci in C”

  1. Fibonacci in PHP Says:

    […] Networking « Fibonacci in C […]

  2. Fibonacci in BASH Says:

    […] complicated problems, so running our Fibonacci program is no sweat. If you haven’t seen the Fibonacci in C and the Fibonacci in PHP, now might be a good time to review […]

  3. Fibonacci in Perl Says:

    […] For those of you who don’t know about the Fibonacci project, you should read the first post, Fibonacci in C. Also, you may want to check out the other posts in the series. The comments in the code will make […]

  4. Fibonacci in Python Says:

    […] program in Python. For those of you who are unfamiliar with what we are doing here, please read the first post here. Python is a newer, but increasingly popular scripting language. I suppose the most interesting […]

  5. Fibonacci in Ruby Says:

    […] Fibonacci in C […]

  6. Mike Says:

    Hey! Was just wondering, I borrowed the code 😉 and well I was just wondering if there maybe a way to set the fib seq to print out at a particular point in which the user declares the start point after prompted to.

  7. mUshu Says:

    here i gOt a prObLEm dUring cOmpilAtion, , , it give eRror in Prototype missing at this line “fibonacci(n);”

  8. Zachary Fox Says:

    I can’t seem to duplicate that problem, works fine for me on both linux and OS X with gcc. Did you copy and paste the code or type it all out, or download the file using the link? I’d try downloading the file first.

    Also, if you’re using another compiler, there may be something different about it that my code doesn’t take into account.

  9. Zachary Fox Says:

    @Mike – Sorry I never responded to you. I’m sure there is, just add another prompt to get another variable from stdin, and then modify the fibonacci function to accept n and that. biggest problem is you don’t know what b is, so if you’re using this example, you’ll still have to calculate everything up to that point.

  10. Adrian H Says:

    How about scheme?? LISP?
    Way shorter than any of those:

    (define (fib n)
    (if (< n 2)
    n
    (+ (fib (- n 1)) (fib (- n 2)))))

  11. Sandy Says:

    Un-related to above, but wanted to make contact. I’ve downloaded your add on for FF for NoDoFollow; but I can’t figure out how to use it. The example shows content is highlighted red. Somewhere, I believe on a blog or two I read using the toolbar was suppose to make things turn blue or red, one meant do follow, the other no follow. But, mine does not turn red or blue, it turns purple and I don’t know if purple is good or bad…good being do follow, bad being no follow.

    I’ve asked this question of multiple people, but so far haven’t been able to get an answer. Am hoping you can set me straight, perhaps hop over to my blog and and let me know.

    Thanks, looking forward to using the tool. I removed the no follow from all 3 of my blogspot blogs, I have the badge showing others I am a do follow; but now would like to go to the next step and no when I’m visiting a blog whether they are a do follow or a no follow.

    Thanks
    Sandy

    I should also add, adding to my confusion somewhere I think I read it was suppose to turn purple or white, but those colors weren’t identified as to which one meant what. Using the tool here now on your blog, your name shows up red and the date of your comment shows up purple. Adrian’s responses also show up red, while Mike’s remain colorless.

  12. MAlevolence_of_C Says:

    pls re post this cause there are some 8 errors when i tried this sample program…

  13. Zachary Fox Says:

    Not really sure why, it works fine for me. There isn’t much that can go wrong. Maybe if you could past the errors, we could figure it out.

  14. Motiur Says:

    Is a recursive implementation of Fibonacci sequence in C possible.

  15. Aron N Says:

    People with errors, either move the function fibonacci above the main-function, or declare a prototype above and keep the definition below. When the compiler comes to fibonacci(n); it has not yet seen the declaration of that function.

    A prototype would be written as
    int fibonacci(int n);

  16. Ahmad Hazim Says:

    Hello, Thanks for this simple program… It is truly helpful and very effective. But there is something missing… This code will not run properly because the main() function is not able to access the fibonacci() function because the C++ compiler works sequentially and so the main() function is declared before the fibonacci(). The missing point is that you should add a forward declaration for fibonacci() as following:

    /** forward declaration for fibonacci() */
    int fibonacci();

    /** main() */
    int main() {
    // the main() function code
    }

    /** fibonacci() code */
    int fibonacci() {
    // the fibonacci() function code definition goes here
    }

    Best regards

  17. Manu Says:

    I compiled one in C which generates upto 10 digits.

    #include

    int main(void)
    {
    int i = 1, sum = 0;

    printf(“%d\n%d\n”, 1,1);
    while (i <= 999999999) {
    sum = i – sum;
    i = i + sum;
    printf("%d\n", i);
    }
    return 0;
    }

  18. Amira Says:

    what about this :) ?

    fibo(int n)
    {
    if (n==1)
    return 1;

    if (n==0)
    return 1;

    if (n>1)
    return fibo(n-1)+fib(n-2);
    }

  19. Imre Says:

    Well for starters i wouldn’t

    return 0;

  20. Saranya Says:

    hai,
    Just amazed on seeing the way explained fibonacci program with explanation.
    I request you to do the same for programs like string manipulation string reverse string trim……….and other programs..

  21. Code for Fibonacci | Swageroo Algorithms Says:

    […] Another article with a more involved code-explanation can be found here. […]

  22. Shailesh Prajapati Says:

    /*Fibonacci in C */

    #include
    #include

    void main()
    {
    int i=1,n;
    unsigned int a=0,b=1,c;
    clrscr();

    printf(“Enter a Value : “);
    scanf(“$d”,&n);

    while(i<=n-2);
    {
    c=a+b;
    printf("%u \t",c);

    a=b;
    b=c;
    i++;
    }
    getch();
    }