差异o / p [英] diff o/p

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

问题描述



#include< stdio.h>

#include< stdlib.h>


int main(void)

{

int x; char ch;

char string [20] =" 100 5";


sscanf(string,"%3d%c",& x ,& ch);

printf(" \ n ...%d%c",x,ch);


返回EXIT_SUCCESS;

}


给o / p ... 100,那没关系,


但是

sscanf(字符串,"%3d%2c",& x,& ch);

printf(" \\\
...%d %c,x,ch);


给o / p ... 53,为什么.... ????


#include<stdio.h>
#include<stdlib.h>

int main(void)
{
int x; char ch;
char string[20] = "100 5";

sscanf(string,"%3d%c",&x,&ch);
printf("\n... %d %c",x,ch);

return EXIT_SUCCESS;
}

is giving o/p ... 100, that''s ok but,

but
sscanf(string,"%3d%2c",&x,&ch);
printf("\n... %d %c",x,ch);

is giving o/p ... 53 , why ....????

推荐答案

On Sun,2008年4月20日09:05:47 -0700(PDT), aa ** ***@gmail.com 写道:
On Sun, 20 Apr 2008 09:05:47 -0700 (PDT), aa*****@gmail.com wrote:

>
#include< stdio.h>

#include< stdlib.h>


int main(无效)

{

int x; char ch;

char string [20] =" 100 5";


sscanf(string,"%3d%c",& x ,& ch);


printf(" \ n ...%d%c",x,ch);


返回EXIT_SUCCESS;

}

给o / p ... 100,那没关系,但是,
>
#include<stdio.h>
#include<stdlib.h>

int main(void)
{
int x; char ch;
char string[20] = "100 5";

sscanf(string,"%3d%c",&x,&ch);
printf("\n... %d %c",x,ch);

return EXIT_SUCCESS;
}

is giving o/p ... 100, that''s ok but,



实际输出应为...... 100。 (注意两个尾随空白,

一个是硬编码的格式字符串,另一个是值

ch)。

The actual output should be "... 100 " (note the two trailing blanks,
one is hard coded in your format string and the other is the value of
ch).


>
但是

sscanf(字符串,"%3d%2c",& x,& ch);

printf(\ n ...%d%c,x,ch);

正在给o / p ... 53,为什么.... ????
>
but
sscanf(string,"%3d%2c",&x,&ch);
printf("\n... %d %c",x,ch);

is giving o/p ... 53 , why ....????



因为你调用了未定义的行为。


你的%2c承诺sscanf& ch指向至少一个数组2

char。可以?第一个字符(空白)进入任何地方

& ch points(显然是ch)。下一个字符(''5'')将

输入ch之后的下一个字节。你已经溢出了你的一个角色

缓冲区。


[O / T]你显然有一个小端的ASCII机器和x跟随

ch在内存中。扫描100后,x包含0x64 0x00 ... 0x00。

扫描空白后,ch包含0x20。扫描''5'后,x

包含0x35 0x00 ... 0x00。这是

字符5的ASCII表示和数字53的二进制表示。

删除电子邮件的del

Because you invoked undefined behavior.

Your %2c promises sscanf that &ch points to an array of at least 2
char. Does it? The first character (the blank) goes into wherever
&ch points (obviously into ch). The next character (the ''5'') goes
into the next byte beyond ch. You have overflowed your one character
buffer.

[O/T] You apparently have a little endian ASCII machine and x follows
ch in memory. After scanning the 100, x contains 0x64 0x00 ... 0x00.
After scanning the blank, ch contains 0x20. After scanning the ''5'', x
contains 0x35 0x00 ... 0x00. This is the ASCII representation of the
character 5 and the binary representation of the number 53.
Remove del for email


aa*****@gmail.com 写道:

>

#include< stdio.h>

#include< stdlib.h>


int main (无效){

int x; char ch;

char string [20] =" 100 5";


sscanf(string,"%3d%c",& x ,& ch);

printf(" \\\
...%d%c",x,ch);

返回EXIT_SUCCESS;

}


给o / p ... 100,那没关系,


但是

sscanf(字符串,"%3d%2c",& x,& ch);

printf(" \ n ...%d%c", x,ch);


给o / p ... 53,为什么...... ????
>
#include<stdio.h>
#include<stdlib.h>

int main(void) {
int x; char ch;
char string[20] = "100 5";

sscanf(string,"%3d%c",&x,&ch);
printf("\n... %d %c",x,ch);
return EXIT_SUCCESS;
}

is giving o/p ... 100, that''s ok but,

but
sscanf(string,"%3d%2c",&x,&ch);
printf("\n... %d %c",x,ch);

is giving o/p ... 53 , why ....????



为什么要丢弃sscanf()的返回值?捕获

他们并分析它们。


-

[邮件]:Chuck F(cinefalconer at maineline dot net)

[page]:< http://cbfalconer.home.att.net>

尝试下载部分。


**发自 http://www.teranews.com **

Why have you discarded the return values from sscanf()? Capture
them and analyze them.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.

** Posted from http://www.teranews.com **


4月21日凌晨4点06分,CBFalconer< cbfalco ... @ yahoo.comwrote:
On Apr 21, 4:06*am, CBFalconer <cbfalco...@yahoo.comwrote:

>

为什么要丢弃sscanf()的返回值? *捕获

他们并分析它们。
>
Why have you discarded the return values from sscanf()? *Capture
them and analyze them.



来自lcc-win32标准库,我有以下


sscanf函数返回宏EOF的值如果输入

在转换之前发生故障。否则,sscanf函数

返回分配的输入项数,如果早期匹配失败,则可以少于提供的b $ b,甚至为零br />

现在

printf(" \ n%d",sscanf(string,"%3d%2c",& x,& ch) ));赠送2,


所以我认为返回类型没有任何问题

sscanf

from lcc-win32 standard library , i have the following

the sscanf function returns the value of the macro EOF if an input
failure occurs before any conversion. otherwise, the sscanf function
returns the number of input items assigned,which can be fewer than
provided for, or even zero,in the event of an early matching failure

now
printf("\n %d",sscanf(string,"%3d%2c",&x,&ch)); is giving 2,

so I don''t think there is any thing wrong with the return type of
sscanf


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

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