C Tips: stdbool.h

本文提供了一个针对VisualStudio2012的自定义stdbool.h实现,解决了某些编译器版本不支持标准stdbool.h头文件的问题。包括详细解释和代码示例。

下面是来自 cprogramming.com 的 stdbool.h 的定义:

#ifndef STDBOOL_H_
#define STDBOOL_H_
 
/**
 * stdbool.h - ISO C99 Boolean type
 * Author    - Bill Chatfield
 * E-mail    - bill underscore chatfield at yahoo dot com
 * Copyright - You are free to use for any purpose except illegal acts
 * Warrenty  - None: don't blame me if it breaks something
 *
 * In ISO C99, stdbool.h is a standard header and _Bool is a keyword, but
 * some compilers don't offer these yet. This header file is an 
 * implementation of the standard ISO C99 stdbool.h header file. It checks
 * for various compiler versions and defines things that are missing in
 * those versions.
 *
 * The GNU and Watcom compilers include a stdbool.h, but the Borland
 * C/C++ 5.5.1 compiler and the Microsoft compilers do not.
 * 
 * See http://predef.sourceforge.net/precomp.html for compile macros.
 */
 
/**
 * Borland C++ 5.5.1 does not define _Bool.
 */
#ifdef __BORLANDC__
typedef int _Bool;
#endif
 
/**
 * Microsoft C/C++ version 14.00.50727.762, which comes with Visual C++ 2005,
 * and version 15.00.30729.01, which comes with Visual C++ 2008, do not 
 * define _Bool.
 */
#if defined(_MSC_VER) && _MSC_VER <= 1500
typedef int _Bool;
#endif
 
/**
 * Define the Boolean macros only if they are not already defined.
 */
#ifndef __bool_true_false_are_defined
#define bool _Bool
#define false 0 
#define true 1
#define __bool_true_false_are_defined 1
#endif
 
#endif /*STDBOOL_H_*/

但是上述文件在 Visual Studio 2012 中不工作。


下面是我自己写的 stdbool.h 的定义:

#ifndef STDBOOL_H_
#define STDBOOL_H_

/**
 * stdbool.h
 * Author    - Yaping Xin
 * E-mail    - xinyp at live dot com
 * Copyright - You are free to use for any purpose except illegal acts
 * Warrenty  - None: don't blame me if it breaks something
 *
 * In ISO C99, stdbool.h is a standard header and _Bool is a keyword, but
 * some compilers don't offer these yet. This header file is an 
 * implementation of the stdbool.h header file.
 *
 */

#ifndef _Bool
typedef unsigned char _Bool;
#endif /* _Bool */
 
/**
 * Define the Boolean macros only if they are not already defined.
 */
#ifndef __bool_true_false_are_defined
#define bool _Bool
#define false 0 
#define true 1
#define __bool_true_false_are_defined 1
#endif /* __bool_true_false_are_defined */
 
#endif /* STDBOOL_H_ */


参考链接:


#include <stdint.h> #include <stdbool.h> #define RAT_TICKS_PERIOD_MS 10 #define ConvertMsToRatTicks(microseconds) \ ((microseconds) / (RAT_TICKS_PERIOD_MS)) #define ONE_SHOT_MODE false #define PERIOD_MODE true /* too much timers */ typedef enum { BEEP_TIMER, RTC_TIMER, NFC_TIMER, NFC_DELAY_TIMER, CARD_TIMER, DEV_REP_TIMER, DEV_DELAY_TIMER, PULSE_TIMER, PAUSE_TIMER, APPOINT_TIMER, AT_DELAY_TIMER, AT_RX_TIMER, GPRS_DELAY_TIMER, GPRS_SEND_TIMER, GPRS_TIMEOUT_TIMER, GPRS_HEARTBEAT_TIMER, GPRS_KEEPALIVE_TIMER, LCD_TICK_TIMER, LCD_TIPS_TIMER, LCD_PAGE_TIMER, LCD_RST_TIMER, LCD_BK_TIMER, OTA_DELAY_TIMER, OTA_TIMEOUT_TIMER, NUM_OF_TIMER }timer_id_t; typedef void (*TimerCallbackFunction_t)(void); /* Define the Timer Control Block data type. */ typedef struct TM_TCB_STRUCT { const char *pcTimerName; /* Timer name for debug */ volatile bool tm_auto_reload; /* One_shot/Period */ volatile bool tm_enabled; /* Timer enabled flag */ volatile bool tm_expirations; /* Number of expirations */ TimerCallbackFunction_t pxCallbackFunction; volatile uint32_t tm_initial_time; /* Initial time */ volatile uint32_t tm_reschedule_time; /* Reschedule time */ } TM_TCB; void SoftTimer_Create( const char* pcTimerName, const uint8_t pvTimerID, bool AutoReload, uint32_t timerTicks, TimerCallbackFunction_t callback ); extern TM_TCB TimerList[NUM_OF_TIMER]; void SoftTimer_Init(void); void SoftTimer_Start(const uint8_t pvTimerID); void SoftTimer_Change_Period(const uint8_t pvTimerID, uint32_t timerTicks); void SoftTimer_Stop(const uint8_t pvTimerID); void SoftTimer_Process(void);
最新发布
07-15
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值