Achyuta Aich
Achyuta Aich

Reputation: 581

system call in C library function

Since the system calls which any library function of C (say printf()) makes is OS dependant , does that imply that we have a different function printf() for different OS ?

Upvotes: 0

Views: 148

Answers (2)

zneak
zneak

Reputation: 138051

It depends on your definition of "different", because I can think of at least three levels of difference:

  • Interface differences
  • High-level code differences
  • Machine code differences

The C standard suggests an interface, and this interface is supposed to be respected across the board. This means that for any OS with a C standard library, the OS should show your program an outlet called printf, and if your program plugs into it, it can expect it to behave as documented. This means that for all you're concerned, printf is the same across the board.

This doesn't mean that printf has to be the same piece of code in every standard library. If someone told me to write a printf function and told you to write a printf function, we could have a different approach, and that would still be fine as long as we both respected the documented behavior. As a matter of fact, for copyright reasons, you can be certain that the code for Windows's printf is different from Linux's printf code.

And finally, even with the same source code, printf would have to be different to accommodate platform differences. You can't expect an x86 printf to work on ARM, for instance. And as you noted, you can't expect a Linux printf to work on Windows because of platform conventions and system call differences.

So the machine code behind the printf outlet will be different, but the point of the standard is to make it work the same.

Upvotes: 2

Jean-Baptiste Yunès
Jean-Baptiste Yunès

Reputation: 36401

If you mean "printf behave differently on different OSes", then the answer is:

  • externally (from the user of the function viewpoint) no, its semantics is standardized. That means that a given call to such a function leads to the same results, whatever is the OS.
  • internally probably, its implementation is free. That means, that the computation that such a function will really do to produce you the result can be different.

Upvotes: 1

Related Questions