sprintf函数

sprintf指的是字符串格式化命令
头文件:#include<stdio.h>
功能:把格式化的数据读入某个字符串中(最终结果是字符串类型)

格式
char str[100];
str( 字符串名,控制格式符,待转化数字 )
注意:控制格式符要用双引号

1.进制转换用法

#include<cstdio>
#include<iostream>
using namespace std;
int main(){
	char str[100];
	
	sprintf(str,"%d",1239);//1339十进制 
	cout<<"转化十进制读入字符串:"<<str<<endl<<endl;
	
	
	
	sprintf(str,"%o",1239);// 2327八进制
	cout<<"转化八进制读入字符串:"<<str<<endl<<endl;
	
	
	
	sprintf(str,"%#o",1239);//02327八进制标号字符 
	cout<<"转化规范化八进制读入字符串:"<<str<<endl<<endl;
	
	
	
	sprintf(str,"%x",1239);//4d7 十六进制 
	cout<<"转化十六进制读入字符串:"<<str<<endl<<endl;
	
	
	
	sprintf(str,"%#x",1239);//0x4d7十六进制标号字符 
	cout<<"转化规范化16进制读入字符串:"<<str<<endl<<endl;
	return 0;
}

在这里插入图片描述
2.与C++string结合实现数字转换字符操作

#include<cstdio>
#include<iostream>
using namespace std;
int main(){
	char str[100];
	sprintf(str,"%d",1239);
	string s(str);
	cout<<s<<endl;
	return 0;
}

在这里插入图片描述

3.字符串拼接操作

#include<cstdio>
int main(){
	char *a="abc";
	char *b="def";
	char str[10];
	sprintf(str,"%s%s",a,b);
	printf("%s\n",str);
	return 0;
}

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值