scanf(),ungetc()行为。 [英] scanf(), ungetc() behaviour.

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

问题描述

我一开始很好奇ungetc()如何将字符返回到

流,所以我做了以下编码。事情按预期工作,除非我将scanf(%c,& j)更改为scanf(%d,& j)。我不明白怎么可能

scanf()会影响i [0]和i [1]的内容。有人可以告诉我为什么吗?


#include< stdio.h>

#include< ctype.h>


void main()

{

char i [2],j;

printf(" Please input a two digit number:");

i [0] = getchar();

i [1] = getchar();

ungetc(我[ 1],stdin);

ungetc(i [0],stdin);

scanf("%c",& j); // scanf("%d"& j);

fflush(stdin);

printf(&\\ n \\ n \\ n \\ n \\ n \\ n \\ n \\ n \\ n \\ n \\ n \\ n \\ n \\ n \\ n% c在字符模式下为

i [0]",i [0],i [0]);

printf(" \ n您输入的小数为%d)

i [1]",i [1],i [1])的字符模式中的%c和%c;

printf(\ n \\ n您输入%d十进制和%c在字符模式下为j,j,j);


getchar();


}

I was curious at the start about how ungetc() returns the character to the
stream, so i did the following coding. Things work as expected except if I
change the scanf("%c",&j) to scanf("%d",&j). I don''t understand how could
scanf() affect the content of i[0] and i[1]. Can someone tell me why?

#include <stdio.h>
#include <ctype.h>

void main()
{
char i[2], j;
printf("Please input a two digit number:");
i[0]= getchar();
i[1]= getchar();
ungetc(i[1],stdin);
ungetc(i[0],stdin);
scanf("%c", &j); //scanf("%d",&j);
fflush(stdin);
printf("\nYou entered %d in decimal and %c in character mode for
i[0]",i[0],i[0]);
printf("\nYou entered %d in decimal and %c in character mode for
i[1]",i[1],i[1]);
printf("\nYou entered %d in decimal and %c in character mode for j",j,j);

getchar();

}

推荐答案

On Fri,2006年3月3日10:19:19 +0800,Argento < ar ******** @ hotmail.com>

在comp.lang.c中写道:
On Fri, 3 Mar 2006 10:19:19 +0800, "Argento" <ar********@hotmail.com>
wrote in comp.lang.c:
我一开始很好奇关于ungetc()如何将字符返回到
流,所以我做了以下编码。除非我将scanf(%c,& j)更改为scanf(%d,& j),否则事情按预期工作。我不明白怎么可能会影响i [0]和i [1]的内容。有人可以告诉我为什么吗?


你的程序有很多未定义行为的实例,那就是C标准不应该说它应该做什么。
什么都没有。 />
#include< stdio.h>
#include< ctype.h>

void main()


C标准要求在托管环境中使用返回类型

int定义main(),除非你有一个符合C99标准的编译器

特别是它接受的文档" void main()",我将

肯定会打赌你没有。这意味着你的程序是

undefined。

{
char i [2],j;
printf("请输入两位数字:");
我[0] = getchar();
我[1] = getchar();
ungetc(i [1],stdin);
ungetc (I [0],标准输入);


你还没有检查ungetc()函数的返回值,

特别是第二个。标准保证只有一个回击角色是

。第二个可能会失败。

scanf("%c",& j); //的scanf(QUOT;%d",&安培; j)的;


''j''是一个字符,大小为一个字节(8位或更多位)。

如果你传递scanf()a "%d"转换说明符和地址

''j'',你告诉scanf()在这个字节中写入一个int。在

你的平台int有多个字节,所以不适合''j''

只有一个字符的空间。当您使用%d时,您正在骗取scanf()

这导致了不确定的行为。

fflush(stdin);


fflush()函数没有为输入流定义,更多未定义

行为。

printf(" \你输入%d的十进制和%c的字符模式为
i [0],i [0],i [0]);
printf(" \ n你输入%d in字符模式中的十进制和%c为
i [1]",i [1],i [1]);
printf(\ n您输入%d的十进制和%c in j",j,j)的字符模式;

getchar();

}
I was curious at the start about how ungetc() returns the character to the
stream, so i did the following coding. Things work as expected except if I
change the scanf("%c",&j) to scanf("%d",&j). I don''t understand how could
scanf() affect the content of i[0] and i[1]. Can someone tell me why?
Your program has so many instances of undefined behavior that there is
nothing at all that the C standard has to say about what it should do.
#include <stdio.h>
#include <ctype.h>

void main()
The C standard requires that main() be defined with a return type of
int in a hosted environment, unless you have a C99 conforming compiler
that specifically documents that it accepts "void main()", and I will
most certainly bet that you do not. This means your program is
undefined.
{
char i[2], j;
printf("Please input a two digit number:");
i[0]= getchar();
i[1]= getchar();
ungetc(i[1],stdin);
ungetc(i[0],stdin);
You haven''t checked the return value of the ungetc() function,
particularly the second one. Only one character of push back is
guaranteed by the standard. The second one could have failed.
scanf("%c", &j); //scanf("%d",&j);
''j'' is a character and has a size of one byte (with 8 or more bits).
If you pass scanf() a "%d" conversion specifier and the address of
''j'', you are telling scanf() to write an int into that one byte. On
your platform int has more than one byte, and so won''t fit into ''j''
which only has room for a single character. You are lying to scanf()
when you use "%d" and this causes undefined behavior.
fflush(stdin);
The fflush() function is not defined for input streams, more undefined
behavior.
printf("\nYou entered %d in decimal and %c in character mode for
i[0]",i[0],i[0]);
printf("\nYou entered %d in decimal and %c in character mode for
i[1]",i[1],i[1]);
printf("\nYou entered %d in decimal and %c in character mode for j",j,j);

getchar();

}




修复程序中未定义的行为,然后提出问题,如果你对它的作用感到困惑,那么就会产生问题。


-

Jack Klein

主页: http://JK-Technology.Com

常见问题解答

comp.lang.c http ://c-faq.com/

comp.lang.c ++ http://www.parashift.com/c++-faq-lite/

alt.comp.lang.learn.c-c ++
http://www.contrib.andrew.cmu.edu/ ~a ... FAQ-acllc.html



Fix the undefined behavior in your program and then ask questions if
you are puzzled about what it does.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html


> C标准要求在托管环境中使用返回类型
> The C standard requires that main() be defined with a return type of
int定义main(),除非你有一个符合C99标准的编译器
它专门接受的文档void main (),我会
肯定会打赌你没有。这意味着您的程序未定义。
int in a hosted environment, unless you have a C99 conforming compiler
that specifically documents that it accepts "void main()", and I will
most certainly bet that you do not. This means your program is
undefined.




嗯,抱歉。我对标准知之甚少,但我正在使用DEV C ++和

,编译器很乐意编译它。

我只是对返回感到懒惰当我没有检查它时。



Well, sorry. I don''t know much about the standard but i am using DEV C++ and
the compiler compiles it happily.
I am just lazy with the ''return'' when i don''t check for it.

{
char i [2],j;
printf(请输入两位数字:);
我[0] = getchar();
我[1] = getchar();
ungetc(我[ 1],stdin);
ungetc(i [0],stdin);
{
char i[2], j;
printf("Please input a two digit number:");
i[0]= getchar();
i[1]= getchar();
ungetc(i[1],stdin);
ungetc(i[0],stdin);



你还没有检查ungetc()函数的返回值,



You haven''t checked the return value of the ungetc() function,
particularly the second one. Only one character of push back is
guaranteed by the standard. The second one could have failed.




好​​的,我在你的帖子后通过用以下内容替换两个ungetc语句

来检查这个。

if((ungetc(i [1],stdin))== i [1])printf(" First ungetc()success.\\\
);

if((ungetc(i [0],stdin))== i [0])printf(" Second ungetc()success.\ n);


看起来,我推回两个角色都没有问题。



Ok, i checked on this after your post by replacing the two ungetc statement
with the following.
if( (ungetc(i[1],stdin))== i[1] ) printf("First ungetc() success.\n");
if( (ungetc(i[0],stdin))== i[0] ) printf("Second ungetc() success.\n");

It seems like that, i have no problem with pushing back both characters.

scanf("%c",& j); // scanf("%d"& j);
scanf("%c", &j); //scanf("%d",&j);



''j''是一个字符,大小为一个字节(8位或更多位)。
如果你传递scanf()a%d转换说明符和
''j''的地址,你告诉scanf()在这个字节中写一个int。你的平台int有多个字节,所以不适合''j''
只有一个字符的空间。当你使用%d时,你正在撒谎scanf()
这导致了不明确的行为。



''j'' is a character and has a size of one byte (with 8 or more bits).
If you pass scanf() a "%d" conversion specifier and the address of
''j'', you are telling scanf() to write an int into that one byte. On
your platform int has more than one byte, and so won''t fit into ''j''
which only has room for a single character. You are lying to scanf()
when you use "%d" and this causes undefined behavior.




你是对的,我是粗心的。如果我将j改为''int''事情就解决了,

然而,更改i [0]和i [1]的内容仍然很奇怪。

也许scanf(''%d",& j)当j为''char''时会覆盖i [0]的内存?



You are right, I was careless. If I change j to ''int'' things are solve,
however, it''s still weird to change the content of i[0] and i[1].
Maybe scanf(''%d", &j) when j is ''char'' overwrite the memory of i[0]?

fflush(stdin);
fflush(stdin);



没有为输入流定义fflush()函数,更多未定义的行为。



The fflush() function is not defined for input streams, more undefined
behavior.




嗯,我只是继续看到使用fflush(stdin)的例子,我是

告诉你在scanf()之后和getchar()之前这样做
因为''\ n''不是由scanf(%d)或scanf(%c)获取的。这是我清除标准输出缓冲区的b
做法。

任何建议/替代方案都会受到赞赏。



Well, i just keep on seeing examples that uses fflush( stdin ) and i was
told to do so after a scanf() and before a getchar()
because the ''\n'' is not taken by scanf("%d") or scanf("%c"). That was my
practice of clearing the stdin''s buffer.
Any suggestion/alternative of doing so would be appreciated.


Argento写道:
Argento wrote:
我一开始很好奇ungetc()如何将字符返回到
流,所以我做了以下编码。除非我将scanf(%c,& j)更改为scanf(%d,& j),否则事情按预期工作。我不明白怎么可能会影响i [0]和i [1]的内容。有人可以告诉我原因吗?

#include< stdio.h>
#include< ctype.h>

void main()
I was curious at the start about how ungetc() returns the character to the
stream, so i did the following coding. Things work as expected except if I
change the scanf("%c",&j) to scanf("%d",&j). I don''t understand how could
scanf() affect the content of i[0] and i[1]. Can someone tell me why?

#include <stdio.h>
#include <ctype.h>

void main()



^^^^

不要费心去学习ungetc()如何工作直到你停止这个

荒谬的BullSchildtism。 br />


^^^^
Don''t bother with trying to learn how ungetc() works until you stop this
absurd BullSchildtism.


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

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