My own version of printf

This is a discussion on My own version of printf within the C Programming forums, part of the General Programming Boards category; Hi everyone I am developing my own console printing application for use with an embedded micro controller, I have created ...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    58

    My own version of printf

    Hi everyone

    I am developing my own console printing application for use with an embedded micro controller, I have created a simple method to allow me to print plain text to my console:

    Code:
    void cPrint (char *cMessage) {
    } // cPrint
    Which works very nicely, I would like to be able to include numbers in my text so as to be able to output system variables. I use a range of numbers from uint8_t to uint64_t, int, real and double formats etc…

    I’m not being 'very' greedy at this stage, and would like to print out (lets say for starters) a uint8_t value within my text.

    Does anyone know how I might do this? At the end of the operation the number will need to be part of the char variable this is passed to the cPrint method as shown above.

    Many thanks for any help you can give

    David

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Does your embedded compiler support vsprintf? If so, you can use vsprintf() to format the message, and then output the formatted string with your own simple "puts" variant.

    Writing a complete printf() is fairly complex, as it supports quite a lot of things.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Nov 2004
    Location
    India
    Posts
    515
    Can you use sprintf() ? If not, you'll have to parse the format string like printf() does and insert the numbers wherever required.
    Suppose you want to print a number 189
    Create a temporary buffer char buf[100] and initialize count to 0
    Then while your number isn't zero,
    Code:
    buf[count++]=(number%10)+'0';
    number/=10;
    Then just reverse the string in buf[] to get your number.
    Code:
    >+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.[-]>++++++++[<++++>-] <.>+++++++++++[<++++++++>-]<-.--------.+++.------.--------.[-]>++++++++[<++++>- ]<+.[-]++++++++++.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem building Quake source
    By Silvercord in forum Game Programming
    Replies: 16
    Last Post: 07-11-2010, 09:13 AM
  2. IF CONDITION plese help
    By birumut in forum C Programming
    Replies: 12
    Last Post: 03-06-2009, 08:48 PM
  3. Replies: 2
    Last Post: 03-05-2009, 09:25 AM
  4. I need help on this particular Linked List problem
    By sangken in forum C Programming
    Replies: 11
    Last Post: 08-06-2006, 12:26 AM
  5. Drawing tables in C
    By stanoman in forum C Programming
    Replies: 5
    Last Post: 10-09-2003, 10:14 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21