#include <stdio.h>
int main()
{
//字符输出的各种形式:
printf("字符输出的各种形式:\n");
char c = 'A';
printf("%%c = %c \n", c); //以字符形式输出,只输出一个字符;
printf("%%hhd = %hhd \n", c); //以字符对应的ASCII码输出,%hhd,%d,%hd等都可;
printf("%%-10c = %-10c \n", c); //输出字符最小宽度10,字符左对齐;
printf("%%10c = %10c \n", c); //输出字符最小宽度10,字符右对齐;
printf("%%010c = %010c \n", c); //输出字符最小宽度10,字符右对齐,空位补"0";
//字符串输出的各种形式:
printf("\n字符串输出的各种形式:\n");
char* s = "ABCD";
printf("%%s = %s \n", s); //输出字符串;
printf("%%-10s = %-10s \n", s); //输出字符串最小宽度10,字符串左对齐;
printf("%%10s = %10s \n", s); //输出字符串最小宽度10,字符串右对齐;
printf("%%010s = %010s \n", s); //输出字符串最小宽度10,字符串右对齐,空位补"0";
printf("%%.2s = %.2s \n", s); //截取字符串长度2,输出AB;
printf("%%5.2s = %5.2s \n", s); //输出字符串最小宽度5,字符串右对齐,截取字符串长度2;
printf("%%05.2s = %05.2s \n", s); //输出字符串最小宽度5,字符串右对齐,截取字符串长度2,空位补"0";
printf("%%-05.2s = %-05.2s \n", s); //输出字符串最小宽度5,字符串左对齐,截取字符串长度2,空位补"0";
printf("%%*.*s = %*.*s \n", 5, 3, s); //用参数控制字符串的最小宽度5和截取字符串的长度3;
//指针输出的三种形式:
printf("\n指针输出的三种形式:\n");
char* p = "abcd";
printf("%%p = %p \n", p); //输出指针;
printf("%%-10p = %-10p \n", p); //输出指针最小宽度10,指针地址左对齐;
printf("%%10p = %10p \n", p); //输出指针最小宽度10,指针地址右对齐;
}
Output result:
字符"A"输出的各种形式:
%c = A
%hhd = 65
%-10c = A
%10c = A
%010c = 000000000A
字符串"ABCD"输出的各种形式:
%s = ABCD
%-10s = ABCD
%10s = ABCD
%010s = 000000ABCD
%.2s = AB
%5.2s = AB
%05.2s = 000AB
%-05.2s = AB
%*.*s = ABC //参数5,3;
指针输出的三种形式:
%p = 00057D14
%-10p = 00057D14
%10p = 00057D14