[C language] output format control characters, format modifiers, and escape characters of printf

Format control characters:

  • %d: Used to output a decimal signed integer.
  • %u: Used to output decimal unsigned integers.
  • %f: Used to output decimal floating-point numbers.
  • %e Or  %E: Used to output floating-point numbers in exponential form.
  • %g Or  %G: used to output floating-point numbers,  %f or  is automatically selected according to the size of the value %e.
  • %x or  %X: for outputting hexadecimal.
  • %c: Used to output a single character.
  • %s: Used to output a string.
  • %p: Used to output pointer address.
  • %lu: Used to output an unsigned long integer.
  • %lld or  %I64d: for outputting a signed long long integer.
  • %llu or  %I64u: for outputting an unsigned long long integer.
  • %Lf: Used to output long double-precision floating-point numbers.
  • %% Format control character: used to output percent signs  %, two percent signs need to be connected together when outputting.

Format modifiers:

  • -: Align left.
  • +: Output sign (plus "+" in front of the positive number).
  • #: Octal prefix (0), hexadecimal prefix (0x or 0X), or floating point decimal point (.).
  • 0: Pad the data output with 0's on the left instead of the default space character.
  • m.n: m is the specified minimum width, n and is the specified precision.
  • *: Used to receive the width and precision passed in dynamically. For example, %*.*f it means to output floating-point numbers, and the width and precision are dynamically passed in with two parameters of type int.

escape character:

  • \a: Alarm (bell ringing).
  • \b: Backspace (back) one space.
  • \f: Change page.
  • \n: Newline.
  • \r: Enter.
  • \t: Horizontal tabs.
  • \v: Vertical tab character.
  • \\: Backslash.
  • \':apostrophe.
  • \":Double quotes.
  • \?:question mark.
  • \0: String end flag.

Code demo:

#include<stdio.h>

int main() {
	// 整型
	int a = 123;
	printf(" % d\n", a); // 输出 123

	// 无符号整型
	unsigned int b = 456;
	printf("%u\n", b); // 输出 456

	// 浮点数
	double c = 1.23;
	printf("%f\n", c); // 输出 1.230000

	// 指数形式浮点数
	double d = 12345.678;
	printf("%e\n", d); // 输出 1.234568e+04

	// 十六进制
	int e = 0x123;
	printf("%x\n", e); // 输出 123

	// 字符
	char f = 'a';
	printf("%c\n", f); // 输出 a

	// 字符串
	char g[] = "hello world";
	printf("%s\n", g); // 输出 hello world

	// 指针地址
	int* h = &a;
	printf("%p\n", h); // 输出 a 的地址

	// 无符号长整型
	unsigned long i = 1234567890ul;
	printf("%lu\n", i); // 输出 1234567890

	// 有符号长长整型
	long long j = -123450000000ll;
	printf("%lld\n", j); // 输出 -123450000000

	// 无符号长长整型
	unsigned long long k = 123450000000ull;
	printf("%llu\n", k); // 输出 123450000000

	// 长双精度浮点数
	long double l = 1.23;
	printf("%Lf\n", l); // 输出 1.230000

	// 百分号转义
	printf("%%\n"); // 输出 %

	// 格式修饰符
	int num = 123;
	double fnum = 3.14159;
	printf("%-5d\n", num); // 输出 123   (左对齐)
	printf("%+d\n", num); // 输出 +123(输出符号)
	printf("%#x\n", num); // 输出 0x7b(十六进制前缀)
	printf("%05d\n", num); // 输出 00123(用 0 左对齐填充)
	printf("%.2f\n", fnum); // 输出 3.14 (精度控制)
	printf("%*.*f\n", 8, 2, fnum); // 输出    3.14 (动态宽度和精度,从后面两个 int 参数中传入)

	// 转义字符
	printf("hello\tworld\n"); // 输出 hello    world

	return 0;
}

        For the use of modifiers and format control characters, you can refer to specific C language tutorials or reference manuals, and choose to use them according to actual needs. At the same time, you can use the return value of the printf function to judge whether the output is successful.

Guess you like

Origin blog.csdn.net/crr411422/article/details/131033649