Printf function of C ++

background

C ++ can be used to output cout.
But some of the cout output formatted data is very troublesome.

such as:

hour, min and sec represents the current time, demand: according to the current format of the output time 12:00:00.

//使用cout输出
cout<<hour<<":"<<min<<":"<<sec<<endl;

problem:

  1. So that the output is too much trouble, code readability is not high.
  2. If the single digits min, 0 plus convenient front.

Printf example

//使用printf输出上面的问题
print("%d:%0.2d:%0.2d",hour,min,sec);

Explanation:

When reading the above code in the familiar high.
printf first parameter is a string, the string representation of the output format.
% d indicates the data, starting from the second parameter, sequentially in the replacement string% d.
% 0.2d expressed, if the data is a single digit, the output is still two digits (preceded by 0).

Detailed Printf

You can see from the above example, the variable is responsible for storing data.
But sometimes, we hope to get the same output data in different formats. For example, in the morning 7:01, using cout becomes 7: 1, we want to get is 7:01.
So variable is responsible for storing data, printf responsible for the output format. (The same value can be different formats).

Integer

Placeholder
%d  //输出十进制
%o  //输出无符号整数的八进制
%x  //输出无符号整数的十六进制(字母小写)
%X	//输出无符号整数的十六进制(字母大写)
format
%5d		//输出宽度为5(右对齐)
%-5d	//输出宽度为5(左对齐)
%+5d	//输出宽度为5(输出符号)
%05d	//输出宽度为5(前面补0,而不是空格)
%#o		//输出八进制前的0(进制前的标识符)
%#x		//输出十六进制前的0x(进制前的标识符)
For example

Example 1:

int a=12;
printf("%d\n",a);       //输出12

Example 2:

int a=12;
printf("%o\n",a);       //输出14(因为十进制的12等于八进制的14)

Example 3:

int a=12;
printf("%x\n",a);       //输出c(因为十进制的12等于十六局进制的c)

Example 4:

int a=12;
printf("%5d\n",a);       //输出"   12"(默认右对齐,总共5个占位,前面补空格)

Example 5:

int a=12;
printf("%-5d\n",a);     //输出"12   "(负号表示左对齐,总共5个占位,后面补空格)

Example 6:

int a=-12;
printf("%+5d\n",a);     //输出"  -12"(正号表示输出符号,而不是在前面加"+")

Example 7:

//%02d表示输出宽度为2,不足2则在前面补0。比如表示时间的时候:
int hour=12,min=3,sec=2;
printf("%02d:%02d:%02d\n",hour,min,sec);    //输出"12:03:02"

Decimal

Placeholder
%f		//输出小数(保留到小数点后6位)
%e,E	//科学计数法,保留到小数点后6位(1.234560e+02)
%g,G	//以%f或%e中较短的输出宽度输出小数
format
%10f	//输出宽度为10(多出不会被截断)
%10.3f	//输出宽度为10(保留到小数点后3位)
For example

Example 1:

float a=1.2;
printf("%f\n",a);       //输出1.200000

Example 2 scientific notation:

float a=1234567;
printf("%e\n",a);   //输出1.234567e+06
//%e和%E分别表示用小写和大写的e表示

Example 3

When we do not know when the variable represents what way, it can be expressed in% g.
For example, if 1.2 scientific notation to represent very cumbersome: 1.200000e + 00
As another example, with 1234567 also cumbersome% f: 1,234,567.000000
then can be automatically determined by% g, represented in a better manner.

float a=1234567,b=1.2;
printf("a=%g\n",a);     //输出a=1.23457e+06
printf("b=%g\n",b);     //输出b=1.2

char

Placeholder
%c		//输出单个字符
%s		//输出字符串
For example

Example 1:

int a=65;
printf("%d\n",a);       //输出65
printf("%c\n",a);       //输出A

Example 2:

char * a="张三";
printf("我的名字是%s\n",a);     //输出"我的名字是张三"

pointer

%p		//输出指针指向的地址

For example:

int a=10;
int *p=&a;
printf("变量a的地址为%p\n",p);      //输出"变量a的地址为0x7ffeefbff54c"

other

%%		//输出%

Published 109 original articles · won praise 51 · views 90000 +

Guess you like

Origin blog.csdn.net/NetRookieX/article/details/104467678