Writing a Simple Operating System — from Scratch

(Jeff_L) #1

CHAPTER 5. WRITING, BUILDING, AND LOADING YOUR


KERNEL 61


// this function ’s interface.
int output = external_function (5, "Hello", 4.5);
}

float divide(float a, float b) {
return a / b;
}

This can be fixed as follows:

// These function prototypes inform the compiler about
// the function interfaces.
float divide(float a, float b); // <-- note the semi -colon
int external_function(int a, char* message , float b);

int add(int a, int b) {
return a + b;
}

void main() {
// This is okay , because our compiler has seen the full
// definition of add.
int result = add(5, 3);

// This is okay now: compiler knows the interface.
result = divide (34.3, 12.76);

// This is okay now: compiler knows the interface.
int output = external_function (5, "Hello", 4.5);
}

float divide(float a, float b) {
return a / b;
}

Now, since some functions will be called from code compiled into other object files,
they will also need to declare identical prototypes of those functions, which would lead to
a lot of duplicated prototype declarations, which is difficult to maintain. For this reason,
many C programs use the#includepre-processor directive to insert common code that
contains the required prototypes prior to compilation. This common code is known as a
header file, which we can think of as the interface to the compiled object file, and which
is used as follows.
Sometimes, one header file may include another, so it is important not to re-define
the same code...



  • Casting types

Free download pdf