美文网首页
23_#error和#line使用分析

23_#error和#line使用分析

作者: 编程半岛 | 来源:发表于2018-03-20 17:00 被阅读11次

关键词:#error的用法、 #error在条件编译中的应用、#line的用法

1.#error的用法

  • #error用于生成一个编译错误消息
  • 用法:#error message,其中message不需要用双引号包围
  • #error编译指示字用于自定义程序员特有的编译错误消息,类似的,#warning用于生成编译警告
  • #error是一种预编译器指示字
  • #error可用于提示编译条件是否满足
#include <stdio.h>

// 使用#error提示错误信息
#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;
}

输出结果:

1.c:4: error: #error This file should be processed with C++ compiler.
1.c:7: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘CppClass’

2. #error在条件编译中的应用

#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 defined!
#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.\n");
    printf("5. Mannul Service.\n");
    printf("6. Exit.\n");
#else
    #error The "PRODUCT" is NOT defined!
#endif
    
    return 0;
}

3. #line的用法

  • #line用于强制指定新的行号和编译文件名,并对源程序的代码重新编号
  • 用法#line number filename:其中filename可省略
  • #line编译指示字的本质是重新定义__LINE____FILE__

4. 小结

  • #error用于自定义一条编译错误信息
  • #warning用于自定义一条编译警告信息
  • #error#warning常用于条件编译的情形
  • #line用于强制指定新的行号和编译文件名

声明:此文章为本人在学习狄泰软件学院《C语言深度解析》所做的笔记,文章中包含狄泰软件资料内容一切版权归狄泰软件所有!

相关文章

  • 23_#error和#line使用分析

    关键词:#error的用法、 #error在条件编译中的应用、#line的用法 1.#error的用法 #erro...

  • OLAP和OLTP

    OLTP 分析 on-line-transaction processingOLAP 传输 on-line-an...

  • Python开发必备库与工具

    1. line_profiler——分析每行耗时(性能) line_profiler是一款分析python的CPU...

  • awk和sed指令入门

    sed偏向于编译文档,awk偏向与分析文本 sed使用方式 增加 sed -i "2a line 3 " test...

  • HTML3

    line-height有什么作用? 定义和用法line-height 属性设置行间的距离(行高)。注释:不允许使用...

  • 使用Facebook和Line等分享

    前言 我们在做开发的时候,需要将自己开发的网页分享到facebook、telegram、line等页面。然后需要在...

  • 135. Candy

    题目分析 There are N children standing in a line. Each child ...

  • 135. Candy

    问题分析 There are N children standing in a line. Each child ...

  • [Command Line]使用Command Line(终端)

    一. 下载安装Git 查看电脑是否安装git,打开终端,输入git,回车如果输出如下,则代表已安装了git 如果未...

  • 在日本,不要发语音消息喔

    导读:想和日本人构建友好关系的话… 日本人的国民聊天APP是LINE,和微信使用起来是差不多的。 LINE在日本近...

网友评论

      本文标题:23_#error和#line使用分析

      本文链接:https://www.haomeiwen.com/subject/yyyjfftx.html