scanf问题 [英] scanf problem

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

问题描述

我知道这是大多数新手所遇到的问题。


#include< stdio.h>

int main(void)

{

char a;

scanf("%c",& a); / * 1st scanf * /

printf("%c\ n,a);

scanf("%c",& a); / * 2nd scanf * /

printf("%c\ n",a);

返回0;

}


这不起作用,因为在第一次扫描之后,在输入流中留下换行符

,未消耗。


我试图解决这个问题


#include< stdio.h>

int main(无效)

{

char a,b;

scanf("%c"& a);

printf("%c \\ \\ n,a);

getchar(); / * put getc(stdin)也有效* /

scanf("%c"& a);

printf("%c\ n" ,a);

返回0;

}


出于好奇我也试过了,


#include< stdio.h>

int main(无效)

{

char a,b;

scanf("%c",& a);

printf("%c\ n",a);

scanf (" \ n%c",& a);

printf("%c\ n",a);

返回0;

}


给了我预期的输出。


但我的问题是(终于!),为什么以下代码不工作,


#include< stdio.h>

int main(无效)

{

char a,b;

scanf("%c\ n",& a); / * 1st scanf * /

printf("%c\ n,a);

scanf("%c",& a); / * 2nd scanf * /

printf("%c\ n",a);

返回0;

}


我的假设是第一次scanf会消耗输入中剩下的换行符

流。

感谢您的时间,

Yugi。

I know this is the problem that most newbies get into.

#include<stdio.h>
int main(void)
{
char a;
scanf("%c",&a); /*1st scanf */
printf("%c\n",a);
scanf("%c",&a); /*2nd scanf*/
printf("%c\n",a);
return 0;
}

This will not work, because after the first scanf a newline is left
behind in the input stream, unconsumed.

I made this attempt to solve this problem

#include<stdio.h>
int main(void)
{
char a,b;
scanf("%c",&a);
printf("%c\n",a);
getchar(); /* putting getc(stdin) also worked*/
scanf("%c",&a);
printf("%c\n",a);
return 0;
}

Out of curiosity I tried this also,

#include<stdio.h>
int main(void)
{
char a,b;
scanf("%c",&a);
printf("%c\n",a);
scanf("\n%c",&a);
printf("%c\n",a);
return 0;
}

which gave me expected output.

But my question is (finally!) , why the following code doesn''t work,

#include<stdio.h>
int main(void)
{
char a,b;
scanf("%c\n",&a); /* 1st scanf */
printf("%c\n",a);
scanf("%c",&a); /* 2nd scanf */
printf("%c\n",a);
return 0;
}

My assumption was 1st scanf will consume the newline left in the input
stream.
Thanks for your time,
Yugi.

推荐答案

我使用fscanf读取文件并使用
消费换行符
MS-VC 6和gcc 3.3.5。你在用什么?

I use fscanf to read from a file and the newline is consumed, using
both MS-VC 6 and gcc 3.3.5. What are you using?


main()写道:
main() wrote:

我知道这是大多数新手进入的问题。


#include< stdio.h>

int main(无效)

{

char a;

scanf("%c",& a); / * 1st scanf * /

printf("%c\ n,a);

scanf("%c",& a); / * 2nd scanf * /

printf("%c\ n",a);

返回0;

}


这不起作用,因为在第一次扫描之后,在输入流中留下换行符

,未消耗。


我试图解决这个问题


#include< stdio.h>

int main(无效)

{

char a,b;

scanf("%c"& a);

printf("%c \\ \\ n,a);

getchar(); / * put getc(stdin)也有效* /

scanf("%c"& a);

printf("%c\ n" ,a);

返回0;

}
I know this is the problem that most newbies get into.

#include<stdio.h>
int main(void)
{
char a;
scanf("%c",&a); /*1st scanf */
printf("%c\n",a);
scanf("%c",&a); /*2nd scanf*/
printf("%c\n",a);
return 0;
}

This will not work, because after the first scanf a newline is left
behind in the input stream, unconsumed.

I made this attempt to solve this problem

#include<stdio.h>
int main(void)
{
char a,b;
scanf("%c",&a);
printf("%c\n",a);
getchar(); /* putting getc(stdin) also worked*/
scanf("%c",&a);
printf("%c\n",a);
return 0;
}



这只会起作用(你的方式似乎想要它)如果第一个输入行中的第二个

字符是换行符。你在这里介绍了一个新的

变量,b,你是不是想用它做点什么?

This will only work (the way you seem to want it to) if the second
character in the first input line is a newline. You introduced a new
variable here, b, did you mean to do something with it?


出于好奇,我也尝试了这个,


#include< stdio.h>

int main(无效)

{

char a,b;

scanf("%c"& a);

printf("%c\ n",a);

scanf(" \ n%c",& a);

printf("%c\ n",a);

返回0;

}


给了我预期的输出。
Out of curiosity I tried this also,

#include<stdio.h>
int main(void)
{
char a,b;
scanf("%c",&a);
printf("%c\n",a);
scanf("\n%c",&a);
printf("%c\n",a);
return 0;
}

which gave me expected output.



格式字符串中的任何空格都将匹配输入中任意数量的空格

,包括无。您的换行符将匹配任意数量的

空格,制表符,换行符等,直到遇到非空格字符


Any whitespace in the format string will match any amount of whitespace
in the input, including none. Your newline will match any number of
spaces, tabs, newlines, etc., until a non-whitespace character is
encountered.


但我的问题是(终于!),为什么以下代码不起作用,


#include< stdio.h>

int main(无效)

{

char a,b;

scanf("%c\ n,& ;一个); / * 1st scanf * /

printf("%c\ n,a);

scanf("%c",& a); / * 2nd scanf * /

printf("%c\ n",a);

返回0;

}


我的假设是第一次scanf将消耗输入

流中剩下的换行符。
But my question is (finally!) , why the following code doesn''t work,

#include<stdio.h>
int main(void)
{
char a,b;
scanf("%c\n",&a); /* 1st scanf */
printf("%c\n",a);
scanf("%c",&a); /* 2nd scanf */
printf("%c\n",a);
return 0;
}

My assumption was 1st scanf will consume the newline left in the input
stream.



第一个scanf调用会将第一个字符存储到a中,然后消耗

紧跟任何空格(包括换行符和任何

后面的空格)。你没有具体说明你使用的输入是什么,或者输出与你的期望有什么不同,所以很难帮你理解什么是"问题"是的,请随意

澄清。


另请参阅我对标题为不能使用char和

整数的主题的回复在同一个程序中 2006年8月10日在这个小组中。


Robert Gamble

The first scanf call will store the first character into a and consume
of any immediately following whitespace (including newlines and any
whitespace that follows). You didn''t specify what the input was that
you used or how the output differed from your expectations, so it''s
difficult to help you understand what the "problem" is, feel free to
clarify.

See also my response to the thread entitled "cannot use char and
integer in the same program" on August 10th 2006 in this group.

Robert Gamble




Robert Gamble写道:

Robert Gamble wrote:

main()写道:
main() wrote:

我知道这是大多数新手得到的问题进入。


#include< stdio.h>

int main(无效)

{

char a;

scanf("%c",& a); / * 1st scanf * /

printf("%c\ n,a);

scanf("%c",& a); / * 2nd scanf * /

printf("%c\ n",a);

返回0;

}


这不起作用,因为在第一次扫描之后,在输入流中留下换行符

,未消耗。


我试图解决这个问题


#include< stdio.h>

int main(无效)

{

char a,b;

scanf("%c"& a);

printf("%c \\ \\ n,a);

getchar(); / * put getc(stdin)也有效* /

scanf("%c"& a);

printf("%c\ n" ,a);

返回0;

}
I know this is the problem that most newbies get into.

#include<stdio.h>
int main(void)
{
char a;
scanf("%c",&a); /*1st scanf */
printf("%c\n",a);
scanf("%c",&a); /*2nd scanf*/
printf("%c\n",a);
return 0;
}

This will not work, because after the first scanf a newline is left
behind in the input stream, unconsumed.

I made this attempt to solve this problem

#include<stdio.h>
int main(void)
{
char a,b;
scanf("%c",&a);
printf("%c\n",a);
getchar(); /* putting getc(stdin) also worked*/
scanf("%c",&a);
printf("%c\n",a);
return 0;
}



这只会起作用(你的方式似乎想要它)如果第一个输入行中的第二个

字符是换行符。你在这里介绍了一个新的

变量,b,你是不是想用它做点什么?


This will only work (the way you seem to want it to) if the second
character in the first input line is a newline. You introduced a new
variable here, b, did you mean to do something with it?


出于好奇,我也尝试了这个,


#include< stdio.h>

int main(无效)

{

char a,b;

scanf("%c"& a);

printf("%c\ n",a);

scanf(" \ n%c",& a);

printf("%c\ n",a);

返回0;

}


给了我预期的输出。
Out of curiosity I tried this also,

#include<stdio.h>
int main(void)
{
char a,b;
scanf("%c",&a);
printf("%c\n",a);
scanf("\n%c",&a);
printf("%c\n",a);
return 0;
}

which gave me expected output.



格式字符串中的任何空格都将匹配输入中任意数量的空格

,包括无。您的换行符将匹配任意数量的

空格,制表符,换行符等,直到遇到非空格字符



Any whitespace in the format string will match any amount of whitespace
in the input, including none. Your newline will match any number of
spaces, tabs, newlines, etc., until a non-whitespace character is
encountered.


但我的问题是(终于!),为什么以下代码不起作用,


#include< stdio.h>

int main(无效)

{

char a,b;

scanf("%c\ n,& ;一个); / * 1st scanf * /

printf("%c\ n,a);

scanf("%c",& a); / * 2nd scanf * /

printf("%c\ n",a);

返回0;

}


我的假设是第一次scanf将消耗输入

流中剩下的换行符。
But my question is (finally!) , why the following code doesn''t work,

#include<stdio.h>
int main(void)
{
char a,b;
scanf("%c\n",&a); /* 1st scanf */
printf("%c\n",a);
scanf("%c",&a); /* 2nd scanf */
printf("%c\n",a);
return 0;
}

My assumption was 1st scanf will consume the newline left in the input
stream.



第一个scanf调用会将第一个字符存储到a中,然后消耗

后面的任何空格(包括换行符和任何

后面的空格)。你没有具体说明你使用的输入是什么,或者输出与你的期望有什么不同,所以很难帮你理解什么是"问题"是的,随意

澄清。


The first scanf call will store the first character into a and consume
of any immediately following whitespace (including newlines and any
whitespace that follows). You didn''t specify what the input was that
you used or how the output differed from your expectations, so it''s
difficult to help you understand what the "problem" is, feel free to
clarify.






的最后一段代码片段

这是我得到的输出

$ b $

z

y

z


当我输入y时,它没有打印任何东西

当我输入z时,它打印了y和z。

我期待的是这个,
$ b $由

y
z

z


感谢您的时间,

Yugi。

Hi,

for the last code snippet
this is the output i got

y
z
y
z

when i entered y , it didn''t print anything
when i entered z, it printed both y and z.
What i expected is this,

y
y
z
z

Thanks for your time,
Yugi.


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

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