C#2140 : Type error in argument 1 to 'scanf' ; expected 'const char * restrict' but found 'int'

标签 c scanf

我正在使用适用于 Windows 的 Pelles 编译器。 我有两个错误

#2168: Operands of '&' have incompatible types 'char *' and 'char *'.
#2140: Type error in argument 1 to 'scanf'; expected 'const char * restrict' but found 'int'.

我的代码看起来像

    #include <stdio.h>
    static char herp[20];

    int main()
    {
         int a;
         a = 2;
         printf("Some random number %d\n" ,a);
         scanf("Input: %c" &herp);
         getchar();
         return 0;
    }

scanf 似乎有很多问题,所以我不知道为什么。我对 C 很陌生,到目前为止我很喜欢它。我们将不胜感激。

最佳答案

scanf("Input: %c" &herp);

缺少一个逗号:

scanf("Input: %c", &herp);

由于 herp 是一个字符数组,因此您应该指定要写入的字符,例如

scanf("Input: %c", &herp[0]); // to write to the first character.

如果您输入字符串,则可以省略 &:

scanf("Input: %s", herp);

关于C#2140 : Type error in argument 1 to 'scanf' ; expected 'const char * restrict' but found 'int' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15714339/

相关文章:

c - 错误 'sscanf_result' 未声明(在此函数中首次使用)

C Scanf : What does a return value of -1 mean?

C: 为什么 "sscanf"不截尾字符?

c - 按位比较

c - 如果 '&' 没有放在 'scanf' 语句中会发生什么?

c - float 被视为双重

c - 是否可以定义自定义大小的位数组

c - 在不使用 getc 的情况下使用 fscanf 直到 EOF

我可以假设以较小的大小调用 realloc 将释放剩余部分吗?

c - 合并排序不适用于所有情况