打印顺序的数字用printf带填零 [英] Print numbers sequentially using printf with filling zeroes

查看:123
本文介绍了打印顺序的数字用printf带填零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++中,用printf我想打印数的顺序,所以我得到的,从一个for循环;

  1
2
...
9
10
11

和我创建这些数字文件。但是,当我使用LS一一列举了,我得到

  10
11
1
2
..

因此​​而不是试图使用bash来解决这个问题,我不知道如何可以打印;

  0001
0002
...
0009
0010
0011

感谢


解决方案

  I = 45;
的printf(%04i,我);

=>

  0045

基本上,0告诉printf的,以填补0,4个是数字计数和'我'是整数的占位符(你也可以用'D')

请参阅维基百科了解格式的占位符。

in C++, using printf I want to print a sequence of number, so I get, from a "for" loop;

1
2
...
9
10
11

and I create files from those numbers. But when I list them using "ls" I get

10
11
1
2
..

so instead of trying to solve the problem using bash, I wonder how could I print;

0001
0002
...
0009
0010
0011

and so on

Thanks

解决方案

i = 45;
printf("%04i", i);

=>

0045

Basically, 0 tells printf to fill with '0', 4 is the digit count and 'i' is the placeholder for the integer (you can also use 'd').

See Wikipedia about the format placeholders.

这篇关于打印顺序的数字用printf带填零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆