How do I align a number like this in C?

后端 未结 11 730
清歌不尽
清歌不尽 2020-12-04 12:13

I need to align a series of numbers in C with printf() like this example:

-------1
-------5
------50
-----100
----1000

Of

11条回答
  •  心在旅途
    2020-12-04 13:09

    As far as I can tell from the question, the amount of padding you want will vary according to the data you have. Accordingly, the only solution to this is to scan the data before printing, to figure out the widest datum, and so find a width value you can pass to printf using the asterix operator, e.g.

    loop over data - get correct padding, put into width
    
    printf( "%*d\n", width, datum );
    

提交回复
热议问题