C语言进阶课程学习记录-第22课 - 条件编译使用分析


本文学习自狄泰软件学院 唐佐林老师的 C语言进阶课程,图片全部来源于课程PPT,仅用于个人学习记录

条件编译基本概念

在这里插入图片描述

条件编译实验

#include <stdio.h>

#define C 1

int main()
{
    const char* s;

    #if( C == 1 )
        s = "This is first printf...\n";
    #else
        s = "This is second printf...\n";
    #endif

    printf("%s", s);

    return 0;
}


/*output:
This is first printf...

*/

条件编译本质

在这里插入图片描述

实验-ifdef

#include <stdio.h>

int main()
{
    const char* s;

    #ifdef C
        s = "This is first printf...\n";
    #else
        s = "This is second printf...\n";
    #endif

    printf("%s", s);

    return 0;
}


/*output:
This is second printf...

*/

include本质

在这里插入图片描述

实验-间接包含同一个头文件

// global.h
//#ifndef _GLOBAL_H_
//#define _GLOBAL_H_
int global = 10;//D:\Users\cy\Test\global.h|4|error: redefinition of 'global'|

//#endif

// test.h

//#ifndef _TEST_H_
//#define _TEST_H_
#include "global.h"
const char* NAME = "test.h";
char* hello_world(){    return "Hello world!\n";}

//#endif

#include <stdio.h>
#include "test.h"
#include "global.h"

int main()
{
    const char* s = hello_world();
    int g = global;

    printf("%s\n", NAME);
    printf("%d\n", g);

    return 0;
}

//error  D:\Users\cy\Test\global.h|4|error: redefinition of 'global'|
// global.h
#ifndef _GLOBAL_H_
#define _GLOBAL_H_
int global = 10;
#endif

// test.h

#ifndef _TEST_H_
#define _TEST_H_
#include "global.h"
const char* NAME = "test.h";
char* hello_world(){    return "Hello world!\n";}

#endif

#include <stdio.h>
#include "test.h"
#include "global.h"

int main()
{
    const char* s = hello_world();
    int g = global;

    printf("%s\n", NAME);
    printf("%d\n", g);

    return 0;
}

/*output:
test.h
10

*/

解决重复包含的方法-ifndef

在这里插入图片描述
在这里插入图片描述

实验-条件编译的应用

“product.h”

#define DEBUG 1
#define HIGH  1
#include <stdio.h>
#include "product.h"

#if DEBUG
    #define LOG(s) printf("[%s:%d] %s\n", __FILE__, __LINE__, s)
#else
    #define LOG(s) NULL
#endif

#if HIGH
void f()
{
    printf("This is the high level product!\n");
}
#else
void f()
{
}
#endif

int main()
{
    LOG("Enter main() ...");

    f();

    printf("1. Query Information.\n");
    printf("2. Record Information.\n");
    printf("3. Delete Information.\n");

    #if HIGH
    printf("4. High Level Query.\n");
    printf("5. Mannul Service.\n");
    printf("6. Exit.\n");
    #else
    printf("4. Exit.\n");
    #endif

    LOG("Exit main() ...");

    return 0;
}

/*output:
[D:\Users\cy\Test\test1.c:143] Enter main() ...
This is the high level product!
1. Query Information.
2. Record Information.
3. Delete Information.
4. High Level Query.
5. Mannul Service.
6. Exit.
[D:\Users\cy\Test\test1.c:159] Exit main() ...

*/

小结

通过编译器命令行能够定义预处理器使用的宏
条件编译可以避免重复包含头同一个头文件
条件编译是在工程开发中可以区别不同产品线的代码条件编译可以定义产品的发布版和调试版

相关推荐

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-04 19:46:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-04 19:46:03       100 阅读
  3. 在Django里面运行非项目文件

    2024-04-04 19:46:03       82 阅读
  4. Python语言-面向对象

    2024-04-04 19:46:03       91 阅读

热门阅读

  1. 汽车CAN网络中的checksum和Rollingcounter的作用?

    2024-04-04 19:46:03       37 阅读
  2. Webpack中loader和plugin的区别?

    2024-04-04 19:46:03       30 阅读
  3. 我爱我缓慢向上的勇气

    2024-04-04 19:46:03       40 阅读
  4. 项目管理工具对比:甘特图与看板

    2024-04-04 19:46:03       42 阅读
  5. Uinx线程详解

    2024-04-04 19:46:03       39 阅读
  6. 【设计模式】-单例模式

    2024-04-04 19:46:03       37 阅读