C语言程序,用于以不同格式打印字符和字符串。
下面的算法解释了C语言中包含的打印字符和字符串的不同格式的过程。
步骤1:读取要打印的字符。
步骤2:在编译时读取名称。
步骤3:使用格式说明符以不同格式输出字符。
- printf("%c
%3c
%5c
", x,x,x); - printf("%3c
%c
", x,x); - printf("
");
步骤4:使用格式说明符以不同格式输出字符串。
- printf("%s
", name); - printf("%20s
", name); - printf("%20.10s
", name); - printf("%.5s
", name); - printf("%-20.10s
", name); - printf("%5s
", name);
示例
以下是C语言程序,用于以不同格式打印字符和字符串:
#include<stdio.h> main(){ char x = 'T'; static char name[20] = "Tutorials Point"; printf("OUTPUT OF CHARACTERS
"); printf("%c
%3c
%5c
", x,x,x); printf("%3c
%c
", x,x); printf("
"); printf("OUTPUT OF STRINGS
"); printf("%s
", name); printf("%20s
", name); printf("%20.10s
", name); printf("%.5s
", name); printf("%-20.10s
", name); printf("%5s
", name); }
输出
执行上述程序后,将产生以下输出:
OUTPUT OF CHARACTERS T T T T T OUTPUT OF STRINGS Tutorials Point Tutorials Point Tutorials Tutor Tutorials Tutorials Point
示例
考虑另一个程序来检查不同的格式:
#include<stdio.h> main() { char x = 'T'; static char name[20] = "Tutorials Point"; printf("OUTPUT OF CHARACTERS
"); printf("%c
", x); printf("%c
%3c
%5c
", x,x,x); printf("%3c
%c
", x,x); printf("%c
%3c
%5c
", x,x,x); printf("%3c
%c
", x,x); printf("
"); printf("OUTPUT OF STRINGS
"); printf("%.5s
", name); printf("%-10.10s
", name); printf("%.20s
", name); printf("%20.10s
", name); printf("%20s
", name); printf("%10s
", name); }
输出
执行上述程序后,将产生以下输出:
OUTPUT OF CHARACTERS T T T T T T T T T T T OUTPUT OF STRINGS Tutor Tutorials Tutorials Point Tutorials Tutorials Point Tutorials Point
广告