1

When I write code such as

#include <stdio.h>

int main(int argc, char** argv) {
    printf("Hello, world!\n");
    return 0;
}

gcc imports stdio.h which in turn requires an associated stdio.c where the functions are defined. I know on linux stdio.h is located in /usr/include/stdio.h but where is stdio.c? There is no /usr/include/stdio.c but, obviously, gcc can compile this code just fine.

2

2 Answers 2

4

libc, like most libraries, is compiled ahead of time and shipped with your system as a compiled library rather than as source code. On Linux, that library will be called libc.a (for static linking) or libc.so (for dynamic linking), and can usually be found in /usr/lib or one of its subdirectories.

Also, note that in C there is actually no requirement that the header file name match that of the source file. For example, glibc, one of the most popular implementations of libc, scatters its stdio implementation across several files (approximately one C file for each function in stdio); these source files can be found here in the glibc git repository.

Sign up to request clarification or add additional context in comments.

Comments

0

When you include stdio.h, it provides declarations of functions like printf, but the actual implementations are precompiled in the C standard library (e.g., glibc) as binary files like libc.so, located in directories like /lib or /usr/lib. These implementations are written in source files (e.g., stdio.c) within the glibc source code, which is not installed by default but can be downloaded separately. During compilation, gcc links your code to this library, so there is no need for a visible stdio.c on your system.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.