本文介绍了什么是与意义QUOT;% - * S"在格式化字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#define FMT "%-*.*s e = %6ld, chars = %7ld, stat = %3u: %c %c %c %c\n"

本宏传递到的printf 功能。是什么% - *?* S 的意思是

This macro is passed into the printf function. What does %-*.*s mean?

推荐答案

您可以阅读这里的printf手册页:http://pubs.opengroup.org/onlinepubs/009695399/functions/fprintf.html.但它更像是一个比一个教程法律文本,这将是很难用有限的英语技能理解。

You can read the manual page for printf here: http://pubs.opengroup.org/onlinepubs/009695399/functions/fprintf.html. But it's more like a law text than a tutorial so it will be hard to understand with limited English skills.

我不知道 *。* ,不得不阅读man page自己。这真有趣。让我们先从一个简单的的printf(%S,ABC)。它将打印字符串 ABC

I didn't know *.* and had to read the man page myself. It's interesting. Let's start with a simple printf("%s", "abc"). It will print the string abc.

的printf(%8S,ABC)将打印      ABC(8是最小的字段宽度)。

printf("%8s", "abc") will print "     abc" (8 is the minimum "field width").

的printf(% - 8S,ABC)将打印ABC      (减号表示在场上左对齐)。

printf("%-8s", "abc") will print "abc     " (the minus indicates left alignment in the field).

现在的明星:
的printf(% - * S,8,ABC)将打印相同的。星号表示该字段宽度将作为一个参数的printf传递。这样,就可以以编程方式更改。

Now for the star:printf("%-*s", 8, "abc") will print the same. The star indicates that the field width will be passed as a parameter to printf. That way it can be changed programmatically.

现在为precision,即:
的printf( - %* 10S81234567890123)将打印1234567890(以下简称precision是的情况下,最大字段宽度字符串)。

Now for the "precision", that is :printf("%-*.10s", 8, "1234567890123") will print "1234567890" (the "precision" is the maximum field width in case of strings).

最后
的printf(% - *。*的,8,10,1234567890123)将打印和以前一样,但最大字段宽度作为参数,了。

And finallyprintf("%-*.*s", 8, 10, "1234567890123") will print the same as before, but the maximum field width is given as a parameter, too.

这篇关于什么是与意义QUOT;% - * S"在格式化字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 09:32