自己的记录 结构体全局变量多个文件调用

文章讲述了在C语言中如何声明和使用全局变量,具体涉及一个名为Quiet_motor_Info_t的结构体类型,包含电机结束标志位、角度计数器和电机运行方向等字段。在a.h头文件中声明全局变量,在a.c和b.c源文件中分别进行初始化和函数调用。同时,文章还提及了条件编译的概念,通过DEBUG宏定义控制调试模式的输出。
摘要由CSDN通过智能技术生成

a.h extern Quiet_motor_Info_t motor_info; // 声明为全局变量

#ifndef __A__H__
#define __A__H__


typedef struct {
    int motor_end_flg; // 电机结束标志位   A端1  B端2
    int angle_count;    // 角度计数器
    int motor_run_dir; // 电机运行方向
} Quiet_motor_Info_t;

extern Quiet_motor_Info_t motor_info; // 声明为全局变量

int get_ang(void);
#endif

a.c 包含头文件

#include "stdio.h"
#include "stdlib.h"
#include "a.h"



int main()
{

    //Quiet_motor_Info_t motor_info;

    motor_info.angle_count = 50;
    printf("main ang:%d\r\n",motor_info.angle_count);
    get_ang();

    printf("main ang:%d\r\n",get_ang());

}

b.c

#include "stdio.h"
#include "a.h"


Quiet_motor_Info_t motor_info;
int get_ang(void)
{
        //Quiet_motor_Info_t motor_info;
        printf("ang:%d\r\n",motor_info.angle_count);
        return motor_info.angle_count;
}

定义条件编译:可以使用 #ifdef、#ifndef、#else 和 #endif 等条件编译指令来进行条件编译

#include <stdio.h>

#define DEBUG

int main() {
  	#ifdef DEBUG
  		printf("Debug mode is enabled.\n");
	#else
  		printf("Debug mode is disabled.\n");
	#endif
  return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Quieeeet

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值