C语言 如何修复“警告:多字符字符常量 [-Wmultichar]”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31064435/
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
How to fix "warning: multi-character character constant [-Wmultichar]"
提问by nb023
So basically the function searches a file for the '\t' (tab) character and when it finds it, its suppose to replace it with a '^I'
所以基本上该函数在文件中搜索 '\t'(制表符)字符,当它找到它时,它假设用 '^I' 替换它
Problem is c = '^I'produces a warning warning: multi-character character constant [-Wmultichar]. The other problem is only the I shows up not the ^.
问题是c = '^I'产生警告warning: multi-character character constant [-Wmultichar]。另一个问题是只有 I 出现而不是 ^。
If I change it to c = "^I"I get this warning "Warning: Assignment makes integer from pointer without a cast". This way also puts "\\" instead of ^I
如果我将其更改为c = "^I"我收到此警告“警告:赋值从指针生成整数而无需强制转换”。这种方式也把 "\\" 而不是 ^I
How can I solve this? Is there a better way to do it?
我该如何解决这个问题?有没有更好的方法来做到这一点?
if ((fileContents = fopen("fileContents.txt", "r")) == 0)
{
perror("fopen");
return 1;
}
while ((c = fgetc(fileContents)) != EOF)
{
if (c == '\t')
{
c = '^I';
}
putchar(c);
}
回答by Imobilis
You can not put two characters in a character literal.
'^I'here, there are two characters - ^and I.
This is responsible for the warning:
不能在字符文字中放入两个字符。
'^I'这里有两个字符 -^和I。这负责警告:
multi-character character constant
多字符字符常量
not sure what are you trying to achieve here, but in C you can only put single ASCII characteror character escape sequencein single quotes. the ^is system-specific ANSI escape code I think for terminal color, supported by DOS and UNIX. You can use it in a string as far as you have it declared somewhere with static or dynamic allocation or just use the printffunction to print it out to the standard output stream: printf("^I ^O test");
不知道你想在这里实现什么,但在 C 中你只能将单个ASCII 字符或字符转义序列放在单引号中。^我认为这是系统特定的 ANSI 转义码,用于终端颜色,受 DOS 和 UNIX 支持。您可以在字符串中使用它,只要您在静态或动态分配的某处声明它,或者仅使用该printf函数将其打印到标准输出流:printf("^I ^O test");
As for the implicit declaration. I do assume that cis declared as an integer or some other sort of arithmetic-type. You can not assign a string literal to it, not in any manner. You can do that with the functions strcpysprintfstrncpyetc. In that case, cmust have been statically or dynamically allocated. To avoid false implicit declaration make sure it is with the type of char*so an area of chars, where the last one is the null-terminating character. If you want to assign a string literal to a char*you will store it in read-only memory area.
至于隐式声明。我确实假设它c被声明为整数或其他某种算术类型。您不能以任何方式为其分配字符串文字。你可以用函数strcpysprintfstrncpy等来做到这一点。在这种情况下,c必须是静态或动态分配的。为避免错误的隐式声明,请确保它的类型为char*so 字符区域,其中最后一个是空终止字符。如果要将字符串文字分配给 a char*,则将其存储在只读内存区域中。
char *string;
string = "^I";
printf(string);
You can turn off warnings with the pragma warning directive. Don't do that. Warnings are there for a reason.
您可以使用 pragma 警告指令关闭警告。不要那样做。警告是有原因的。
回答by Ryan Haining
A charcan only be a single character. '^I'is two. In your case you'll want to if/else on the character, printing the string using a normal printf
Achar只能是一个字符。 '^I'是两个。在您的情况下,您需要在字符上使用 if/else,使用普通的printf
if (c == '\t') {
printf("^I");
} else {
putchar(c);
}
回答by o11c
'^I'is, quite obviously, two characters. You can't use putcharwith multiple characters, and as gcc warns you, multi-char constants are not something you want to use.
'^I'很明显,是两个字符。您不能使用putchar多个字符,并且正如 gcc 警告您的那样,您不想使用多字符常量。
Instead, make sure you use a string. Try something like:
相反,请确保使用字符串。尝试类似:
const char *replacement = NULL;
if (c == '\t') replacement = "^I";
if (replacement) fputs(replacement, stdout);
else putchar(c);

