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.
/** * You'll notice that we need to include a header file that * contains functions we need to use. Being a compiled language, * it's inefficient to include functions that aren't needed. * stdio.h contains functions for reading from and writing to the console */ #include <stdio.h> /** * In C, the program executes the main function. You should also take note * that we must declare a return type for the function. In this case, it's * an integer, and we return 0 to indicate successful completion of the * program. */ int main () { /* Notice that we need to declare our variables, and their type */ int n; /* printf prints a formated string to the stdout */ printf("\nHow many numbers of the sequence would you like?\n"); /* scanf reads a formated string from the stdin. We are expecting an integer here. */ scanf("%d",&n); /* Here we call the fibonacci function */ fibonacci(n); /* Finally, return 0 */ return 0; } /** * This is the simple fibonacci sequence generator. Notice also, we * declare the type of variable we expect to be passed to the function. */ int fibonacci(int n) { /** * Here we declare and set our variables. */ int a = 0; int b = 1; int sum; int i; /** * Here is the standard for loop. This will step through, performing the code * inside the braces until i is equal to n. */ for (i=0;i<n;i++) { printf("%d\n",a); sum = a + b; a = b; b = sum; } return 0; }
HTML code generated by vim-color-improved v.0.4.0.Download this code: fibonacci.c
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.
January 3rd, 2008 at 1:49 pm
[...] Networking « Fibonacci in C [...]
January 3rd, 2008 at 1:59 pm
[...] 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 [...]
January 3rd, 2008 at 3:47 pm
[...] 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 [...]
January 5th, 2008 at 6:34 pm
[...] 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 [...]
January 5th, 2008 at 7:01 pm
[...] Fibonacci in C [...]
February 15th, 2009 at 6:47 pm
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.
April 15th, 2009 at 9:28 am
here i gOt a prObLEm dUring cOmpilAtion, , , it give eRror in Prototype missing at this line “fibonacci(n);”
April 15th, 2009 at 10:05 am
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.
April 15th, 2009 at 10:08 am
@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.
July 15th, 2009 at 6:42 pm
How about scheme?? LISP?
Way shorter than any of those:
(define (fib n)
(if (< n 2)
n
(+ (fib (- n 1)) (fib (- n 2)))))
August 22nd, 2009 at 7:43 am
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.
September 28th, 2009 at 7:16 am
pls re post this cause there are some 8 errors when i tried this sample program…
October 26th, 2009 at 8:04 am
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.