This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

sprintf and justification\padding?

I'm trying to pad\justify my text such that everything aligns properly. Can I do this from within sprintf? I've tried all kinds of combinations within the format string.

Basically, this is the output I would like:

#001
#002
#003
....
#010
etc

unsigned char num;
unsigned char buf[20];

num = 0x01;

sprintf(&buf[0], "#%bU", num);

How can I pad the zeros in there?

Thanks
James

  • I think this will do what you want:

    sprintf(buf, "#%3.3bu\n", num);

    Jon

  • Ahh cool...that works.

    What about lining up decimal points for floats?

    for example:

    #001: 1000.0
    #002: 20.1

    float f;
    unsigned char c;
    
    unsigned char buf1[20];
    unsigned char buf2[20];
    
    c = 1;
    f = 1000.0;
    sprintf(buf1, "#%3.3bu  %4.1f\n", c, f);
    
    c = 2;
    f = 20.1;
    sprintf(buf2, "#%3.3bu  %4.1f\n", c, f);
    

    Can't seem to get decimals aligned either.

    Thanks
    James

  • Err...

    My example of the decimal alignment didn't come out quite right. Here is how I am needing it to align:

    #001  1000.0
    #002    10.1
    etc
    

  • The printf/sprintf format specifiers are described in detail in the Manual

    They are standard ANSI 'C', so any decent 'C' textbook should also have the information you require!

  • I have stared at the manual's printf description for quite some time and haven't seen the answer that I'm looking for.

    I'll check some of my C books.

    But in the meantime, as a forum user, sharing ideas or discussing that which others may not understand\find is the sole purpose of a forum. Its a great way to get a quick answer someone may already know. So I encourage you, Mr. Neil, to participate if possible.

    Thanks
    James

  • Maybe this link would be helpful. That's where I went to figure out the first question.

    http://www.keil.com/support/man/docs/c51/c51_printf.htm

    Jon

  • "But in the meantime, as a forum user, sharing ideas or discussing that which others may not understand\find is the sole purpose of a forum."

    The purpose of *this* forum is to discuss Keil tools. ANSI C is very well catered for in other forums, not to mention some excellent books. If you want a clear, complete and concise description of the printf() format strings I would recommend "C: A reference manual" by Harbison and Steele. If you want to discuss ANSI C, I would recommend comp.lang.c on usenet.

    "Its a great way to get a quick answer someone may already know"

    printf() format strings are quite complex. In spite of having been a C programmer for 14 years I still regularly have to refer to the manual. As you'll see from Jon Ward's response, he too had to refer to the manual to answer this question. All you are really achieving by asking questions that are answered in the manual is wasting everyone's time.

    "So I encourage you, Mr. Neil, to participate if possible."

    Mr. Neil participates a great deal in this forum, but he understandably gets a bit irritated when people ask questions that are better answered by looking in the manual.

    So, I encourage you, Mr. Corbin, to Please read the manual.

    Stefan

  • In "%4.1f", the numbers do not mean, as you might expect, "4 digits before the decimal, and 1 after". They mean "total field width of 4 characters, with 1 character after the decimal point." For your right-justified floats to line up, the total field width must be at least as large as the value you are trying to print. 2000.1, for example, is six characters wide. Larger values will overflow the field.

    So, you might try:

    "#%03u %6.1f"

    or, including the two spaces in the field width of the float

    "#%03u%8.1f"

    The "precision" field (the number after the decimal point) is irrelevant for integers.