Fibonacci’s Number – In C

Recently, one of my friends brought the idea to me to write fibonacci’s number in C. Surprisingly, this became a confusing task at first. You must find a way to loop, which in this case, a while loop would do fine. But at the same time, you needed to get your variables set and to the point. So i decided to go with the code below (look for slight explanation below)

#include <stdio.h>
#include <unistd.h>

int main(void)
{
    // Define Integers
    int a = 1;
    int b = 0;
    int c = 0;
    int d = 0;
    int e = 0;

    // Start an infinite loop
    while (1)
    {
        // Start pattern
        c = a + b;
        d = b + c;
        e = c + d;
        a = d + e;
        b = e + a;
        // At this point, the pattern repeat

        // Print all the results
        sleep(1);
        printf("%i\n", c);
        sleep(1);
        printf("%i\n", d);
        sleep(1);
        printf("%i\n", e);
        sleep(1);
        printf("%i\n", a);
        sleep(1);
        printf("%i\n", b);

    }
    return 0;
}

So basically, I chose a rather long way to go about this problem. I set integers a, b, c, d, and e with a being the only value starting at 1 (the rest zero). Therefore, a + b = 1, b + c= 1 (c=1, b=0) and so on and so forth. From here on you can see, if you follow this pattern, eventually the variables will repeat after e + a to a + b. This being stated, where the loop begins to repeat, we are allowed to stop. (Also, if you haven’t noticed, the key to this pattern is to start off with a+b, then take b down and take the variable holding the calculation of the one before it)

Now our next problem, showing the results. At first, I over thought this problem. Eventually, I decided upon simply writing the printf’s in the order the variables are set, with a 1 second delay in-between them to prevent a system from crashing if someone wished to run this code.

Infinite Loops

So you’re learning C now and you keep getting told to watch out for infinite loops. You are wondering though, what exactly is an infinite loop, how do I watch for it? To answer that question is simple, an infinite loop is any loop, such as a for or which statement, that has a conditional statement that is always true. For example, the while statement can be set to always be true with a one (e.g. while(1) {…})

If your loop becomes infinite, the best way to fix it is to see why your test always returns true. Below you will find an example of a helpful infinite loop and a down right bad infinite loop that causes a system crash.

Good Example:
A good example, that takes very little time to write, would be counting. The below program creates an infinite loop and every second “counts” to the next number (starting from zero).

#include <stdio.h>
#include <unistd.h>

int main(void)
{
    // Define the variable
    int number = 0;

    // Create an infinite loop by forcing while to be true
    while(1)
    {
        // Wait for one second, like a clock
        sleep(1);
        // Print the integer defined in the variable "number"
        printf("Number %d\n", number);
        // Add one to variable "number"
        number++;
    }
}

Bad Example:
A bad example of an infinite loop would be a fork bomb. A fork bomb is basically any program that infinitely executes itself. This eats up system resources, and eventually kills, your computer. Only a reboot can save you with this type of code (usually)

#include <stdio.h>

int main(void)
{
    while(1)
    {
        system("a");
        printf("You're doomed!");
    }
}

Home for hacks, mods, code and science