sprintf函数的使用2

    前车之鉴:spintf函数容易望文生义,误以为是一个打印函数。实质上是一个字符串格式化命令。主要功能是把格式化的数据写入某个字符串中,至于需要打印的话,还是得需要打印函数来对其进行打印。

    sprintf函数相信通过以下代码可以知道它的强大。下面代码是转载的一篇博客中的,我对应着整理在一起来。希望可以共同学习:  

#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
int main()
{
	
/*	char buffer[10];
    int i = 0x20401;
	sprintf(buffer, "string:%x\n", i );
  
	printf("%s\n",buffer);*/
	int number=12345;
	char string[32];
//	itoa(number,string,10);
//	std::cout<<"integer="<<number<<" string="<<string<<std::endl;  
	sprintf(string,"%d",number);
	std::cout<<"the number in this way is"<<string<<std::endl;

    sprintf(string,"%8d%8d",123,4567);
	std::cout<<"the number in this way is"<<string<<std::endl;//如博客中所述

	sprintf(string, "%-8d%8d", 123, 4567); 
	std::cout<<"the number in this way is"<<string<<std::endl;
	
	sprintf(string, "%8x", 4567); //小写16 进制,宽度占8 个位置,右对齐
	std::cout<<"the number in this way is"<<string<<std::endl;
	
	sprintf(string, "%-8X", 4567); //大写16 进制,宽度占8 个位置,左对齐
	std::cout<<"the number in this way is"<<string<<std::endl;

	sprintf(string, "%08X", 4567); //产生:"000011D7"
	std::cout<<"the number in this way is"<<string<<std::endl;
	
	short si = -1;
	sprintf(string, "%04X", si);
	std::cout<<"the number in this way is"<<string<<std::endl;//这个地方不是很容易理解,应该多思考。

    sprintf(string,"%04X",(unsigned short)si);
	std::cout<<"the number in this way is"<<string<<std::endl;
	
	sprintf(string, "%f", 3.1415926); //产生"3.141593"
	std::cout<<"the number in this way is"<<string<<std::endl;//默认保留小数点后6位 。下面自己控制打印小数点保留的位数
	
	sprintf(string, "%10.3f", 3.1415626);   //右对齐
	std::cout<<"the number in this way is"<<string<<std::endl;
	
	sprintf(string, "%-10.3f", 3.1415626);	//左对齐
	std::cout<<"the number in this way is"<<string<<std::endl;
	
	sprintf(string, "%.3f", 3.1415626); //不指定总宽度,产生:"3.142"
	std::cout<<"the number in this way is"<<string<<std::endl;
	
	//int i=100;
//	sprintf(string,"%.2f",i);
//	std::cout<<"the number in this way is"<<string<<std::endl;  //是产生100.00吗??

//	sprintf(string, "%.2f", (double)i);					//凌乱了,那么下面这个呢?也跟上面一致吗?猜想应该有一个是100.00吧
	//std::cout<<"the number in this way is"<<string<<std::endl;
	
	for(int i=32;i<127;i++)
	{
		printf("[%c]:%3d 0x%#04X/n",i,i,i);  //这个也有点难理解暂时。
	}

	char *who="I";
	char *whom="CSDN";
	sprintf(string,"%s love %s",who,whom);
	std::cout<<"the number in this way is"<<string<<std::endl;
	
	char a1[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G'};
	char a2[] = {'H', 'I', 'J', 'K', 'L', 'M', 'N'};
	//sprintf(string, "%s%s", a1, a2); //Don't do that!
	//sprintf(string, "%7s%7s", a1, a2);
	//sprintf(string, "%.7s%.7s", a1, a2);//产生:"ABCDEFGHIJKLMN" that is a correct answer.
	std::cout<<"the number in this way is"<<string<<std::endl; //why???

	sprintf(string, "%.*s%.*s", 7, a1, 7, a2);
	std::cout<<"the number in this way is"<<string<<std::endl;//还是有点没太理解。。。
	
	sprintf(string,"%u",&i);//%u表示unsigned int 来表示数据,不过人们还是通常采用16进制来表示地址
	std::cout<<"the number in this way is"<<string<<std::endl;

	sprintf(string,"%08X",&i);
	std::cout<<"the number in this way is"<<string<<std::endl;

	sprintf(string, "%p", &i);
	std::cout<<"the number in this way is"<<string<<std::endl;//sprintf中专门提供的打印地址的%p

	sprintf(string, "%0*x", 2 * sizeof(void *), &i);
	std::cout<<"the number in this way is"<<string<<std::endl;	//还是有细微区别的???和博客上面讲的

	int len=sprintf(string,"%d",number);
	std::cout<<len<<std::endl;

	srand(time(0));
	char s[64];
	int offset=0;
	for(int j=0;j<10;j++)  //这部分代码也有点难以理解,多消化几遍吧。读完了整篇博客后确实发现sprintf函数很是强大啊。
	{
		offset+=sprintf(s+offset,"%d",rand()%100);
	}
	s[offset-1]='\n';
	printf(s);
	


	return 0;


}

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页