C语言 使用 scanf 读取字符串作为输入
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35103745/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Read a string as an input using scanf
提问by Hafiz Temuri
I am new to C language and I am trying read a character and a string (a sentence; max-length 25) from a user.
我是 C 语言的新手,我正在尝试从用户那里读取一个字符和一个字符串(一个句子;最大长度为 25)。
Not sure what I am doing wrong in the following lines of code, its giving me an error "Segment Fault".
不确定我在以下代码行中做错了什么,它给了我一个错误“段错误”。
#include <stdio.h>
int main(){
char * str[25];
char car;
printf("Enter a character: ");
car = getchar();
printf("Enter a sentence: ");
scanf("%[^\n]s", &str);
printf("\nThe sentence is %s, and the character is %s\n", str, car);
return 0;
}
Thanks!
谢谢!
采纳答案by Edward Karak
stris an array of 25 pointers to char, not an array of char. So change its declaration to
str是一个包含 25 个指向 的指针char的数组,而不是char. 所以将其声明更改为
char str[25];
And you cannot use scanfto read sentences--it stops reading at the first whitespace, so use fgetsto read the sentence instead.
并且你不能scanf用来阅读句子——它在第一个空格处停止阅读,所以用fgets阅读句子代替。
And in your last printf, you need the %cspecifier to print characters, not %s.
You also need to flush the standard input, because there is a '\n'remaining in stdin, so you need to throw those characters out.
在你的最后printf,你需要说明%c符来打印字符,而不是%s. 您还需要刷新标准输入,因为还有'\n'剩余的 in stdin,因此您需要将这些字符扔掉。
The revised program is now
修改后的程序现在是
#include <stdio.h>
void flush();
int main()
{
char str[25], car;
printf("Enter a character\n");
car = getchar();
flush();
printf("Enter a sentence\n");
fgets(str, 25, stdin);
printf("\nThe sentence is %s, and the character is %c\n", str, car);
return 0;
}
void flush()
{
int c;
while ((c = getchar()) != '\n' && c != EOF)
;
}
回答by Spikatrix
You have to make four changes:
您必须进行四项更改:
Change
char * str[25];to
char str[25];as you want an array of 25
chars, not an array of 25 pointers tochar.Change
char car;to
int car;as
getchar()returns anint, not achar.Change
scanf("%[^\n]s", &str);to
scanf( "%24[^\n]", str);which tells
scanfto- Ignore all whitespace characters, if any.
- Scan a maximum of 24 characters (+1 for the Nul-terminator
'\0') or until a\nand store it instr.
Change
printf("\nThe sentence is %s, and the character is %s\n", str, car);to
printf("\nThe sentence is %s, and the character is %c\n", str, car);as the correct format specifier for a
charis%c, not%s.
改变
char * str[25];到
char str[25];因为你想要一个 25
char秒的数组,而不是一个 25 个指向char.改变
char car;到
int car;as
getchar()返回一个int,而不是一个char。改变
scanf("%[^\n]s", &str);到
scanf( "%24[^\n]", str);它告诉
scanf给- 忽略所有空白字符(如果有)。
- 最多扫描 24 个字符(Nul-terminator 为 +1
'\0')或直到 a\n并将其存储在str.
改变
printf("\nThe sentence is %s, and the character is %s\n", str, car);到
printf("\nThe sentence is %s, and the character is %c\n", str, car);因为 a 的正确格式说明符
char是%c,不是%s。
回答by Mihajlo Gajic
// This is minimal change to your code to work
// 这是对您的代码进行的最小更改以使其正常工作
#include <stdio.h>
int main(){
char car,str[25];
printf("Enter a character: ");
car = getchar();
printf("Enter a sentence: ");
scanf("%s", str);
printf("\nThe sentence is %s, and the character is %c\n", str, car);
return 0;
}

