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

5 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 [...]

Leave a Reply