非常非常非常基本的问题 [英] Very very very basic question

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

问题描述

#include< stdio.h>

#include< stdlib.h>

#include< string.h>

int main(无效){


char input_string [50];


printf(" Please enter conversion:" ;);

scanf("%s",input_string);


printf("输出为%s \ n",input_string) ;


退出(0);

}

为什么这段代码会忽略空格后的任何输入文字?


例如如果我输入Hello World它只存储(并打印)你好。在

input_string。


对不起这个愚蠢的问题,这是我的第一个C程序。 comp.lang.ci中的

解决方案

读取:

scanf("%s" ,input_string);
为什么这段代码会忽略空格后的任何输入文字?




因为这就是它应该做的事情:


s匹配不是空格字符的字节序列。


-

a签名


认识我的人不需要我的名字 < no **************** @ usa.net>

在留言新闻中写道:m1 ************ comp@lang.ci中的*@usa.net ...

读取:

scanf("%s",input_string);

为什么这段代码会忽略空格后的任何输入文字?



因为这就是它应该做的事情:

s匹配不是空格字符的字节序列。




谢谢。


我正在寻找什么功能,然后接受任何长度的字符串

(无论是否有空格)?

Peter写道:

char input_string [50];

printf(" Please enter conversion:");
scanf ("%s",input_string);
为什么此代码会忽略空格后的任何输入文本?

如果我输入Hello World它只存储(并打印)你好。在
input_string。




在scanf()中使用%s将读取字符,直到它击中任何空格

字符。如果你想读取包含空格的整行,请使用fgets():


fgets(input_string,sizeof input_string,stdin);


请注意,fgets()会将\ n留在最后,假设有一个。


- 约翰


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

int main(void) {

char input_string[50];

printf("Please enter conversion: ");
scanf("%s", input_string);

printf("The output is %s\n", input_string);

exit(0);
}
Why does this code ignore any input text after a space?

e.g. If I enter "Hello World" it only stores (and prints) "Hello" in
input_string.

Sorry for the stupid question, it''s my first C program. :)

解决方案

in comp.lang.c i read:

scanf("%s", input_string); Why does this code ignore any input text after a space?



because that''s what it''s supposed to do:

s Matches a sequence of bytes that are not white-space characters.

--
a signature


"those who know me have no need of my name" <no****************@usa.net>
wrote in message news:m1*************@usa.net...

in comp.lang.c i read:

scanf("%s", input_string);


Why does this code ignore any input text after a space?



because that''s what it''s supposed to do:

s Matches a sequence of bytes that are not white-space characters.



Thanks.

What''s the function I''m looking for then to take in any length of string
(whether white space or not)?


Peter wrote:

char input_string[50];

printf("Please enter conversion: ");
scanf("%s", input_string); Why does this code ignore any input text after a space?

e.g. If I enter "Hello World" it only stores (and prints) "Hello" in
input_string.



Using %s in scanf() will read characters up until it hits any whitespace
character. If you want to read a whole line including spaces, use fgets():

fgets(input_string, sizeof input_string, stdin);

Mind that fgets() will leave the \n on the end, assuming there is one.

--John


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

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