好吧,我正在为类编写代码,我认为一切正常,除了我在 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.
最佳答案
查看 (“你的机器是 Little Endian 机器\n”);
。注意“弯曲引号”:这些显然不是 ASCII 引号(看起来像这样:"
)。您必须将它们替换为“直引号”。(这也适用于所有其他字符串).
不要在任何不是合适的文本 编辑器中编辑代码。特别是,不要编辑代码,例如MS Word、写字板或富文本编辑器,因为您可能会遇到类似这样的有趣问题。
关于c - 在文字和标识符之外不允许使用非 ASCII 字符(C 编程),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22058133/