-1

Sorry for my bad english.

I'm using macOS 12.3 in M1 Pro MacBook Pro

I want to know about source code of printf in C standard library.

I can find stdio.h file in usr/include directory.

But I can't find printf.c or stdio.c file in same directory.

And I think Apple Open Source's printf.c is too complicate, dosen't looks like a real code in macOS and dosen't helpful for me. Is that Apple Open Source printf.c code is really used in macOS?

https://opensource.apple.com/source/xnu/xnu-201/osfmk/kern/printf.c.auto.html

  • 1
    Can you link to that "Apple Open Source printf.c code"? For what it's worth, `printf` _isn't_ very simple; [here's](https://github.com/bminor/musl/blob/master/src/stdio/vfprintf.c) the musl c library's implementation. – AKX Mar 23 '22 at 06:23
  • https://opensource.apple.com/source/xnu/xnu-201/osfmk/kern/printf.c.auto.html Here is the link! – Temeraire Eurus Mar 23 '22 at 06:24
  • 4
    Yeah, `printf` is nothing like a "simple function", it's one of the most complex one of the C library... And it's often full of dirty tricks, like `goto` usage, variadic arguments processing, and bitfield parsing... – Wisblade Mar 23 '22 at 06:26
  • You've linked the kernel implementation of printf, which is simpler than the Libc (C standard library) one. [Here's the Apple libc implementation of `printf`.](https://opensource.apple.com/source/Libc/Libc-1439.40.11/stdio/FreeBSD/vfprintf.c.auto.html) – AKX Mar 23 '22 at 06:28

1 Answers1

5

The newest Apple Open Source libc implementation of vfprintf (which is the generic underlying function that plain printf calls) can be found here on opensource.apple.com.

It's about 1400 lines, and yes, it is complex.

Some other alternate implementations (which are complex too) are:

AKX
  • 152,115
  • 15
  • 115
  • 172