sprintf中细节用法

在看程序时看到这样两句话

sprintf(format, "| %c-%ds |", '%', length);

sprintf(Column, format, name);

现在假设length=17, name="test";

那么format是|%-17s|,Column是|test             |;

第一句程序中替代%c的是%,替代%d的是length,其实这还是带有sprintf参数的,%和参数之间带有的-表示左对齐,数字表示这个填入参数的长度,如果填入参数不足这个长度,比如17,就会用空格代替。在这个例子当中,||之间一定是17的宽度,但是test只有4个宽度,所以后面有13个空格。

还有一些细节可以参考该文章https://blog.csdn.net/oyhb_1992/article/details/75095472#commentBox

猜你喜欢

转载自blog.csdn.net/qq_34489443/article/details/89453485
今日推荐