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

In summary, the C programming language allows for the use of .h files, which contain function declarations but no implementations. When including a .h file in a .c file, the function declarations must be implemented in the code. However, for standard functions like printf(), their definitions can be found in a library that the linker searches through during compilation and linking. Therefore, there is no need to implement these functions in the code.
  • #1
CGandC
326
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 Wrichik Basu
Technology news on Phys.org
  • #2
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 Wrichik Basu, Mark44, berkeman and 1 other person
  • #3
I understand now, thanks!
 
  • Like
Likes berkeman

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

1. Why are declarations in not implemented in certain programming languages?

There are a few reasons why declarations in may not be implemented in certain programming languages. One reason could be that the language has its own built-in input/output functions that serve the same purpose as those in . Another reason could be that the language is designed for a specific platform or operating system that does not support functions.

2. Can I use functions in all programming languages?

No, functions are specific to the C programming language and may not be compatible with other programming languages. It is important to check the documentation of the language you are using to see if functions are supported.

3. Are there any alternatives to using functions?

Yes, there are some alternatives to using functions. For example, some languages have their own input/output libraries or you could use a third-party library that provides similar functionality to .

4. Why are functions important in programming?

functions are important in programming because they provide a standardized way to perform input/output operations. This makes it easier to create portable code that can be compiled and run on different platforms and operating systems.

5. Can I write my own functions?

Yes, you can write your own functions if needed. However, it is important to understand the underlying principles and functionality of before attempting to create your own versions.

Back
Top