Hi

Say I want to write a header file as an exercise. It will contain a bunch of functions and some constants.

Q1. I noticed that the standard header files only contain the function prototypes.
a> Where are the actual functions?
b> How do I write a header file such that the actual functions are separately stored?
c> How are these two files connected?
d> What is the advantage of this?

Q2. How do I include this header file in my code? Just the normal #include<>?

Thanks.

Recommended Answers

All 5 Replies

a> Where are the actual functions?

Linked into the compiler as either static libraries or dynamic libraries.

b> How do I write a header file such that the actual functions are separately stored?

The header file should contain only declarations. You'll provide definitions in a separate .c file that is subsequently compiled and linked with calling code:

/* header.h */
#ifndef HEADER_H
#define HEADER_H

void foo(); /* Declaration only */

#endif
/* header.c */
#include <stdio.h>
#include "header.h"

void foo()
{
    puts("FOO!!!!");
}
/* main.c */
#include "header.h"

int main(void)
{
    foo();

    return 0;
}
$ gcc header.c main.c

c> How are these two files connected?

The #include directive takes the contents of the file and pastes it over itself. The main.c file above would look like this after such a paste:

/* main.c */
/* header.h */
#ifndef HEADER_H
#define HEADER_H

void foo(); /* Declaration only */

#endif

int main(void)
{
    foo();

    return 0;
}

So at link time the header files no longer exist. Provided the declarations are available, your code will compile. Then the linker puts the definitions and calling code together.

d> What is the advantage of this?

You don't have to put everything in one epically huge file.

Q2. How do I include this header file in my code? Just the normal #include<>?

Convention is to use double quotes instead of angle brackets for headers you write.

What Narue said is pretty much on the money, but the way the answers are given may be a bit confusing. Allow me to re-post an old piece I wrote on DevShed many moons ago:

http://forums.devshed.com/showpost.php?p=2001351&postcount=14

Thanks guys. I'll see what I can learn from all this.

New questions!
a> What is static library? What is dynamic library?
b> What is the difference between c code, object code and exe file??
c> What is this linker? How does it work?
d> What does #ifndef HEADER_H statement mean? More specifically, what is HEADER_H?

a> What is static library? What is dynamic library?

What is google?

b> What is the difference between c code, object code and exe file??

C code is human readable source code. Object code is C code compiled into machine readable instructions. EXE files are directly executable object code.

c> What is this linker? How does it work?

What is google?

d> What does #ifndef HEADER_H statement mean? More specifically, what is HEADER_H?

It's an inclusion guard.

Thanks again.

Yes, I'll google them.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.