②o格式:以无符号八进制形式输出整数。对长整型可以用"%lo"格式输出。同样也可以指定字段宽度用“%mo”格式输出。 例: main() { int a = -1; printf("%d, %o", a, a); } 运行结果:-1,177777 程序解析:-1在内存单元中(以补码形式存放)为(1111111111111111)2,转换为八进制数为(177777)8。
//还有一个特殊的格式%*.* ,这两个星号的值分别由第二个和第三个参数的值指定 printf("%.*s \n", 8, "abcdefgggggg"); printf("%*.*f \n", 3,3, 1.25456f);使用printf ()函数 printf()的基本形式: printf("格式控制字符串",变量列表);#include<cstdio>int main() { //for int int i=30122121; long i2=309095024l; short i3=30; unsigned i4=2123453; printf("%d,%o,%x,%X,%ld,%hd,%u\n",i,i,i,i,i2,i3,i4);//如果是:%l,%h,则输不出结果 printf("%d,%ld\n",i,i2);//试验不出%ld和%d之间的差别,因为long是4bytes printf("%hd,%hd\n\n\n",i,i3);//试验了%hd和%d之间的差别,因为short是2bytes //for string and char char ch1='d'; unsigned char ch2=160; char *str="Hello everyone!"; printf("%c,%u,%s\n\n\n",ch1,ch2,str);//unsigned char超过128的没有字符对应 //for float and double,unsigned and signed can not be used with double and float float fl=2.566545445F;//or 2.566545445f double dl=265.5651445; long double dl2=2.5654441454;
//%g没有e格式,默认6位包括小数点前面的数, //%f没有e格式,默认6位仅只小数点后面包含6位 //%e采用e格式,默认6位为转化后的小数点后面的6位 printf("%f,%e,%g,%.7f\n",fl,dl,dl,dl); printf("%f,%E,%G,%f\n",fl,dl,dl,dl);//%F is wrong printf("%.8f,%.10e\n",fl,dl); printf("%.8e,%.10f\n\n\n",fl,dl);
//for point int *iP=&i; char *iP1=new char; void *iP2;//dangerous! printf("%p,%p,%p\n\n\n",iP,iP1,iP2);