
c语言 sprintf的问题
想搞一个一直更换命令行背景颜色的实验,需要将一个随机数转换为字符串,但是如下程序并不成功。#include<stdio.h>#include<windows.h>#inc...
想搞一个一直更换命令行背景颜色的实验,需要将一个随机数转换为字符串,但是如下程序并不成功。
#include<stdio.h>
#include<windows.h>
#include<string.h>
void main()
{
union utype
{
int i;
char ch;
float a;
}temp;
temp.i=266;
printf("%d\n",temp.ch);
while(1)
{
system(sprintf("color 1%d",(8*rand)));//
Sleep(1000);
}
system("pause");
}
请问怎样才可以正确的将数字转换为字符,不是用强制转换哦。 展开
#include<stdio.h>
#include<windows.h>
#include<string.h>
void main()
{
union utype
{
int i;
char ch;
float a;
}temp;
temp.i=266;
printf("%d\n",temp.ch);
while(1)
{
system(sprintf("color 1%d",(8*rand)));//
Sleep(1000);
}
system("pause");
}
请问怎样才可以正确的将数字转换为字符,不是用强制转换哦。 展开
5个回答
展开全部
我弄好了
自动变换颜色哦
#include<stdio.h>
#include<windows.h>
#include<string.h>
#include <time.h>
void main()
{
srand((unsigned)time(NULL));
char com[20]={0};
union utype
{
int i;
char ch;
float a;
}temp;
temp.i=266;
printf("%d\n",temp.ch);
while(1)
{
int a= rand()%8;
sprintf(com,"color 1%d",a);
system(com);//
Sleep(1000);
}
system("pause");
}
自动变换颜色哦
#include<stdio.h>
#include<windows.h>
#include<string.h>
#include <time.h>
void main()
{
srand((unsigned)time(NULL));
char com[20]={0};
union utype
{
int i;
char ch;
float a;
}temp;
temp.i=266;
printf("%d\n",temp.ch);
while(1)
{
int a= rand()%8;
sprintf(com,"color 1%d",a);
system(com);//
Sleep(1000);
}
system("pause");
}
追问
在VC里面调试你的代码srand((unsigned)time(NULL));不行。还有那个随机数的也没解决。需要得到随机数是1到9
追答
#include
#include
#include
#include
void main()
{
union utype
{
int i;
char ch;
float a;
}temp;
char com[20]={0};
srand((unsigned)time(NULL));
temp.i=266;
printf("%d\n",temp.ch);
while(1)
{
int a= rand()%8+1;
sprintf(com,"color 1%d",a);
system(com);//
Sleep(1000);
}
system("pause");
}
这下我测试好了 VC6下 c语言环境
原来有问题是因为我在VC6下C++环境中测的
展开全部
system(sprintf("color 1%d",(8*rand)));//中sprintf()的用法不对
参考资料: http://tech.e800.com.cn/articles/2009/928/1254104995809_1.html
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
String string;
int reta;
string.sprintf("%d",reta);
string = string.fromUtf8(string.toStdString().data());
关键是最后一句
int reta;
string.sprintf("%d",reta);
string = string.fromUtf8(string.toStdString().data());
关键是最后一句
追问
你坑我java啊。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2011-12-07
展开全部
转ASIIC码 或数字直接 +“0”
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
itoa
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询