好吧所以我正在为课程编写代码而且我认为一切都是正确的,除非我在printf语句中遇到错误我不知道如何做c代码而我的老师让我们自学。我收到一个未声明的标识符错误以及printf语句中的非ASCII字符错误有人可以帮我弄清楚为什么我会收到这些错误?我只是希望他们逐字逐句地打印出这个陈述,那么为什么要把它看成不同的东西?
#include <inttypes.h>
#include <stdio.h>
typedef enum{false, true} bool;
bool is_little_endian()
{
int x = 1;
char *y = (char*)&x;
return 1;
}
unsigned int merge_bytes( unsigned int x, unsigned int y )
{
return (y & 0xffffff00) | (x & 0xff);
}
unsigned int replace_byte (unsigned int x, int i, unsigned char b)
{
int shift = (b << (8 * i));
int mask = 0xff << shift;
return (~mask & x) | shift;
}
int main()
{
if( is_little_endian() )
{
printf(“Your machine is a Little Endian machine\n”);
}
int x = 0x89ABCDEF;
int y = 0x76543210;
printf(“Merged number = 0x%x\n”, merge_bytes(x,y));
unsigned char z= 0x22;
printf(“Replaced number = 0x%x\n”, replace_byte(x,3,z));
return 0;
}
这是我得到的错误
HW3.c:30:8: error: non-ASCII characters are not allowed outside of literals and
identifiers
printf(“Your machine is a Little Endian machine\n”);
^
HW3.c:30:11: error: use of undeclared identifier 'Your'
printf(“Your machine is a Little Endian machine\n”);
^
HW3.c:30:52: error: non-ASCII characters are not allowed outside of literals and
identifiers
printf(“Your machine is a Little Endian machine\n”);
^
HW3.c:35:8: error: non-ASCII characters are not allowed outside of literals and
identifiers
printf(“Merged number = 0x%x\n”, merge_bytes(x,y));
^
HW3.c:35:11: error: use of undeclared identifier 'Merged'
printf(“Merged number = 0x%x\n”, merge_bytes(x,y));
^
HW3.c:35:33: error: non-ASCII characters are not allowed outside of literals and
identifiers
printf(“Merged number = 0x%x\n”, merge_bytes(x,y));
^
HW3.c:37:8: error: non-ASCII characters are not allowed outside of literals and
identifiers
printf(“Replaced number = 0x%x\n”, replace_byte(x,3,z));
^
HW3.c:37:11: error: use of undeclared identifier 'Replaced'
printf(“Replaced number = 0x%x\n”, replace_byte(x,3,z));
^
HW3.c:37:35: error: non-ASCII characters are not allowed outside of literals and
identifiers
printf(“Replaced number = 0x%x\n”, replace_byte(x,3,z));
^
9 errors generated.
答案 0 :(得分:15)
看看(“Your machine is a Little Endian machine\n”);
。请注意“曲线引号”:这些显然不是ASCII引号(如下所示:"
)。你必须用“直引号”替换它们。 (这也适用于所有其他字符串)。
不要在任何不适当的文本编辑器中编辑代码。特别是,不要在例如编辑代码MS Word,写字板或富文本编辑器,因为你可能会遇到这样有趣的问题。
答案 1 :(得分:4)
如果您进行复制和粘贴,则可能会遇到这些问题。 答案是删除所有@,“等标志并使用键盘重做它们。希望这会有所帮助。
答案 2 :(得分:0)
关键是你正在使用类似MS Word的文本符号,这些在c或其他编程语言中是不允许的,你知道C是大小写敏感的。例如当你阅读关于c编程的pdf文档时你会复制一些源代码,你将它们粘贴在文本编辑器或编译器中,你将主要得到这些错误。
ex:char s3 [100] = {&#39; a&#39;,&#39;&#39;,&#39; \ 0&#39;,&#39; d&#39;}; //正确
char s3 [] = {'S','t','u','d','i','\ 0','e','r','e','r'} ; //不允许
因此,当您重写代码而不是复制它们并粘贴到编辑器中时,它会更好。