简体   繁体   English

Scanf 更改 C 中的输入

[英]Scanf changing the input in C

I have a fairly basic problem but I can't manage to solve it.我有一个相当基本的问题,但我无法解决它。

I'm trying to get an input from a user like this:我正在尝试从这样的用户那里获得输入:

int main() {
    char coord[2];

    fflush(stdin);
    scanf("%c", coord);
}

When i'm trying this code with printf("%c", coord);当我用printf("%c", coord);尝试这段代码时, it displays a completely different string from what I typed. ,它显示的字符串与我输入的完全不同。 For example, if I type "g6", it prints "Ê".例如,如果我输入“g6”,它会打印“Ê”。 I really have no clue why it's happening.我真的不知道为什么会这样。

Thanks for helping me !谢谢你帮助我!

If you want to get string(char array) from user you should do this:如果你想从用户那里获取字符串(字符数组),你应该这样做:

scanf("%s",coord);

%c is for single char %c 用于单个字符

First of all avoid using fflush (stdin);首先避免使用fflush (stdin); . . Standard input flashing is undefined behavior, according to C standard, and may lead to big issues.根据 C 标准,标准输入闪烁是未定义的行为,可能会导致大问题。

Then, you are trying to get an input string using %c format, that is supposed to acquire a single character.然后,您尝试使用%c格式获取输入字符串,该格式应该获取单个字符。 Furthermore, your coord array has not enough room for the string terminator character ( \0 ).此外,您的coord数组没有足够的空间容纳字符串终止符\0 )。

The format to be used in order to acquire a string with scanf (and to print it with printf ) is %s :用于使用scanf获取字符串(并使用printf打印)的格式是%s

int main() {
    char coord[3] = {0};

    scanf("%2s", coord);

    printf ("%s\n", coord);
}

The "2" added to the format makes sure that at most two characters are read (exactly those you can have in you string array without overwriting the last character).添加到格式中的“2”确保最多读取两个字符(正是您可以在字符串数组中拥有的那些而不覆盖最后一个字符)。

For starters this statement对于初学者来说,这个声明

fflush(stdin);

has undefined behavior and shall be removed.具有未定义的行为,应被删除。

The conversion specifier %c of printf expects an argument of the type char while you are passing an expression of the type char * to which the array designator is implicitly converted printf 的转换说明符%c需要 char 类型的参数,而您正在传递数组指示符隐式转换为的 char * 类型的表达式

printf("%c", coord);

you have to write either你必须写

printf("%c", *coord);

or或者

printf("%c", coord[0]);

Pay attention to that using this call of scanf注意使用这个 scanf 调用

scanf("%c", coord);

you can enter only a single character.您只能输入一个字符。 You can not enter a string.不能输入字符串。

If you want to enter a string in the array coord that has only two elements then you have to write如果你想在数组坐标中输入一个只有两个元素的字符串,那么你必须写

scanf( "%1s", coord);

In this case the array will be filled with a string of length equal to 1.在这种情况下,数组将填充长度等于 1 的字符串。

In this case you can output it like在这种情况下,您可以像 output 一样

printf("%s", coord);

If you want to enter a string like this "g6" then you have to declare the array like如果你想输入一个像"g6"这样的字符串,那么你必须像这样声明数组

char coord[3];

and write the following call of scanf并编写以下 scanf 调用

scanf( "%2s", coord);

The line char coord[2];char coord[2]; declares coord as an array of characters (also known as a "string").coord声明为字符数组(也称为“字符串”)。 However, the %c (in both scanf and printf) reads/writes a single character.但是, %c (在scanfprintf)读取/写入单个字符。

For strings, you need to use the %s format.对于字符串,您需要使用%s格式。

Also, if you want to store/read/print the string, "g6" , you will need to allocate (at least) three characters to your coord array, as you must terminate all C-strings with a nul character .此外,如果您想存储/读取/打印字符串"g6" ,则需要为coord数组分配(至少)三个字符,因为您必须nul字符终止所有 C 字符串

Furthermore, calling fflush on the stdin stream is not effective (actually, it causes undefined behaviour , so anything could happen) - see here: I am not able to flush stdin .此外,在stdin上调用fflush无效(实际上,它会导致未定义的行为,因此任何事情都可能发生) - 请参见此处:我无法刷新标准输入

So, a 'quick fix' for your code would be something like this:因此,您的代码的“快速修复”将是这样的:

#include <stdio.h>
int main()
{
    char coord[3]; // Allow space for nul-terminator
//    fflush(stdin); // don't do it
    scanf("%2s", coord); // The "2" limits input to 2 characters
    printf("%s\n", coord);
    return 0; // ALways good practice to return zero (success) from main
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM