Results 1 to 10 of 18

Thread: C programmers

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Aug 2010
    Posts
    5

    Arrow How to be a C programmer - part 3

    But by now you are burning to actually program. So let's do the traditional 'hello world' program.
    -----------------------------------------------------
    #include "stdio.h"

    void main( ){

    printf("Hello World!\n");

    }

    ---------------------------------------------------------

    That may look all a bit puzzling to a beginner. So let's begin with the beginning. I said that functions were subsystems but the entire program itself is also considered to be a function, it's the main function. It has to be called 'main' so that the compiler would know where the program begins. Since functions are (sub)systems it should not be surprising they have the following structure.

    <type of output> <name of function> ( <type of input> ) {

    <actual code here>

    }

    In our case the type of output was void. Which means 'no specific type of output'. The name is main and in between the brackets there is the input; nothing in this case. The function itself goes from the opening curly bracket till the closing one.
    In between the curly brackets I put printf. That means 'display a bit of text in the terminal'. Printf is itself a function. It was written by someone else, the body of the function is not visible here. It's being called, that means we want to make use of it here. In between the round brackets we put the input. Which is "Hello World!\n". The double quotes is used when dealing with text as opposed to numbers. The '\' means that the next character is not to be taken literally. The 'n' after that means take a new line.
    The body of printf is not in the program but the compiler needs to be able to find it. That's why we write #include. The include tells the compiler we might be using the functions in our program that are described in "stdio.h", like printf.

  2. #2
    Join Date
    Aug 2010
    Posts
    5

    Arrow How to be a C programmer - part 4

    Ok, that's for the very, very basics. Let's do something a bit more complicated. The following program does compute the first 10 Fibonacci numbers. I must admit I didn't compile this so minor errors are a possibility. ( I writing from a cyber cafe so compiling here is not practical.)

    ------------------------------------------------------------------------------------------

    #include "stdio.h"

    // text after these two forward slashes means comment
    // the compiler ignores everything on the line

    /*
    everything inside those symbols
    means comment too
    */

    int fibonacci (int n);

    void main( ){

    int i = 1;
    int j = 0;

    while( i < 11){
    j = fibonacci(i);
    printf("Fibonacci number %d is %d\n", i, j);
    i = i + 1;
    }

    }

    // program to be continued in part 5

  3. #3
    Join Date
    Aug 2010
    Posts
    5

    Arrow How to be a C programmer - part 5

    int fibonacci(int n){

    if ( n <= 0){
    return -1;
    //-1 means ERROR
    }
    if(n == 1 || n == 2){
    return 1;
    }

    int prev = 1;
    int pprev = 1;
    int tmp = 1;
    n = n - 2;

    while(n){
    tmp = pprev;
    pprev = prev;
    prev = prev + tmp;
    n = n - 1;
    }
    return prev;

    }

    ----------------------------------------------------------------------------

    That's a whole lot of stuff so let's comment a small bit. At the very top I wrote:

    int fibonacci( int n);

    That's called the prototype. That's to make the compiler aware that this function will be used during the program. The compiler reads the file from top to bottom. If it finds the function first in the main function it will raise an error and say ' I don't know about that function' or something to that effect.

    int i = 0;

    That means reserve a bit of memory and call it ' i ' and set it to zero. The first word is the type 'int' as in 'integer' means we are going to use ' i ' for whole numbers only.

    Ok so that was a fair bit for today. I will write more tomorrow. Just in case you didn't know the fibonacci sequence is a series of numbers where the next number is the sum of the two previous. It goes:
    1 1 2 3 5 8 etc.

    Cheers
    Gregg

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •