#error、#warning和#line

#error的用法

#error用于生成一个编译错误消息,并停止编译

用法          

#error  message

注:message不需要用双引号包围

#error编译指示字用于自定义程序员特有的编译错误消息

类似的,#warning用于生成编译警告,但不会停止编译


例1:

#include <stdio.h>

#define CONST_NAME1 "CONST_NAME1"
#define CONST_NAME2 "CONST_NAME2"

int main()
{  
    #ifndef COMMAND
    #warning Compilation will be stoped ...
    #error No defined Constant Symbol COMMAND
    #endif

    printf("%s\n", COMMAND);
    printf("%s\n", CONST_NAME1);
    printf("%s\n", CONST_NAME2);

    return 0;
}
编译报错,会显示我们自己定义的#warning和#error信息


#line的用法:

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

用法

#line  number  filename

   注:filename可省略

#line编译指示字的本质是重定义内置宏__LINE__和__FILE__

例2:

test.c

#include <stdio.h>

#line 14 "Hello.c"  //告诉编译器,现在编译的文件名改为Hello.c了,然后从这一行的下一行开始是新指示的行,第4行变成了第14行,往后按新的行类推

#define CONST_NAME1 "CONST_NAME1"
#define CONST_NAME2 "CONST_NAME2"

void f()
{
    return 0;   //此处有警告,本来这一行是第10行,但是经过前面#line后,变成第20行了,所以警告信息会说在第20行
}

int main()
{
    printf("%s\n", CONST_NAME1);
    printf("%s\n", CONST_NAME2);
    printf("%d\n", __LINE__);
    printf("%s\n", __FILE__);
    
    f();

    return 0;
}
编译结果如下

注释掉函数f()中的return 0,在编译,会输出

可以看到__LINE__成了27,__FILE__成了Hello.c,原因就是前面的#line  14  "Hello.c"




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
STDOUT: Error: line 1: RuntimeError: file C:\Program Files\Autodesk\Maya2018\Python\lib\site-packages\maya\app\renderSetup\model\sceneObservable.py line 365: (kInvalidParameter): Argument is a NULL pointer 2023-07-23 19:44:36: 0: WARNING: Strict error checking on, ignoring the following unrecognized error or warning. If it is fatal, please email support@thinkboxsoftware.com with the error message. 2023-07-23 19:44:36: 0: STDOUT: Warning: line 1: Python callback failed 2023-07-23 19:44:36: 0: WARNING: Strict error checking on, ignoring the following unrecognized error or warning. If it is fatal, please email support@thinkboxsoftware.com with the error message. 2023-07-23 19:44:36: 0: STDOUT: Error: line 1: RuntimeError: file C:\Program Files\Autodesk\Maya2018\Python\lib\site-packages\maya\app\renderSetup\model\sceneObservable.py line 365: (kInvalidParameter): Argument is a NULL pointer 2023-07-23 19:44:36: 0: WARNING: Strict error checking on, ignoring the following unrecognized error or warning. If it is fatal, please email support@thinkboxsoftware.com with the error message. 2023-07-23 19:44:36: 0: STDOUT: Warning: line 1: Python callback failed 2023-07-23 19:44:36: 0: WARNING: Strict error checking on, ignoring the following unrecognized error or warning. If it is fatal, please email support@thinkboxsoftware.com with the error message. 2023-07-23 19:44:36: 0: STDOUT: Error: line 1: RuntimeError: file C:\Program Files\Autodesk\Maya2018\Python\lib\site-packages\maya\app\renderSetup\model\sceneObservable.py line 365: (kInvalidParameter): Argument is a NULL pointer 2023-07-23 19:44:36: 0: WARNING: Strict error checking on, ignoring the following unrecognized error or warning. If it is fatal, please email support@thinkboxsoftware.com with the error message. 2023-07-23 19:44:36: 0: STDOUT: Warning: line 1: Python callback failed 2023-07-23 19:44:36: 0: WARNING: Strict error checking on, ignoring the following unrecognized error or warning. If it is fatal, please email support@thinkboxsoftware.com with the error message. 2023-07-23 19:44:36: 0: STDOUT: Error: line 1: RuntimeError: file C:\Program Files\Autodesk\Maya2018\Python\lib\site-packages\maya\app\renderSetup\model\sceneObservable.py line 365: (kInvalidParameter): Argument is a NULL pointer 2023-07-23 19:44:36: 0: WARNING: Strict error checking on, ignoring the following unrecognized error or warning. If it is fatal, please email support@thinkboxsoftware.com with the error message. 2023-07-23 19:44:36: 0: STDOUT: Warning: line 1: Python callback failed 2023-07-23 19:44:48: 0: WARNING: Strict error checking on, ignoring the following unrecognized error or warning. If it is fatal, please email support@thinkboxsoftware.com with the error message. 2023-07-23 19:44:48: 0: STDOUT: Error: line 17: Division by zero. 2023-07-23 19:44:48: 0: WARNING: Strict error checking on, ignoring the following unrecognized error or warning. If it is fatal, please email support@thinkboxsoftware.com with the error message. 2023-07-23 19:44:48: 0: STDOUT: Error: line 17: Division by zero.
07-24
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值