Giuseppina Rossi
Giuseppina Rossi

Reputation: 9

definition of function printf in C language

I have read that C language does not include instructions for input and for output and that printf, scanf, getchar, putchar are actually functions.

Which are the primitive C language instructions to obtain the function printf , then? Thank you.

Upvotes: 0

Views: 1619

Answers (3)

DevSolar
DevSolar

Reputation: 70223

No programming language provides true "primitives" for I/O. Any I/O "primitives" rely on lower abstraction levels, in this language or another.

I/O, at the lowest level, needs to access hardware. You might be looking at BIOS interrupts, hardware I/O ports, memory-mapped device controlers, or something else entirely, depending on the actual hardware your program is running on.

Because it would be a real burden to cater for all these possibilities in the implementation of the programming language, a hardware abstraction layer is employed. Individual I/O controllers are accessed by hardware drivers, which in turn are controlled by the operating system, which is providing I/O services to the application developer through a defined, abstract API. These may be accessed directly (e.g. by user-space assembly), or wrapped further (e.g. by the implementation of a programming language's interpreter, or standard library functions).

Whether you are looking at "commands" like (bash) echo or (Python) print, or library functions like (Java) System.out.println() or (C) printf() or (C++) std::cout, is just a syntactic detail: Any I/O is going through several layers of abstraction, because it is easier, and because it protects you from all kinds of erroneous or malicious program behaviour.

Those are the "primitives" of the respective language. If you're digging down further, you are leaving the realm of the language, and enter the realm of its implementation.


I once worked on a C library implementation myself. Ownership of the project has passed on since, but basically it worked like this:

  • printf() was implemented by means of vfprintf() (as was, eventually, every function of the *printf() family).
  • vfprintf() used a couple of internal helpers to do the fancy formatting, writing into a buffer.
  • If the buffer needed to be flushed, it was passed to an internal writef() function.
  • this writef() function needed to be implemented differently for each target system. On POSIX, it would call write(). On Win32, it would call WriteFile(). And so on.

Upvotes: 1

Werner Henze
Werner Henze

Reputation: 16726

If you want to use printf, you have to #include <stdio.h>. That file declares the function.

If you where thinking about how printf is implemented: printf might internally call any other functions and probably goes down to putc (also part of the C runtime) to write out the characters one-by-one. Eventually one of the functions needs to really write the character to the console. How this is done depends on the operating system. On Linux for example printf might internally call the Linux write function. On Windows printf might internally call WriteConsole.

Upvotes: 2

Codor
Codor

Reputation: 17595

The function printf is documented here; in fact, it is not part of the C language itself. The language itself does not provide a means for input and output. The function printf is defined in a library, which can be accessed using the compiler directive #include <stdio.h>.

Upvotes: 1

Related Questions