Why declarations in <stdio.h> aren't implemented?

Click For Summary
SUMMARY

The discussion clarifies the behavior of header files in the C programming language, specifically regarding the stdio.h header. When a programmer includes stdio.h using #include, they gain access to standard input/output functions like printf() without needing to implement these functions themselves. This is because the linker searches through standard libraries for the definitions of these functions during the compilation process, unlike user-defined headers such as foo.h, which require explicit implementation in the corresponding source file.

PREREQUISITES
  • Understanding of C programming language syntax
  • Familiarity with header files and their purpose
  • Knowledge of the compilation and linking process in C
  • Basic understanding of standard libraries in C
NEXT STEPS
  • Explore the C compilation process, focusing on how linking works with libraries
  • Learn about the differences between user-defined headers and standard library headers
  • Investigate the role of the linker in C programming
  • Study the implementation of inline functions in C
USEFUL FOR

C programmers, software developers, and computer science students seeking to deepen their understanding of header files and the compilation process in C.

CGandC
Messages
326
Reaction score
34
In the C programming language any .h file is a file that contains constants and/or function declarations ( but no implementations, besides inline functions ). That means that when I write #include <foo.h> at the start of my .c file, I'll have to implement all those function declarations ( similar to implementing an interface in Java ).

But if I write for example, #include <stdio.h> at the beginning of the code, then I read that by doing this I am now able to call input/output related functions such as printf() ; I don't know how calling these functions is possible right now because I understood that after adding #include <stdio.h> to the beginning of the code then I have only added function declarations from stdio.h into my .c file and not yet implemented these declarations ( haven't defined them yet ) - this means that I have to implement those functions in my .c code ; however, a .c file compiles without implementing printf() for example.

So I haven't really understood, If #include <foo.h> just copies the contents of the declarations in foo.h file - function declarations which I have to implement in the .c file, then why when including #include <stdio.h> In my .c file don't have to implement functions declared in that header such as printf() ?

Thanks in advance for any help!
 
  • Like
Likes   Reactions: Wrichik Basu
Technology news on Phys.org
When you compile (and link) your code, there is a long list of libraries, some standard and some user-supplied, that the linker will look through for the function definitions. That is where it finds the definitions of those standard functions.
 
  • Informative
  • Like
Likes   Reactions: Wrichik Basu, Mark44, berkeman and 1 other person
I understand now, thanks!
 
  • Like
Likes   Reactions: berkeman

Similar threads

Replies
81
Views
7K
Replies
14
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
Replies
6
Views
2K
  • · Replies 6 ·
Replies
6
Views
12K
  • · Replies 4 ·
Replies
4
Views
1K
  • · Replies 0 ·
Replies
0
Views
2K
Replies
65
Views
5K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 4 ·
Replies
4
Views
1K