C基础第23课--#error 和 #line使用分析

学习自狄泰软件学院唐佐林老师C语言课程,文章中图片取自老师的PPT,仅用于个人笔记。


实验1

情景:需要定义某个宏的时候,假如没有定义 代码就会执行其他分支,不会执行目标分支,这时候如果在条件编译的时候就添加 #error预处理器指示字 没有定义该宏 就报错 就会避免这种隐患。

在这里插入图片描述

在这里插入图片描述

实验1

情景:需要定义某个宏的时候,假如没有定义 代码就会执行其他分支,不会执行目标分支,这时候如果在条件编译的时候就添加 #error 没有定义该宏 就报错 就会避免这种隐患。

没有加 #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;
}



mhr@ubuntu:~/work/C$ gcc 23-2.c
mhr@ubuntu:~/work/C$ ./a.out 
1. Query Information.
2. Record Information.
3. Delete Information.
mhr@ubuntu:~/work/C$ 
mhr@ubuntu:~/work/C$ 

加 #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 macro 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");
#endif
    
    return 0;
}

由于忘记定义目标宏 则条件编译的时候直接输出错误

mhr@ubuntu:~/work/C$ gcc 23-2.c
23-2.c: In function ‘f’:
23-2.c:12:3: error: #error the macro PRODUCT is not defined
  #error  the macro PRODUCT is not defined
   ^
mhr@ubuntu:~/work/C$ 

命令行定义宏:gcc 23-2.c -DPRODUCT=3

mhr@ubuntu:~/work/C$ gcc 23-2.c -DPRODUCT=3
mhr@ubuntu:~/work/C$ ./a.out 
This is a high level product!
1. Query Information.
2. Record Information.
3. Delete Information.
4. High Level Query.
5. Mannul Service.
6. Exit.
mhr@ubuntu:~/work/C$ 

同理 #warning 也是这样使用 但是 #warning 预处理器关键字 指示会有编译警告 不会是错误

在这里插入图片描述

实验2

#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" //重新指定 行号和文件名,下一行为第一行 文件名由23-3.c 换成了delphi_tang.c


int main()
{
    printf("%s : %d\n", __FILE__, __LINE__);
    
    printf("%s : %d\n", __FILE__, __LINE__);
    
    return 0;
}

// End

mhr@ubuntu:~/work/C$ gcc 23-3.c
mhr@ubuntu:~/work/C$ ./a.out 
delphi_tang.c : 5
delphi_tang.c : 7
mhr@ubuntu:~/work/C$ 
mhr@ubuntu:~/work/C$ 

在这里插入图片描述

发布了192 篇原创文章 · 获赞 100 · 访问量 8万+

猜你喜欢

转载自blog.csdn.net/LinuxArmbiggod/article/details/103946332