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

What does %.2s mean in C language?

下次还敢
Release: 2024-05-02 18:42:49
Original
624 people have browsed it

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>
Copy after login

Output:

<code>输出长度为 2 个字符的字符串:He
输出长度为 10 个字符的字符串:Hello     
输出没有长度限制的字符串:Hello</code>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template