大厦字符串从C变量 [英] Building Strings from variables in C

查看:95
本文介绍了大厦字符串从C变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用套接字,我的操作系统类的礼貌裸机21点游戏。
我们得到一个socket接口已经通过该字符数组来回。

I'm working on a bare-bones Blackjack game that uses sockets, courtesy of my Operating Systems class. We were given a socket interface already which passes an array of characters back and forth.

我的希望的我可以做这样的事情:

I had hoped I could do something like this:

char[] msgOut = printf("Dealer's Card is %C %C", char1, char2);
sendMsg(msgOut);

后,谷歌搜索使我确定的printf的返回值是字符的输出数一个int,不可以一个char []的字符本身(如我所希望的)。

HOWEVER, googling lead me to determine that the return value of printf is an int of the number of Char's output, not a char[] of the chars themselves (as I had hoped).

有另一种C函数,让我从我的变量建立字符串?

Is there another C function that lets me build strings from my variables?

推荐答案

的printf 写到标准输出。 的snprintf 完成你要的东西在这里。内插的字符串被存储在所述调用的snprintf后缓冲。您可能要定义你的缓冲区大小的小更智能,但是这仅仅是一个例子。

printf writes to standard output. snprintf accomplishes what you are going for here. The interpolated string is stored in 'buffer' after the call to snprintf. You may want define your buffer size a little more intelligently, but this is just an example.

char buffer[1024];
snprintf(buffer, sizeof(buffer), "Dealer's Card is %C %C", char1, char2);

这篇关于大厦字符串从C变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆