moped
moped

Reputation: 21

How does my compiler know what printf() does?

Maybe this is a stupid question, but I'm new to this and trying to understand things.

Let's say I want to write a simple "Hello World" program:

// hello.c
int main() {
    printf("%s\n", "Hello World");
    return 0;
}

This doesn't compile because my compiler doesn't know what "printf" is (I'm using gcc on Ubuntu). So I #include <stdio.h> and now I can compile it and it works as expected. But why? stdio.h is only a header file containing the declaration of "printf". When I include it, gcc knows what "printf" is and how to call it, but how does it know what "printf" actually does?

I assume that the actual implementation of printf and the other functions declared in stdio.h is somewhere in some precompiled object file, let's call it stdio.o (even though I didn't find such a file in any directory). Why don't I have to include that object file for gcc to know the actual implementation of printf? I mean, why don't I have to do something like this:

gcc hello.c stdio.o -o hello

When I write an object file myself, I need to explicitly include the object file:

// main.c
#include "myobj.h"
// some code...

// in gcc: gcc main.c myobj.o -o main

Why isn't that the case with stdio.h?

Upvotes: 1

Views: 64

Answers (0)

Related Questions