C_learn/1_lesson/05_printf/main.c

47 lines
1.3 KiB
C
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <stdio.h>
int main(int argc, char *argv[])
{
int a = 100;
printf("a = %d\n",a);
printf("a = %o\n",a); // 八进制输出
printf("a = %#o\n",a); // %#o 可以输出八进制数的前导符
printf("a = %x\n",a); // 十六进制输出
printf("a = %#x\n",a);
// 输出浮点型数据,float使用%f,double使用%lf
float b = 3.1415926;
double c = 2345.2345;
printf("b = %f\n",b);
printf("c = %lf\n",c);
// 输出字符,使用%c使出字符使用%d可以输出字符的ascii码值
char d = 'y';
printf("d = %c %d\n",d,d);
char e[] = "hello world";
printf("e = %s\n",e);
// 输出地址,使用%p
int f = 999;
// &:取一个变量的地址,一般地址用十六进制标识
printf("&p = %#p\n", &f); // 获取当前变量的地址用 %p
int m = 456;
printf("%d%d\n", m, m);
printf("%5d%5d\n", m, m);
// %05d:输出的宽度为5右对齐如果实际数据的宽度小于5则左边位置补0
printf("%05d%05d\n", m, m);
//%-5d:输出的宽度为5左对齐如果实际数据的宽度小于5则右边补空格
printf("%-5d%-5d\n", m, m);
float n = 3.678;
printf("n = %f\n", n);
//%.2f:小数点后保留两位,并且可以四舍五入
printf("n = %.2f\n", n);
}