1. 程式人生 > >#error和#line使用分析

#error和#line使用分析

提示 %d lin {} \n 使用 分析 ID 定義

#error的用法

  • #error用於生成一個編譯錯誤消息
    • 用法:error message(不需要用雙引號包圍)
  • #error編譯指示字用於自定義程序員特有的編譯錯誤,消息類似的
  • #warning用於生成編譯警告
  • #error是一種預編譯器指示字
  • #error可用於提示編譯條件是否滿足

編譯過程中的任意錯誤信息意味著無法生成最終的可執行程序

例子1:#error預處理初探

#include<stdio.h>

#ifndef __cplusplus
    #error This file should be processed with c++ compliler.
#endif 
// __cplusplus class CppClass { private: int m_value; public: CppClass(){} ~CppClass(){} }; int main() { return 0; }

#line的用法

#line用於強制制定新的行號和編譯文件名,並對源程序的代碼重新編號
用法:#line number filename(filename可以省略)
#line編譯指示字的本質是重定義 __LINE__和FILE

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

小結

  • #error用於自定義一條編譯錯誤信息
  • #warning用於自定義一條編譯警告信息
  • #error和#warning常用於條件編譯的情形
  • #line用於強制指定新的行號和編譯文件名

#error和#line使用分析