Monday, September 26, 2011

Coding Lessons: C and included header files (lesson 8)

In the last lesson I introduced the idea of putting either complicated or repetitive lines of code into a function of their own in order to reduce the amount of typing that you may need to do.

Now, I'm going to introduce a radical new concept, what if as well as using a function multiple times in that program, you also want to use that same function in multiple programs, are you going to copy and paste your function hundreds of times into your new code?

In the same way as you don't re-write all the code to actually do the getting of characters onto the screen, (you just use the printf function, you don't write out all the code each time) you also don't want to have to re-write (or copy paste) the code for your repetitive functions.

You might be wondering, since I've just said that printf is a function, where is this function? where is it declared? where is it called?

Well, you'll notice that the first line of every program that we've written so far has #include, (that means include this file), then the name of a file, the fact that the file name is in little triangular brackets means that it's in the tcc includes folder.)

well, those functions, Printf, scanf (and a whole load more) are declared in that stdio.h file.

you see that exporting those functions to a header file is useful, what's contained in the header file is just code, (exactly like we write), but it's used again and again, (in every program we've written so far).

so lets have a look at including our own header files.

so we start the code in the usual fashion

(include the standard header files from the includes directory of the Tcc compiler)
#include <stdio.h>

now we'll include our own header file, but this time from the same source directory.

#include "bmi.h"

see how it's included in quote marks, and not brackets. that means from this path, rather than from your library.

of course, copying header files might not be your idea of a great time so you could put your header into the main includes folder in the tcc directory, or you might want to start a header library of your own and include like this:
#include "../../mylibrary/bmi.h"


After this we write the main part of the program exactly as before.
int main()
{
    int weight, height;
    float bmi;
    printf("BMI Calculator\r\n");
    printf("Enter your weight in Kilos:");
    scanf("%d", &weight);
    printf("Please enter your height in centimeters:");
    scanf("%d", &height);
    bmi = bodymassindex(height, weight);
    printf("your BMI is: %f\r\n", bmi);
    printf("\r\n\r\nUnderweight = <18 -="-" .5=".5" 24.9="24.9" 29.9="29.9" 30="30" br="br" greater="greater" n="n" nnormal="nnormal" nobesity="BMI" noverweight="25" of="of" or="or" r="r" weight="18.5">
}


inside our source directory we also need to have the header file, (called bmi.h)

this header file is literally, a line to declare a function exists, and the function, exactly the same as the last lesson:
float bodymassindex(h, w);
float bodymassindex(int h, int w)
{
    float result;
    /*bmi = mass(kg) / height^2(m)*/
    result = h * h;
    result = result/10000;
    result = w/result;
    return(result);
}




in completion the source code looks like this:

File 1, source.c
#include <stdio.h>
#include "bmi.h"
int main()
{
    int weight, height;
    float bmi;
    printf("BMI Calculator\r\n");
    printf("Enter your weight in Kilos:");
    scanf("%d", &weight);
    printf("Please enter your height in centimeters:");
    scanf("%d", &height);
    bmi = bodymassindex(height, weight);
    printf("your BMI is: %f\r\n", bmi);
    printf("\r\n\r\nUnderweight = < 18 -="-" .5=".5" 24.9="24.9" 29.9="29.9" 30="30" br="br" greater="greater" n="n" nnormal="nnormal" nobesity="BMI" noverweight="25" of="of" or="or" r="r" weight="18.5">
}

File 2, bmi.h
float bodymassindex(h, w);
float bodymassindex(int h, int w)
{
    float result;
    /*bmi = mass(kg) / height^2(m)*/
    result = h * h;
    result = result/10000;
    result = w/result;
    return(result);
}

Complied and run, this program looks exactly the same as the program fro mthe last two examples.

No comments: