Thread: Simulating printf function

  1. #1
    Registered User
    Join Date
    Mar 2005

    Simulating printf function

    Hi all,
    I have to write a function named print, which simulates the library function printf, which has the following header:
    void print(const char *fmt, ...);
    It should be able to deal with "%d", "%f" and "%s" format specifiers pointed to by fmt as the printf function does. The function print will print other characters directly. The template (in blue) is given and is unchangable.

    I tried to use sscanf to read the characters in fmt, but realized everytime I called it, it reads from the first character and hence, gives me an infinite loop. I tried to copy fmt to another string of characters, but got segmentation fault.

    I am also not sure of how to handle the %s format specifier. Specifically, I don't know which data type I should use for the second argument in va_arg, as the string is a pointer(?).

    My code is below:

    #include <stdarg.h>
    #include <stdio.h>
    #include <string.h>
    void print(const char *fmt, ...);
    int main(void)
       float x=1.0;
       int i=1;
       char* s="Happy New Year.";
       print("x=%f, i=%d, and %s\n", x, i, s);
       return 0;
    void print(const char *fmt, ...)
       int c, flag = 0, next_int;
       double next_float;
       char next_string;
       va_list arg_addr;
       va_start(arg_addr, fmt);
       while (sscanf(fmt, "%c", &c) != EOF)
    	if (c == '%')
       	       flag += 1;
    	if (flag == 1)
                           if (c == 'd')
       		next_int = va_arg(arg_addr, int);
          		printf("%d", next_int);
                                    flag = 0;
       		if (c == 'f')
             		      next_float = va_arg(arg_addr, double);
    	                      printf("%lf", next_float);
                                          flag = 0;
             		      if (c == 's')
                		              next_string = va_arg(arg_addr, int);
                   	                              printf("%s", next_string);
                                                 flag = 0;
       	   printf("%c", c);
    Please help.

    Thank you.


  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    > while (sscanf(fmt, "%c", &c) != EOF)
    sscanf doesn't work it's way along the string

    while ( (c=*fmt++) != '\0' )
    This walks the string, copying each char in turn into variable c

    > I am also not sure of how to handle the %s format specifier
    Like you do with anything else
       char *next_string;
       next_string = va_arg(arg_addr, char*);
    You might want to work on your identation as well.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Mar 2005

    simulating printf function


    I guess using a switch statement would be preferable.. switch (c) will help you get rid of so many if - else statement..

    It can be easily done .. think abt it..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. segmentation fault upon reload
    By yabud in forum C Programming
    Replies: 8
    Last Post: 12-18-2006, 06:54 AM
  2. Simple C question: user input to repeat a loop
    By evernaut in forum C Programming
    Replies: 2
    Last Post: 11-18-2006, 09:23 AM
  3. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  4. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  5. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM