输出/输入问题 [英] Output/Input Problem

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

问题描述



我正在linux机器上尝试一个非常简单的程序。代码如下:


int main {

char a,b;


printf("输入一个 ;);

scanf("%c",& a);

printf(" Enter b");

scanf("%c"& b);

printf(" b =%c \ n",b);

返回0;

}


当我在这里运行这是我得到的

输入:c

输入b :b =

它只允许我输入一个字符,一旦按下

,输入第二个提示就会随b =语句和
程序退出

我做错了什么,如何更正?

解决方案

scanf("%s"& a);


%s - 此开关允许您从控制台读取更多字符,如果

char a [..];声明是这样或作为指针。

你最好尝试阅读更多信息:尝试man scanf。


scanf("%s"& a);

如果我使用%s,这并不意味着我试图读取一个空的

终止字符串?我不是因为我只读了一个字符来自

stdin


在文章< 11 ***** *****************@z14g2000cwz.googlegroups .com> ;,

NO_Code< ka ****** @ gmail.com>写道:

我在linux机器上尝试一个非常简单的程序。下面给出的代码
int main {
char a,b;
printf(" Enter a");
scanf("%c",& a);
printf(" Enter b");
scanf("%c",& b);
printf(" b =%c \ nn",b) ;
返回0;
}
当我在这里运行这是我得到的
输入:c
输入b:b =
它只允许我只输入一个字符,一旦按下
输入第二个提示就会打印出b =语句和
程序退出
我做错了什么以及如何纠正这个?



scanf()与%c格式元素读取*完全*一个字符。你

输入两个字符,你想要的输入和换行符

终止该行。因此在第一次scanf()之后,你仍然在输入缓冲区中有

a字符等待第二个
scanf()读取。

你需要知道的另一部分:

- scanf()总是将缓冲区放在

读取之后;

- *除了*为%c和%[格式元素,scanf()跳过前导

空格...包括可能在缓冲区中碰巧是
的换行符;

- %c和%[格式元素不会跳过前导空格;

- %s格式元素在行尾之前不会读取:它

跳过领先的空白然后读取直到第一个空格...

可能恰好是下一个换行符,但也可能发生

仅仅是空格或制表符。

注意这里的含义,除非你使用%c或%[

,否则scanf()将忽略输入元素之间的行边界。 />
例如,scanf("%) d%d,& i,& j)非常乐意阅读以下任何内容:


10 11

10 11

[tab] 10 [tab] 11

[换行] [换行] [换行] 10 11

10 [空格] [空格] [空格] [换行符] [空格] [空格] [空格] [空格] 11

-

按日期排序的结果排序是近似值。


Hi,
I am trying a very simple program on a linux machine. Code given below

int main{
char a,b;

printf("Enter a ");
scanf("%c",&a);
printf("Enter b ");
scanf("%c",&b);
printf("b= %c\n",b);
return 0;
}

when i run this here is what i get
Enter a: c
Enter b: b=
It only allows me to enter just one character and as soon as I press
enter the second prompt is printed along with the b= statement and the
program exits
What am i doing wrong and how can i correct this?

解决方案

scanf("%s", &a);

%s - this switch allows you to read more characters from console if
char a[..]; is declared like that or as a pointer.
You better try to read more info: try "man scanf".


scanf("%s", &a);
If i use %s would this not mean that I am trying to read a null
terminated string? which i am not as i am reading only a character from
stdin


In article <11**********************@z14g2000cwz.googlegroups .com>,
NO_Code <ka******@gmail.com> wrote:

I am trying a very simple program on a linux machine. Code given below int main{
char a,b;
printf("Enter a ");
scanf("%c",&a);
printf("Enter b ");
scanf("%c",&b);
printf("b= %c\n",b);
return 0;
} when i run this here is what i get
Enter a: c
Enter b: b=
It only allows me to enter just one character and as soon as I press
enter the second prompt is printed along with the b= statement and the
program exits
What am i doing wrong and how can i correct this?



scanf() with a %c format element reads *exactly* one character. You
are entering two characters, the input you want and the newline to
terminate the line. Thus after the first scanf(), you still have
a character in the input buffer waiting to be read by the second
scanf().
The other part of what you need to know:
- scanf() always leaves the buffer positioned right after what was
read;
- *except* for %c and %[ format elements, scanf() skips leading
whitespace... including possibly any newline that happened to be
in the buffer;
- the %c and %[ format elements do NOT skip leading whitespace;
- the %s format element does NOT read until the end of line: it
skips leading whitespace and then reads until the first whitespace...
which might happen to be the next newline, but also might happen
to be merely a space or tab.
Notice the implication here, that unless you are using %c or %[
then scanf() will ignore line boundaries between input elements.
For example, scanf("%d%d", &i, &j) is perfectly happy to read any of these:

10 11
10 11
[tab]10[tab]11
[newline][newline][newline]10 11
10[space][space][space][newline][space][space][space][space]11
--
The ordering of results sorted by date is approximate.


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

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