How does "printf()" work?

Asked

Viewed 819 times

5

How the code works behind the function printf() do C? I am aware of what this function does, I want to know how it does.

  • 1

    Good question. See this answer: http://stackoverflow.com/questions/4867229/code-for-printf-function-in-c

  • This in English, but gives a good explanation of the function: http://www.cplusplus.com/reference/cstdio/printf/

  • Did any help you more? You need something to be improved?

1 answer

5

Each compiler can use as much code as they like in their library as long as they do everything specified. And most make the source code available, so the best thing is to look there. Some places:

  • If you have Visual Studio, you can see it on your computer:

    C:\Program Files\Microsoft Visual Studio 10.0\VC\crt\src
    
  • GCC

  • Apple (specifically the function you want)
  • Openbsd (all the way)
  • uclibc
  • A little simpler code

Note that codes are usually bad to read as they seek to be optimized and not readable.

The function essentially makes a parse in string sent, prints normal characters and switches to specific functions when you have to replace a pattern with a value that will often have to be converted to string first. For printing depends on the operating system or even the hardware it is running on. You may need to write to a port or you may need to call an OS function, or you may need a more complex and specific solution.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.