Home >Backend Development >C#.Net Tutorial >What does %.2s mean in C language?
The meaning of %.2s in C language
In the formatted output function (such as printf()) of C language, "%.2s" means:
Therefore, the "%.2s" format specifier means to output a string of 2 characters in length. If you want to output a string of 10 characters in length, you can use the "%.10s" format specifier. If you want to output a string with no length limit, you can use the "%s" format specifier.
Example:
<code class="c">#include <stdio.h> int main() { char str[] = "Hello"; printf("输出长度为 2 个字符的字符串:%.2s\n", str); printf("输出长度为 10 个字符的字符串:%.10s\n", str); printf("输出没有长度限制的字符串:%s\n", str); return 0; }</code>
Output:
<code>输出长度为 2 个字符的字符串:He 输出长度为 10 个字符的字符串:Hello 输出没有长度限制的字符串:Hello</code>
The above is the detailed content of What does %.2s mean in C language?. For more information, please follow other related articles on the PHP Chinese website!