错误:程序中有游离的……

本文详细介绍了在C代码中遇到的游离特殊字符问题及其原因,并提供了判断文件编码类型的方法及解决方案,包括使用enca工具转换文件编码。
中文下:
cc -c -I. test.c -o output/obj/test.o
test.c:1: 错误:程序中有游离的 \357’
test.c:1: 错误:程序中有游离的 \273’
test.c:1: 错误:程序中有游离的 \277’

英文下:
cc -c -I. test.c -o output/obj/test.o
test.c:1: error:stray \357’ in program
test.c:1: error:stray \273’ in program
test.c:1: error:stray \277’ in program

造成的原因主要有两个:
1.源代码(*.c / *.h)中使用了中文的标点符号(全角标点),如:逗号、分号、空格、加号、花括号。
2.源代码文件为 UTF-8 BOM 编码格式,如何判断文件是否是使用了 UTF-8 BOM 编码格式的呢?
$ cat test.c | hd -n 10
00000000  ef bb bf 2f 2a 0a 20 2a  20 e7                    |.../*. * .|
0000000a
输出的前三个码是:ef bb bf,表示该文件是UTF-8 BOM格式。
解决办法:
1.安装 enca
$ sudo apt-get install enca

2.将所有 *.c 文件由 UTF-8 BOM 编码格式转成 UCS-2
$ enca -L zh_CN -x ucs-2 *.c

3.再查看 test.c 文件的编码格式
$ cat test.c | hd -n 10
00000000  fe ff 00 2f 00 2a 00 0a  00 20                    |.../.*... |
0000000a
#include<sys/types.h> #include<sys/wait.h> #include<unistd.h> #include<stdio.h> main() { int child; if((child=fork())==0) { printf(“child’PID is %d\n”,getpid()); exit(0); } printf(“child’PID to return to parent is %d\n”,child); wait(0); } 5:1: 警告:返回类型默认为‘int’ [-Wimplicit-int] main() ^~~~ 3.c: 在函数‘main’中: 3.c:9:15: 错误程序中游离的‘\342’ { printf(���child’PID is %d\n”,getpid()); ^ 3.c:9:16: 错误程序中游离的‘\200’ { printf(���child’PID is %d\n”,getpid()); ^ 3.c:9:17: 错误程序中游离的‘\234’ { printf(���child’PID is %d\n”,getpid()); ^ 3.c:9:23: 错误程序中游离的‘\342’ { printf(“child���PID is %d\n”,getpid()); ^ 3.c:9:24: 错误程序中游离的‘\200’ { printf(“child���PID is %d\n”,getpid()); ^ 3.c:9:25: 错误程序中游离的‘\231’ { printf(“child���PID is %d\n”,getpid()); ^ 3.c:9:23: 错误:expected ‘)’ before ‘PID’ { printf(“child���PID is %d\n”,getpid()); ^ ~~~ ) 3.c:9:35: 错误程序中游离的‘\’ { printf(“child’PID is %d\n”,getpid()); ^ 3.c:9:37: 错误程序中游离的‘\342’ { printf(“child’PID is %d\n���,getpid()); ^ 3.c:9:38: 错误程序中游离的‘\200’ { printf(“child’PID is %d\n���,getpid()); ^ 3.c:9:39: 错误程序中游离的‘\235’ { printf(“child’PID is %d\n���,getpid()); ^ 3.c:9:18: 警告:传递‘printf’的第 1 个参数时将整数赋给指针,未作类型转换 [-Wint-conversion] { printf(“child’PID is %d\n”,getpid()); ^~~~~ In file included from 3.c:4: /usr/include/stdio.h:332:43: 附注:需要类型‘const char * restrict’,但实参的类型为‘int’ extern int printf (const char *__restrict __format, ...); ~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~ 3.c:10:9: 警告:隐式声明函数‘exit’ [-Wimplicit-function-declaration] exit(0); ^~~~ 3.c:10:9: 警告:隐式声明与内建函数‘exit’不兼容 3.c:10:9: 附注:include ‘<stdlib.h>’ or provide a declaration of ‘exit’ 3.c:5:1: +#include <stdlib.h> main() 3.c:10:9: exit(0); ^~~~ 3.c:12:13: 错误程序中游离的‘\342’ printf(���child’PID to return to parent is %d\n”,child); ^ 3.c:12:14: 错误程序中游离的‘\200’ printf(���child’PID to return to parent is %d\n”,child); ^ 3.c:12:15: 错误程序中游离的‘\234’ printf(���child’PID to return to parent is %d\n”,child); ^ 3.c:12:21: 错误程序中游离的‘\342’ printf(“child���PID to return to parent is %d\n”,child); ^ 3.c:12:22: 错误程序中游离的‘\200’ printf(“child���PID to return to parent is %d\n”,child); ^ 3.c:12:23: 错误程序中游离的‘\231’ printf(“child���PID to return to parent is %d\n”,child); ^ 3.c:12:21: 错误:expected ‘)’ before ‘PID’ printf(“child���PID to return to parent is %d\n”,child); ^ ~~~ ) 3.c:12:53: 错误程序中游离的‘\’ printf(“child’PID to return to parent is %d\n”,child); ^ 3.c:12:55: 错误程序中游离的‘\342’ printf(“child’PID to return to parent is %d\n���,child); ^ 3.c:12:56: 错误程序中游离的‘\200’ printf(“child’PID to return to parent is %d\n���,child); ^ 3.c:12:57: 错误程序中游离的‘\235’ printf(“child’PID to return to parent is %d\n���,child); ^ 3.c:12:16: 警告:传递‘printf’的第 1 个参数时将整数赋给指针,未作类型转换 [-Wint-conversion] printf(“child’PID to return to parent is %d\n”,child); ^~~~~ In file included from 3.c:4: /usr/include/stdio.h:332:43: 附注:需要类型‘const char * restrict’,但实参的类型为‘int’ extern int printf (const char *__restrict __format, ...); ~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~
最新发布
10-16
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值