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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 10:19:04  来源:igfitidea点击:

Read a string as an input using scanf

cscanf

提问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:

您必须进行四项更改:

  1. Change

    char * str[25];
    

    to

    char str[25];
    

    as you want an array of 25 chars, not an array of 25 pointers to char.

  2. Change

    char car;
    

    to

    int car;
    

    as getchar()returns an int, not a char.

  3. Change

    scanf("%[^\n]s", &str);
    

    to

    scanf( "%24[^\n]", str);
    

    which tells scanfto

    1. Ignore all whitespace characters, if any.
    2. Scan a maximum of 24 characters (+1 for the Nul-terminator '\0') or until a \nand store it in str.
  4. 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.

  1. 改变

    char * str[25];
    

    char str[25];
    

    因为你想要一个 25char秒的数组,而不是一个 25 个指向char.

  2. 改变

    char car;
    

    int car;
    

    asgetchar()返回一个int,而不是一个char

  3. 改变

    scanf("%[^\n]s", &str);
    

    scanf( "%24[^\n]", str);
    

    它告诉scanf

    1. 忽略所有空白字符(如果有)。
    2. 最多扫描 24 个字符(Nul-terminator 为 +1 '\0')或直到 a\n并将其存储在str.
  4. 改变

    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;
}