How printf() works ??

Discussion in 'C' started by HARIT, Jul 25, 2011.

  1. HARIT

    HARIT New Member

    Joined:
    Jul 16, 2011
    Messages:
    17
    Likes Received:
    0
    Trophy Points:
    0
    anyone can explain me...How printf() function works.

    there any way to find out the coding of any library function in Turbo C??
     
  2. eriyer

    eriyer New Member

    Joined:
    Jan 22, 2011
    Messages:
    32
    Likes Received:
    0
    Trophy Points:
    0
    To get the code for Turbo C library functions you need to use tlib (included with turboc) - extract the function from the corresponding library (.lib file in directory lib) - disassemble to get an assembly language listing - I do not know if a better tool is available.

    As to how printf works I am appending the foll program - I am also enclosing as zip file
    Code:
    #include <stdio.h>
    #include <stdarg.h>
    #include <string.h>
    
    /*
    http://msdn.microsoft.com/en-us/library/kb57fad8%28v=vs.71%29.aspx 
    I used the above link for this example - to get the method of picking trailing args from the stack
    */
    void display_int(int d);
    void demo_printf(char *narration, ...); /* use ONLY 3 '.' i.e.  ... */
    void display_str(char *s);
    
    void main()
    {
    	demo_printf("the value of %s is between %d and %d\n***", "PI", 3, 4);
    }
    
    /* our toy implementation of printf */
    void demo_printf(char *narration, ...) /* use ONLY 3 '.' i.e.  ... to denote variable no of trailing arguments*/
    {
    	/* this illustrates use of only %s, %d and \n */
    	va_list argptr;
    	char *ptr;
    	int d, i;
    	
    	va_start(argptr, narration);
    	
    	for( i = 0; i < strlen(narration); i++ )
    	{
    		if( narration[ i ] != '%' && narration[ i ] != '\\' )
    			putc(narration[ i ], stdout);
    		else if( narration[ i ] == '%' )
    		{
    			if( narration[ i + 1 ] == 'd' )
    			{
    				d = va_arg(argptr, int);
    				display_int(d);
    				i++; /* skip next char in for loop */
    			}
    			else if( narration[ i + 1 ] == 's' )
    			{
    				ptr = va_arg(argptr, char *);
    				display_str(ptr);
    				i++; /* skip next char in for loop */
    			}
    		}
    		else if( narration[ i + 1 ] == 'n' )
    		{
    			putc('\n', stdout);
    			i++;
    		}
    	}
    	
    	va_end(argptr);
    	
    }
    
    void display_str(char *s)
    {
    	int i;
    	
    	for( i = 0; i < strlen(s); i++ )
    		putc(s[ i ], stdout);
    	
    }
    
    void display_int(int d)
    {
    	char intstr[ 15 ];
    	int i;
    	
    	i = sizeof(intstr) - 1;
    	intstr[ i-- ] = 0; /* null terminate*/
    	for( ; i >= 0 && d; i-- )
    	{
    		intstr[ i ] = '0' + d % 10;
    		d /= 10;
    	}
    	i++; /* to offset last decrement in for loop */
    	
    	display_str(intstr + i);
    }
    
    
     
  3. lionaneesh

    lionaneesh Active Member

    Joined:
    Mar 21, 2010
    Messages:
    848
    Likes Received:
    224
    Trophy Points:
    43
    Occupation:
    Student
    Location:
    India
    We have a basic tutorial on How Format String Functions work , You can have a look.
     
  4. K SAICHARAN

    K SAICHARAN New Member

    Joined:
    Jul 30, 2011
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    I helps to print what ever is typed between inverted comas.
     
  5. HARIT

    HARIT New Member

    Joined:
    Jul 16, 2011
    Messages:
    17
    Likes Received:
    0
    Trophy Points:
    0
    its not clear to me....please explain
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice