【C语言进阶剖析】23、#error 和 #line 使用分析


本篇博客介绍两个和预处理器相关的指示字,#error 和 #line,这两个预处理指示字在现在的软件产品中已经用的比较少了,但是作为 C 语言一个比较重要的知识点,还是有必要掌握。

1 #error 的用法

  • #error 用于生成一个编译错误消息

到目前为止,和编译相关的错误都是编译器给出的,那么有没有必要自己定义一个 error,在编译时提示编译错误呢,既然 C 语言给出了这种用法,自然是有用的,我们先看看怎么用。

用法如下:
在这里插入图片描述
#error 编译指示字用于自定义程序员特有的编译错误消息,类似的,#warning 用于生成编译警告

编译错误不会继续编译,无法生成可执行程序,编译警告只是给出警告,不会阻拦编译,编译还会继续执行,会生成可执行文件。

  • #error 是一种预编译指示字
  • #error 可用于提示编译条件是否满足

下面看一个例子
在这里插入图片描述
__cplusplus 是C++ 中特有的一个宏,假设我们写了一个 C++ 代码,不小心用 C 语言编译器编译这段代码,结果肯定会有很多莫名其妙的问题,完全无里头,但是如果我们使用了 #error 生成一个编译错误消息,我们马上就能意识到是编译器用错了,下面就来尝试一下

// 23-1.c
#include <stdio.h>
#ifndef __cplusplus
    #error This file should be processed with C++ compiler.
#endif
class CppClass
{
private:
    int m_value;
public:
    CppClass()
    { 
    }
    ~CppClass()
    {
    }
};
int main()
{
    return 0;
}

上面是一个 C++ 代码,我们我们用 gcc 编译器编译,由于找不到宏 __cplusplus,就会生成一条编译错误消息,应该使用 C++ 编译器编译。编译结果如下:

$ gcc 23-1.c -o 23-1
23-1.c:4:6: error: #error This file should be processed with C++ compiler.
     #error This file should be processed with C++ compiler.
      ^~~~~
23-1.c:6:1: error: unknown type name ‘class’
 class CppClass
 ^~~~~
23-1.c:7:1: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
 {
 ^

我们注释第 3 行到第 5 行,再次编译如下,不再提示我们编译器使用错误

$ gcc 23-1.c -o 23-1
23-1.c:6:1: error: unknown type name ‘class’
 class CppClass
 ^~~~~
23-1.c:7:1: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
 {
 ^

2 编程实验

上面认识了 #error,下面我们就来看一个实际工程开发中会遇到的问题。直接看代码

// 23-2.c
#include<stdio.h>
void f()
{
#if (PRODUCT == 1)
    printf("This is a low level product!\n");
#elif (PRODUCT == 2)
    printf("This is a middle level product!\n");
#elif (PRODUCT == 3)
    printf("This is a high level product!\n");
#endif
}
int main()
{
    f();
    printf("1. Query Information.\n");
    printf("2. Record Information\n");
    printf("3. Delete Information.\n");
#if (PRODUCT == 1)
    printf("4. Exit.\n");
#elif (PRODUCT == 2)
    printf("4. High Level Query.\n");
    printf("5. Exit.\n");
#elif (PRODUCT == 3)
    printf("4. HIgh Level Query");
    printf("5. Manual Service.\n");
    printf("6. Exit.\n");
#endif
    return 0;
}

函数 f() 用预编译来判断软件版本,如果宏 PRODUCT 等于 1 为低端版本,等于 2 为中端产品,等于 3 为高端产品。
main 函数中用宏 PRODUCT 来决定不同版本软件的功能,除了基本的三个功能,低端产品有退出功能;中端产品有高级查询,退出功能;高端产品有高端查询,人工服务,退出功能。

我们来编译运行一下,结果如下:

$ gcc 23-2.c -o 23-2
skx@ubuntu:~/c$ ./23-2
1. Query Information.
2. Record Information
3. Delete Information.

这是什么版本的产品,什么都不是呀,连退出功能都没有,这软件根本没法用呀,什么原因呢,原来是我们忘记定义宏 PRODUCT,下面在命令行定义宏,再次编译运行。结果如下:

$ gcc 23-2.c -DPRODUCT=2 -o 23-2
$ ./23-2
This is a middle level product!
1. Query Information.
2. Record Information
3. Delete Information.
4. High Level Query.
5. Exit.

从结果可以看到,这是一个中端产品。

从上面的过程可以看出,如果忘记定义宏 PRODUCT,编译依然可以通过,但是编译出来的产品是有问题的,有什么办法可以避免这个问题呢,就是使用 #error,修改的代码如下:

// 23-2.c
#include<stdio.h>
void f()
{
#if (PRODUCT == 1)
    printf("This is a low level product!\n");
#elif (PRODUCT == 2)
    printf("This is a middle level product!\n");
#elif (PRODUCT == 3)
    printf("This is a high level product!\n");
#else
    #error The "PRODUCT" is not define!				// 增加 #error
#endif
}
int main()
{
    f();
    printf("1. Query Information.\n");
    printf("2. Record Information\n");
    printf("3. Delete Information.\n");
#if (PRODUCT == 1)
    printf("4. Exit.\n");
#elif (PRODUCT == 2)
    printf("4. High Level Query.\n");
    printf("5. Exit.\n");
#elif (PRODUCT == 3)
    printf("4. HIgh Level Query");
    printf("5. Manual Service.\n");
    printf("6. Exit.\n");
#else
    #error The "PRODUCT" is not define!				// 增加 #error
#endif
    return 0;
}

在第11、12 和第 30、31 行增加了#error,如果忘记定义宏 PRODUCT,将给出编译错误的提示,我们来试试

$ gcc 23-2.c -o 23-2
23-2.c: In function ‘f’:
23-2.c:12:6: error: #error The "PRODUCT" is not define!
     #error The "PRODUCT" is not define!
      ^~~~~
23-2.c: In function ‘main’:
23-2.c:31:6: error: #error The "PRODUCT" is not define!
     #error The "PRODUCT" is not define!
      ^~~~~

果然不定义宏 PRODUCT,不能通过编译,必须定义才能通过编译,下面再编译出一个低端版本。

$ gcc 23-2.c -DPRODUCT=1 -o 23-2
skx@ubuntu:~/c$ ./23-2
This is a low level product!
1. Query Information.
2. Record Information
3. Delete Information.
4. Exit.

类似的我们我们把第 12 行,第 31 行的 error 改成 warning,不定义宏 PRODUCT,将给出警告,但是依然可以生成可执行文件,下面我们试试,结果如下:

$ gcc 23-2.c -o 23-2
23-2.c: In function ‘f’:
23-2.c:12:6: warning: #warning The "PRODUCT" is not define! [-Wcpp]
     #warning The "PRODUCT" is not define!
      ^~~~~~~
23-2.c: In function ‘main’:
23-2.c:31:6: warning: #warning The "PRODUCT" is not define! [-Wcpp]
     #warning The "PRODUCT" is not define!
      ^~~~~~~
$ ./23-2
1. Query Information.
2. Record Information
3. Delete Information.

3 #line 的用法

#line 在现在的软件工程中已经用的非常少了,但是作为一个知识点,我们还是了解一下吧。

  • #line 用于强制指定新的行号和编译文件名,并对源程序的代码重新编号

用法如下:
在这里插入图片描述
下面看段简短的代码有个直观的感受。

// 23-3.c
#include<stdio.h>
int main()
{
    printf("file:%s, line:%d\n", __FILE__,__LINE__);
    #line 1 "skx.c"
    printf("file:%s, line:%d\n", __FILE__,__LINE__);
    return 0;
}

程序只有两行打印语句,分别打印文件名和当前行号,编译,运行结果如下:

$ gcc 23-3.c -o 23-3
$ ./23-3
file:23-3.c, line:5
file:skx.c, line:1

从第二行打印语句开始,行号从 1 开始重新编码,文件名也变成了 skx.c。其实就是修改了文件名,让行号重新开始编码,这有什么用呢?在 C 语言诞生之初,软件规模还没有这么大,更喜欢将所有的代码都放到一个文件中,一个软件是由不同的程序员开发的,出了问题谁来维护呢,当然是谁开发的谁来维护了,怎么确定这段代码是谁开发的呢,#line 的作用也就来了。

通过一个案例感受一下:一个程序是由三个人开发的,最后放到一起,为了表现错误,这里将 printf 语句的分号省略,代码如下:

// 23-3.c
#include<stdio.h>
// The code section is written by A.
// Begin
#line 1 "a.c"
// End

// The code section is written by B.
// Begin
#line 1 "b.c"
// End

// The code section is written by skx.
// Begin
#line 1 "skx.c"
int main()
{
    printf("file:%s, line:%d\n", __FILE__,__LINE__)
    return 0;
}
// End

编译结果如下:文件 skx.c 中在 return 语句之前省略了分号,我们也就很容易的定位到了错误,对应的开发人员需要去修改代码。

$ gcc 23-3.c -o 23-3
skx.c: In function ‘main’:
skx.c:4:5: error: expected ‘;’ before ‘return’

4 小结

1、#error 用于自定义一条编译错误信息
2、#warning 用于自定义一条编译警告信息
3、#error 和 #warning 常用于条件编译的情况
4、#line 用于强制指定新的行号和变异文件名

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. 创建lex文件,命名为c_lex.l,输入以下内容: %{ #include "c_yacc.tab.h" %} %% "int" { return INT; } "float" { return FLOAT; } "char" { return CHAR; } "if" { return IF; } "else" { return ELSE; } "while" { return WHILE; } "for" { return FOR; } "return" { return RETURN; } [0-9]+ { yylval.num = atoi(yytext); return NUM; } [a-zA-Z_][a-zA-Z0-9_]* { yylval.id = strdup(yytext); return ID; } ";" { return SEMICOLON; } "," { return COMMA; } "(" { return LEFTPAREN; } ")" { return RIGHTPAREN; } "{" { return LEFTBRACE; } "}" { return RIGHTBRACE; } "==" { return EQ; } ">" { return GT; } ">=" { return GE; } "<" { return LT; } "<=" { return LE; } "!=" { return NE; } "=" { return ASSIGN; } "//" { while(yyinput() != '\n'); } "/*" { while(yyinput() != '*' || yyinput() != '/') yyinput(); } [ \t\n] { } . { printf("Error: unknown character %c\n", *yytext); } %% int yyinput(void) { return fgetc(yyin); } 2. 创建yacc文件,命名为c_yacc.y,输入以下内容: %{ #include <stdio.h> #include <stdlib.h> #include <string.h> int yylex(); void yyerror(char *error); int yyparse(); extern FILE *yyin; extern int yylineno; extern char *yytext; %} %token INT FLOAT CHAR IF ELSE WHILE FOR RETURN %token NUM ID %token SEMICOLON COMMA LEFTPAREN RIGHTPAREN LEFTBRACE RIGHTBRACE %token EQ GT GE LT LE NE ASSIGN %start program %% program: statement ; statement: declaration | assignment | if_statement | while_statement | for_statement | return_statement | SEMICOLON ; declaration: type ID SEMICOLON ; assignment: ID ASSIGN expression SEMICOLON ; if_statement: IF LEFTPAREN expression RIGHTPAREN LEFTBRACE statement RIGHTBRACE | IF LEFTPAREN expression RIGHTPAREN LEFTBRACE statement RIGHTBRACE ELSE LEFTBRACE statement RIGHTBRACE ; while_statement: WHILE LEFTPAREN expression RIGHTPAREN LEFTBRACE statement RIGHTBRACE ; for_statement: FOR LEFTPAREN expression SEMICOLON expression SEMICOLON expression RIGHTPAREN LEFTBRACE statement RIGHTBRACE ; return_statement: RETURN expression SEMICOLON ; type: INT | FLOAT | CHAR ; expression: expression EQ expression | expression GT expression | expression GE expression | expression LT expression | expression LE expression | expression NE expression | expression '+' expression | expression '-' expression | expression '*' expression | expression '/' expression | LEFTPAREN expression RIGHTPAREN | ID | NUM ; %% void yyerror(char *error) { printf("Error: %s at line %d\n", error, yylineno); } int main(int argc, char *argv[]) { if(argc < 2) { printf("Usage: %s <filename>\n", argv[0]); return 1; } FILE *fp = fopen(argv[1], "r"); if(!fp) { printf("Error: cannot open file %s\n", argv[1]); return 1; } yyin = fp; yyparse(); fclose(fp); return 0; } 3. 编译生成可执行文件,输入以下命令: lex c_lex.l yacc -d c_yacc.y gcc lex.yy.c y.tab.c -o c_parser 4. 执行可执行文件,并输入要分析C语言代码文件名,例如: ./c_parser test.c 其中test.c为要分析C语言代码文件。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值