Home >Backend Development >C#.Net Tutorial >What does %.2s mean in C language?

What does %.2s mean in C language?

下次还敢
下次还敢Original
2024-05-02 18:42:49643browse

The meaning of %.2s in C language

In the formatted output function (such as printf()) of C language, "%.2s" means:

  • %: Format specifier, indicating that the data will be output in a specific format.
  • .: Precision indicator, indicating the length of the output string.
  • 2: Specify the length of the output string to be 2 characters.
  • s: Specify the output data type as string.

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn