Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
GCC编译错误:格式‘;%c’;应为类型为‘;字符*’;,但参数2的类型为‘;int’;[-Wformat]_C_Gcc_Char - Fatal编程技术网

GCC编译错误:格式‘;%c’;应为类型为‘;字符*’;,但参数2的类型为‘;int’;[-Wformat]

GCC编译错误:格式‘;%c’;应为类型为‘;字符*’;,但参数2的类型为‘;int’;[-Wformat],c,gcc,char,C,Gcc,Char,好的,我是C语言的noob,但我认为代码是基本的和直接的。此程序用于大学作业,并应具有“isdigit()”功能。这是密码 //by Nyxm #include <stdio.h> #include <ctype.h> main() { char userChar; int userNum, randNum; srand(clock()); printf("\nThis program will generate a random num

好的,我是C语言的noob,但我认为代码是基本的和直接的。此程序用于大学作业,并应具有“isdigit()”功能。这是密码

//by Nyxm
#include <stdio.h>
#include <ctype.h>

main()
{
    char userChar;
    int userNum, randNum;
    srand(clock());
    printf("\nThis program will generate a random number between 0 and 9 for a user to guess.\n");
    /*I changed it from '1 to 10' to '0 to 9' to be able to use the isdigit() function which
    will only let me use a 1 digit character for an argument*/
    printf("Please enter a digit from 0 to 9 as your guess: ");
    scanf("%c", userChar);
    if (isdigit(userChar))
    {
            userNum = userChar - '0';
            randNum = (rand() % 10);
            if (userNum == randNum)
            {
                    printf("Good guess! It was the random number.\n");
            }
            else
            {
                    printf("Sorry, the random number was %d.\n", randNum);
            }
    }
    else
    {
            printf("Sorry, you did not enter a digit between 0 and 9. Please try to run the program again.\$
    }
}
到底发生了什么事?我迫切需要帮助。任何帮助。我真的要放弃这个节目了。当我的教科书显示“%c”代表常规ole“char”时,为什么它说它需要参数“char*”?如果有什么不同的话,我正在使用nano、gcc和Ubuntu带有
scanf(“%c”和&userChar)

对于
scanf()
,您需要传递指向
char
的指针,否则它无法存储字符,因为所述
char
将按值传递。所以你需要
&userChar

假设在调用之前,
userChar
0
。对于您当前的代码,您基本上做到了这一点(就实用程序而言):

你想要的是:

scanf("%c", some-location-to-put-a-char);
这是
&userChar

scanf
man
页面提到:


您需要传入一个指针,而不是char的值

scanf("%c",&userChar);

gcc
为您的程序生成多个警告。你应该把它们都修好。例如,您缺少
#include
(对于
srand()
rand()
是必需的)和
#include
(对于
clock()
是必需的)。@KeithThompson在编译时,我不再有错误,程序现在可以按我所希望的方式运行。所以,我想我是在问它是否会继续工作?因为在我的教科书中,唯一需要的预处理器语句是“#include”,而“#include”用于isdigit()函数。在某些情况下,你可以不使用它们而逃之夭夭,但我不会详细说明;只需添加
#include
指令。如果您使用
-std=c99-pedantic
,gcc将对函数调用发出警告。你在用什么教科书?如果它显示了一个调用
rand()
而不调用
#include
,或者调用
clock()
而不调用
#include
,那么你的教科书是错误的。@KeithThompson给我的书是Michael A.Vine的《绝对初学者C编程第二版》。我注意到,每章末尾的一些练习都与高级主题有关,这些主题甚至还没有接近讨论的程度。例如,在第二章(关于主要数据类型)的末尾,一个练习是创建一个使用scanf()允许用户输入其名称的程序。这将是一个字符串,本章几乎没有涉及字符,更不用说字符串了。我应该在前面提到:
printf
scanf
格式不一样。对于
printf
%c
需要类型为
int
的参数,该参数应为字符值。对于
scanf
%c
需要指向
char
的指针。确保您正在阅读所使用函数的文档。很好。我真不敢相信我忘了“&”。伙计,我觉得自己像个白痴。将再次直接评论结果。效果完美。非常感谢你。我已经为这个项目奋斗了几天了。首先是字符到整数的转换,现在是这个。再一次,谢谢+1通过实际扩展值来解释传递值的好方法。
scanf("%c", some-location-to-put-a-char);
   c      Matches  a  sequence  of characters whose length is specified by
          the maximum field width (default 1); the next pointer must be  a
          pointer to char, and there must be enough room for all the char‐
          acters (no terminating null byte is added).  The usual  skip  of
          leading  white  space is suppressed.  To skip white space first,
          use an explicit space in the format.
scanf("%c",&userChar);