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

What does %.1 mean in C language?

下次还敢
下次还敢Original
2024-05-02 18:39:48439browse

In the printf() function of C language, %.1 represents the floating-point variable output format, specifying one digit after the decimal point.

What does %.1 mean in C language?

The meaning of %.1 in C language

%.1 is in the printf() function in C language Commonly used format specifiers. It is used to specify the output format of floating point variables.

Detailed description:

  • % means to output a variable.
  • . is used to separate the numbers before the decimal point and the numbers after the decimal point.
  • 1 means outputting one digit after the decimal point.

Example:

<code class="c">#include <stdio.h>

int main() {
  float num = 3.141592;
  printf("小数点后一位:%.1f\n", num);
  return 0;
}</code>

Output:

<code>小数点后一位:3.1</code>

In the above example, the printf() function uses the %.1f format specification The symbol outputs the floating-point variable num to one decimal place. Therefore, the output is 3.1.

Note:

  • %.1 is only applicable to floating-point variables. For integer variables, you can use the %d format specifier.
  • If there are no digits after the decimal point, the output is 0.
  • If there is more than one digit after the decimal point, the extra digits will be truncated.

The above is the detailed content of What does %.1 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