C printf Usage Guide

The printf function in the C language is used to display output, with the following usage:

  1. Grammar: the rules governing the structure of words and their arrangement in sentences.
printf("格式控制字符串", 参数1, 参数2, ...);

In the format control string, there can be both regular characters and format conversion directives to specify the output format, while the parameters are the content to be output.

  1. Format conversion indicator:
  1. %d: output a signed decimal integer.
  2. Output a decimal unsigned integer.
  3. %f: Output a floating-point number.
  4. %c: outputs a single character.
  5. %s: Prints out a string.
  6. %p: print out the value of the pointer.
  1. 原文:我喜欢读书,因为它让我感觉到很放松。
    Paraphrase: I enjoy reading because it helps me relax.
int num = 10;
float f = 3.14;
char ch = 'A';
char str[10] = "Hello";
printf("整数:%d\n", num);
printf("浮点数:%f\n", f);
printf("字符:%c\n", ch);
printf("字符串:%s\n", str);
printf("指针:%p\n", &num);

Output the result:

整数:10
浮点数:3.140000
字符:A
字符串:Hello
指针:0x7ffd324e6aac

Please note that in format control strings, the characters following the % sign such as numbers, symbols, and periods can be used to specify the output format. For example, %5d represents a decimal integer with a width of 5, and %.2f represents a floating point number with two decimal places. For more detailed format specifications, refer to the C language’s formatting input and output documentation.

bannerAds