的sprintf [英] sprintf

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

问题描述

大家好,


我正在使用sprintf并在以下情况下获得starnge输出


char temp_rn [12];

memset(temp_rn,''\'',12);

sprintf(temp_rn," 0%s",temp_rn);


temp_rn中的最终值是00

它的行为如何,当源字符串和输出字符串是

相同时。

推荐答案

你好gyan

当你使用memeset时你正在填充temp_rn将为NULL

因此第一个字符字符串将是一个NULL characet或其他

字temp_rn变成一个空字符串。


现在当你使用sprintf时,你正在改变第一个字符

temp_rn为''0''(零)。因此,此时temp_rn具有值0。现在你

将temp_rm附加到自身,因此它变为00


希望清除它


Rohin Koul


gyan写道:
Hi gyan
When you are using memeset you are filling temp_rn will NULLs
Thus first character of the string will be a NULL characet or in other
words temp_rn becomes a empty string.

Now when you are using sprintf you are changing the first character of
temp_rn to ''0'' (zero). Thus at this point temp_rn has value "0" now you
append temp_rm to itself hence it becomes "00"

Hope that clears it

Rohin Koul

gyan wrote:

大家好,


我正在使用sprintf并在以下情况下获得starnge输出


char temp_rn [12];

memset(temp_rn,''\ 0'',12);

sprintf(temp_rn," 0%s",temp_rn);


temp_rn中的最终值是00

怎么样当源字符串和输出字符串相同时,表现就像这样。
Hi All,

I am using sprintf and getting starnge output in following case

char temp_rn[12];
memset(temp_rn,''\0'',12);
sprintf(temp_rn,"0%s",temp_rn);

the final value in temp_rn is 00
how it is behaving like this, when source string and output string is
same.


gyan< gy ******** @ tcs.comwrote:
gyan <gy********@tcs.comwrote:

大家好,


我正在使用sprintf并在以下情况下获得starnge输出


char temp_rn [12];

memset(temp_rn,''\'',12);

sprintf(temp_rn," 0%s",temp_rn);


temp_rn中的最终值是00

如果它的行为如此,当源字符串和输出字符串是

相同时。
Hi All,

I am using sprintf and getting starnge output in following case

char temp_rn[12];
memset(temp_rn,''\0'',12);
sprintf(temp_rn,"0%s",temp_rn);

the final value in temp_rn is 00
how it is behaving like this, when source string and output string is
same.



表现未定义

-

:wq

^ X ^ Cy ^ K ^ X ^ C ^ C ^ C ^ C

it is behaving undefined
--
:wq
^X^Cy^K^X^C^C^C^C


yeti< ro ******** @ gmail.comwrote:


(请不要发帖。)
yeti <ro********@gmail.comwrote:

(Please don''t top post.)

gyan写道:
gyan wrote:


char temp_rn [12];

memset(temp_rn,''\'',12);

sprintf(temp_rn," 0%s",temp_rn);
char temp_rn[12];
memset(temp_rn,''\0'',12);
sprintf(temp_rn,"0%s",temp_rn);


当你使用memeset时你正在填充temp_rn将为NULL
When you are using memeset you are filling temp_rn will NULLs



那是'NUL'; NULL是一个产生空指针常量的宏。

差别很微妙但非平凡。

That''s "NUL"; NULL is a macro which yields a null pointer constant.
The difference is subtle but non-trivial.


因此字符串的第一个字符将是一个NULL characet
Thus first character of the string will be a NULL characet



这将是正确的null character (注意大写)。

That would be correct as "null character" (note capitalization).


现在当你使用sprintf时,你将第一个字符

temp_rn改为''0''(零)。因此,此时temp_rn具有值0。现在你

将temp_rm附加到自身,因此它变为00。
Now when you are using sprintf you are changing the first character of
temp_rn to ''0'' (zero). Thus at this point temp_rn has value "0" now you
append temp_rm to itself hence it becomes "00"



编号n869的附件J.2描述了未定义的行为,其中包括


" snprintf,sprintf,sscanf ,vsnprintf,vsprintf,mbstowcs,

wcstombs,memcpy,strcpy,strncpy,strcat,strncat,strxfrm或

strftime函数,或<声明的任何函数。 wchar.h>

(除非另有说明),用于复制

重叠对象。


什么OP试图做错了,标准允许

实现做*任何*,其中包括OP

注意到的行为。

-

C. Benson Manica |我*应该*知道我在说什么 - 如果我

cbmanica(at)gmail.com |不,我需要知道。火焰欢迎。

No. Annex J.2 of n869 describes undefined behavior, which includes

"The snprintf, sprintf, sscanf, vsnprintf, vsprintf, mbstowcs,
wcstombs, memcpy, strcpy, strncpy, strcat, strncat, strxfrm, or
strftime function, or any of the functions declared by <wchar.h>
(except where otherwise specified), is used to copy between
overlapping objects."

What OP was trying to do is Wrong, and the Standard allows the
implementation to do *anything*, which includes the behavior OP
noticed.

--
C. Benson Manica | I *should* know what I''m talking about - if I
cbmanica(at)gmail.com | don''t, I need to know. Flames welcome.


这篇关于的sprintf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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