IIN将操作数验证为二进制%(具有.char*.and.int.)

IIN将操作数验证为二进制%(具有.char*.and.int.),c,C,我是C编程新手,所以这可能是一个愚蠢的问题……但我得到了以下错误: 45: error: invalid operands to binary % (have .char*. and .int.) 45: error: expected .). before string constant 第45行是第二个printf函数 一些信息: charsInWord和indexOf都返回一个int值 int main() { char word [20]; char c;

我是C编程新手,所以这可能是一个愚蠢的问题……但我得到了以下错误:

45: error: invalid operands to binary % (have .char*. and .int.)
45: error: expected .). before string constant
第45行是第二个printf函数

一些信息: charsInWord和indexOf都返回一个int值

int main() 
{ 
    char word [20]; 
    char c;

    printf("Enter a word and a character separated by a blank ");
    scanf("%s %c", word, &c);

    printf("\nInput word is "%c". contains %d input character. Index of %c in it is %d\n", word, charsInWord(word), c, indexOf(word, c));

    return 0;
}

您需要“转义”嵌入的引号。更改:

printf("\nInput word is "%c". contains %d input character. Index of %c in it is %d\n", word, charsInWord(word), c, indexOf(word, c));
致:


或者只使用单引号。

您需要“转义”嵌入的引号。更改:

printf("\nInput word is "%c". contains %d input character. Index of %c in it is %d\n", word, charsInWord(word), c, indexOf(word, c));
致:


或者只使用单引号。

您需要在格式字符串中转义引号

printf("\nInput word is "%c". contains %d input character. Index of %c in it is %d\n", word, charsInWord(word), c, indexOf(word, c));
应该是


您需要在格式字符串中转义引号

printf("\nInput word is "%c". contains %d input character. Index of %c in it is %d\n", word, charsInWord(word), c, indexOf(word, c));
应该是

首先,您需要避开这些引号,如下所示:

printf("\nInput word is \"%c\". contains %d input
接下来,确保格式字符串后面的参数类型与格式说明符预期的类型匹配。也就是说,如果指定
%c
,请确保相应的参数是
char
。如果指定
%f
,请确保相应的参数是浮点类型,依此类推

在本例中,您可能试图为第一个参数传递字符串(
word
),该参数已指定为字符(
%c
)。如果要传递
字符[]
字符*
(并确保字符串以null结尾),请使用
%s
格式说明符

首先,您需要避开这些引号,如下所示:

printf("\nInput word is \"%c\". contains %d input
接下来,确保格式字符串后面的参数类型与格式说明符预期的类型匹配。也就是说,如果指定
%c
,请确保相应的参数是
char
。如果指定
%f
,请确保相应的参数是浮点类型,依此类推

在本例中,您可能试图为第一个参数传递字符串(
word
),该参数已指定为字符(
%c
)。如果要传递
char[]
char*
(并确保字符串以null结尾),请使用
%s
格式说明符。

更改

"Input word is "%c". contains %d input character."

引号的转义是为了删除错误。从
%c
更改为
%s
是因为需要
%s
打印字符串。

更改

"Input word is "%c". contains %d input character."


引号的转义是为了删除错误。从
%c
更改为
%s
是因为您需要
%s
打印字符串。

使用现代GCC库,您可以消除%s溢出缓冲区和扰乱程序思维的风险。出于相关原因,检查scanf()的返回值也很重要。你也可以把长长的队伍稍微分开一点。该示例对示例中未定义的函数使用了一些标准库调用:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main() 
{ 
    char *word = 0;
    char c;

    printf("Enter a word and a character separated by a blank ");
    /* %ms is in GCC and the upcoming POSIX.1 standard */
    if(2 != scanf("%ms %c", &word, &c)) {
        fputs("oops, something went wrong in that input.\n", stderr);
        return -1;
    } 
    char *first = strchr(word, c);  /* NULL if not found */
    printf("\nInput word is \"%s\". contains %u input character. "
           "Index of %c in it is %ld\n",
           word, (unsigned int)strlen(word), c,
           first - word);  /* probably negative if it wasn't found */

    free(word);
    return 0;
}
#包括
#包括
#包括
int main()
{ 
char*word=0;
字符c;
printf(“输入用空格分隔的单词和字符”);
/*%ms在GCC和即将推出的POSIX.1标准中*/
如果(2!=scanf(“%ms%c”、&word、&c)){
fputs(“哎呀,输入出错了。\n”,stderr);
返回-1;
} 
char*first=strchr(word,c);/*如果未找到,则为NULL*/
printf(“\n输入字为\%s\”。包含%u个输入字符
“其中%c的索引为%ld\n”,
单词,(无符号整数)strlen(单词),c,
第一个单词);/*如果没有找到,可能是否定的*/
免费(字);
返回0;
}

处理未找到的字母是一项练习…:-)

使用现代化的GCC库,您可以消除%s溢出缓冲区和扰乱程序思维的风险。出于相关原因,检查scanf()的返回值也很重要。你也可以把长长的队伍稍微分开一点。该示例对示例中未定义的函数使用了一些标准库调用:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main() 
{ 
    char *word = 0;
    char c;

    printf("Enter a word and a character separated by a blank ");
    /* %ms is in GCC and the upcoming POSIX.1 standard */
    if(2 != scanf("%ms %c", &word, &c)) {
        fputs("oops, something went wrong in that input.\n", stderr);
        return -1;
    } 
    char *first = strchr(word, c);  /* NULL if not found */
    printf("\nInput word is \"%s\". contains %u input character. "
           "Index of %c in it is %ld\n",
           word, (unsigned int)strlen(word), c,
           first - word);  /* probably negative if it wasn't found */

    free(word);
    return 0;
}
#包括
#包括
#包括
int main()
{ 
char*word=0;
字符c;
printf(“输入用空格分隔的单词和字符”);
/*%ms在GCC和即将推出的POSIX.1标准中*/
如果(2!=scanf(“%ms%c”、&word、&c)){
fputs(“哎呀,输入出错了。\n”,stderr);
返回-1;
} 
char*first=strchr(word,c);/*如果未找到,则为NULL*/
printf(“\n输入字为\%s\”。包含%u个输入字符
“其中%c的索引为%ld\n”,
单词,(无符号整数)strlen(单词),c,
第一个单词);/*如果没有找到,可能是否定的*/
免费(字);
返回0;
}

处理未找到的字母是一项练习…:-)

%c.
您应该转义
,因为它在
printf
中。试试这个--
\%c\”
%c.
您应该转义
,因为它在
printf
中。试试这个--
\%c\”
谢谢!我刚刚删除了嵌入的引号,解决了这个问题,但是哇,还有另一个错误,我想我可以从这里处理它:Dthanks!我刚刚删除了嵌入的引号,解决了这个问题,但是哇,还有另一个错误,我想我可以从这里处理它:谢谢字符串帮助!这修复了我的另一个错误,现在我的程序可以运行了:)C与java如此不同,我通常习惯于对它进行编码,只是有些奇怪的事情我一直在C中忘记,比如在Cthanks中没有“字符串”之类的东西来获得字符串帮助!这修复了我的另一个错误,现在我的程序可以运行了:)C和java非常不同,我通常习惯于对它进行编码,只是有些奇怪的事情我一直在C中忘记,比如C中没有“字符串”之类的东西