23_#error和#line使用分析

2018-03-20  本文已影响11人  编程半岛

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

1.#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的用法

4. 小结

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

上一篇 下一篇

猜你喜欢

热点阅读