Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When including a header file in C, does it automatically include the .c file of the same name?

Tags:

c

header

I've looked around online as well as in my textbook and this is confusing me.

Say you have some functions for stacks in stack.c, and you put their prototypes in stack.h. Your main program, say, test.c has #include "stack.h" at the top. This is how all the examples show.

So it includes the prototypes, but how does it get their implementations? The header files don't seem to require that you #include stack.c with them. Does it just search all the .c files in the same folder and try to find them?

like image 802
Doug Smith Avatar asked Apr 09 '12 01:04

Doug Smith


People also ask

Can you include a .C file in header?

The answer to the above is yes. header files are simply files in which you can declare your own functions that you can use in your main program or these can be used while writing large C programs. NOTE:Header files generally contain definitions of data types, function prototypes and C preprocessor commands.

What happens when you include a header file in C?

It attempts to find a function definition (implementation), which exactly matches the header you declared earlier. What happens if you #include header file is that compiler (specifically, the preprocessor) copies the whole contents of header file into place, where you put your #include .

Are header files automatically included?

Rationale. In the C and C++ programming languages, a header file is a file whose text may be automatically included in another source file by the C preprocessor by the use of a preprocessor directive in the source file.

Does a C file need to include its own header?

You don't have to, but you can. The header file contains the declarations. You're free to invoke anytime as long as the prototype matches. And as long as the compiler finds an implementation before finishing compilation.


1 Answers

No; it includes just the header.

You compile the source separately, and link that with your code that uses it.

For example (toy code):

stack.h

extern int pop(void);
extern void push(int value);

stack.c

#include "stack.h"
#include <stdio.h>
#include <stdlib.h>

enum { MAX_STACK = 20 };
static int stack[MAX_STACK];
static int stkptr = 0;

static void err_exit(const char *str)
{
     fprintf(stderr, "%s\n", str);
     exit(1);
}

int pop(void)
{
    if (stkptr > 0)
        return stack[--stkptr];
    else
        err_exit("Pop on empty stack");
}

int push(int value)
{
    if (stkptr < MAX_STACK)
        stack[stkptr++] = value;
    else
        err_exit("Stack overflow");
}

test.c

#include <stdio.h>
#include "stack.h"

int main(void)
{
    for (int i = 0; i < 10; i++)
        push(i * 10);

    for (int i = 0; i < 10; i++)
        printf("Popped %d\n", pop());
    return(0);
}

Compilation

c99 -c stack.c
c99 -c test.c
c99 -o test_stack test.o stack.o

Or:

c99 -o test_stack test.c stack.c

So, you compile the source files (optionally producing object files) and link them. Often, the stack.o file would be placed into a library (other than the standard C library) and you'd link with that library. This is what happens with the standard C library functions as well, of course. The C compiler automatically adds the C library (usually -lc) to the linking command.

like image 146
Jonathan Leffler Avatar answered Oct 12 '22 12:10

Jonathan Leffler