C语言进阶课程学习记录-第23课 - #error 和 #line 使用分析
- 实验-#errer的使用
- 演示cmd窗口
- 实验-缺少#error
- 实验-#line 1的使用
- 实验-#line 1用于标记代码
- 小结
本文学习自狄泰软件学院 唐佐林老师的 C语言进阶课程,图片全部来源于课程PPT,仅用于个人学习记录
实验-#errer的使用
//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;
}
//23-1X.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;
}
演示cmd窗口
D:\Users\cy\Cxuexi\gccLearn\23>gcc 23-1X.c
23-1X.c:7:1: error: unknown type name 'class'
class CppClass
^~~~~
23-1X.c:8:1: error: expected '=', ',', ';', 'asm' or '__attribute__' before '{' token
{
^
D:\Users\cy\Cxuexi\gccLearn\23>g++ 23-1X.c
D:\Users\cy\Cxuexi\gccLearn\23>gcc 23-1.c
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:7:1: error: unknown type name 'class'
class CppClass
^~~~~
23-1.c:8:1: error: expected '=', ',', ';', 'asm' or '__attribute__' before '{' token
{
^
实验-缺少#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");
#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");
#endif
return 0;
}
/*output:
1. Query Information.
2. Record Information.
3. Delete Information.
*/
实验-#line 1的使用
#include <stdio.h>
int main()
{
printf("%s : %d\n", __FILE__, __LINE__);//文件名 所在行号//129
#line 1 "delphi_tang.c"//重新定义行号:下一行为1 文件名"delphi_tang.c"
printf("%s : %d\n", __FILE__, __LINE__);//文件名 所在行号
return 0;
}
/*output:
D:\Users\cy\Test\test1.c : 129
delphi_tang.c : 2
*/
实验-#line 1用于标记代码
#line 1用于标记代码编写人和行号(便于Debug),代码示例:
#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 Delphi.
// Begin
#line 1 "delphi_tang.c"
int main()
{
printf("%s : %d\n", __FILE__, __LINE__);
printf("%s : %d\n", __FILE__, __LINE__);
return 0;
}
// End
/*output:
delphi_tang.c : 5
delphi_tang.c : 7
*/
小结
#error用于自定义一条编译错误信息
#warning用于自定义一条编译警告信息
#error和#warning常应用于条件编译的情形
#line用于强制指定新的行号和编译文件名