printf functionality

printf functionality

Page 1 of 1

5 Replies - 2495 Views - Last Post: 11 June 2010 - 10:49 AM Rate Topic: -----

#1 isha_03  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 11-April 10

printf functionality

Posted 10 June 2010 - 10:15 PM

Hi
Please let me know how the printf function acts internally . without using printf how can i print a statement in c . I want to know the logic ..
Is This A Good Question/Topic? 0
  • +

Replies To: printf functionality

#2 GWatt  Icon User is offline

  • member icon

Reputation: 278
  • View blog
  • Posts: 3,078
  • Joined: 01-December 05

Re: printf functionality

Posted 10 June 2010 - 10:35 PM

there is a write function that will write to a file descriptor.

write() from opengroup
Was This Post Helpful? 0
  • +
  • -

#3 Banfa  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 83
  • View blog
  • Posts: 109
  • Joined: 07-June 10

Re: printf functionality

Posted 11 June 2010 - 01:50 AM

The internal working of printf is implementation defined. Every platform is free to implement it any way they want. At the extreme that means that if a platform existed where printf was actually implemented as an assembler instruction then for that platform the implementation would consist of calling that instruction.

This is true of all library functions, the platform is free to implement them however it chooses and you would really expect the platform makers to have implemented the standard library taking all advantage of the specific facilities of the platform to enhance library performance.


now all that said it is unlikely that printf will ever be implemented as an assembler instruction. I have seen a number of platforms that implement the whole printf family though 1 underlying function that implements the format parsing. If you would like to see 1 implementation to try and understand it then I suggest you download the GNU C Library source code and look it up in there.
Was This Post Helpful? 0
  • +
  • -

#4 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: printf functionality

Posted 11 June 2010 - 02:25 AM

Worth a read here for some useful background and links to useful resources
http://en.wikipedia.org/wiki/Printf
Was This Post Helpful? 0
  • +
  • -

#5 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1371
  • View blog
  • Posts: 4,752
  • Joined: 19-February 09

Re: printf functionality

Posted 11 June 2010 - 10:25 AM

The computer can output characters, so you can use putchar() and puts().

For other data types you may need to convert them to character type.
E.g. int you can use non-standard function itoa(), if available or create your own, which is not too difficult.


For an idea of what occurs in printf() have a look at this code:-
Variable Argument List Example
Was This Post Helpful? 0
  • +
  • -

#6 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: printf functionality

Posted 11 June 2010 - 10:49 AM

Generally speaking printf() does NOT actually display or "print" anything -- it passes this on to the OS to do. Printing is done via the stdout stream (which is generally owned by the OS).

So for example at its base level printf in windows probably does something similar to this snippet I posted a while back in assembly. It uses two OS function GetStdHandle which returns the handle to the standard output, and then WriteFile to actually send the data to the stream.

However that is just a guess -- BUT there is often a way you can find out how YOUR compiler does it! If you have downloaded VS 2010 Beta2 it has the source code for the C Runtime library with it, you can look in the source to see exactly what happens. It should be quite an adventure!

For me the source can be found at: "C:\Program Files(x86)\Microsoft Visual Studio 10.0\VC\crt\src" printf.c leads to vprintf.c which makes a call to outfn which is of type OUTPUTFN which is defined in internal.h -- so maybe you can't actually look at the source code for these internal functions... sorry about that.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1