牛骨文教育服务平台(让学习变的简单)
博文笔记

stm32 C语言的数据类型说明

创建时间:2015-10-17 投稿人: 浏览次数:3812

该文章参考和转载:http://www.cnblogs.com/wangh0802PositiveANDupward/archive/2013/01/01/2841697.html。

stm32编程过程经常定义变量类型,经常担心数据运算过程中 超过变量类型范围。因为在编程过程中,不同的CPU,其数据类型的意义各不相同,所以一定要注意相应变量数据类型的定义和转换,否则在计算中可能会出现不确定的错误。所以下面列出常见数据类型:

一、C语言数据类型

stm32使用的数据类型定义在 stm32f4xx.h中

整型定义:

#include "core_cm4.h"             /* Cortex-M4 processor and core peripherals */
#include "system_stm32f4xx.h"
#include <stdint.h>

/** @addtogroup Exported_types
  * @{
  */  
/*!< STM32F10x Standard Peripheral Library old types (maintained for legacy purpose) */
typedef int32_t  s32;
typedef int16_t s16;
typedef int8_t  s8;

typedef const int32_t sc32;  /*!< Read Only */
typedef const int16_t sc16;  /*!< Read Only */
typedef const int8_t sc8;   /*!< Read Only */

typedef __IO int32_t  vs32;
typedef __IO int16_t  vs16;
typedef __IO int8_t   vs8;

typedef __I int32_t vsc32;  /*!< Read Only */
typedef __I int16_t vsc16;  /*!< Read Only */
typedef __I int8_t vsc8;   /*!< Read Only */

<span style="color:#ff6666;">typedef uint32_t  u32;    /*常用类型*/
typedef uint16_t u16;
typedef uint8_t  u8;</span>

typedef const uint32_t uc32;  /*!< Read Only */
typedef const uint16_t uc16;  /*!< Read Only */
typedef const uint8_t uc8;   /*!< Read Only */

typedef __IO uint32_t  vu32;
typedef __IO uint16_t vu16;
typedef __IO uint8_t  vu8;

typedef __I uint32_t vuc32;  /*!< Read Only */
typedef __I uint16_t vuc16;  /*!< Read Only */
typedef __I uint8_t vuc8;   /*!< Read Only */
浮点型:

#if !defined(__STRICT_ANSI__) || defined(__USE_C99_MATH)
  /* C99 additions */
  typedef float float_t;
  typedef double double_t;
注:还有float 浮点型 编译器中不能看到其定义(估计已编译了)。

而uint32_t 、uint16_t、uint8_t在哪里定义?在stdint.h文件中,详见下面:

    /* exact-width signed integer types */
typedef   signed          char int8_t;
typedef   signed short     int int16_t;
typedef   signed           int int32_t;
typedef   signed       __int64 int64_t;

    /* exact-width unsigned integer types */
typedef unsigned          char uint8_t;
typedef unsigned short     int uint16_t;
typedef unsigned           int uint32_t;
typedef unsigned       __int64 uint64_t;

    /* minimum values of exact-width signed integer types */
#define INT8_MIN                   -128     /*	s8 占用1个byte,数据范围 -2^7  到	(2^7-1)  */
#define INT16_MIN                -32768		/* s16 占用2个byte,数据范围 -2^15 到 (2^15-1)	*/
#define INT32_MIN          (~0x7fffffff)   /* -2147483648 is unsigned   s32	占用 4个byte,数据范围 -2^31 到 (2^31-1)   */
#define INT64_MIN  __ESCAPE__(~0x7fffffffffffffffll) /* -9223372036854775808 is unsigned  int64_t占用8个byte,数据范围 -2^63 到 (2^63-1) */

    /* maximum values of exact-width signed integer types */
#define INT8_MAX                    127
#define INT16_MAX                 32767
#define INT32_MAX            2147483647
#define INT64_MAX  __ESCAPE__(9223372036854775807ll)

    /* maximum values of exact-width unsigned integer types */
#define UINT8_MAX                   255				/* u8  占用1个byte,	数据范围 0 - 2^8*/
#define UINT16_MAX                65535				/* u16 占用2个byte,	数据范围 0 - 2^16*/
#define UINT32_MAX           4294967295u			/* u32 占用4个byte,	数据范围 0 - 2^32*/
#define UINT64_MAX __ESCAPE__(18446744073709551615ull)

由上述可知:

1、有符号整型

  • s8 占用1个byte,数据范围 -2^7  到 (2^7-1)
  • s16 占用2个byte,数据范围 -2^15 到 (2^15-1)
  • s32 占用 4个byte,数据范围 -2^31 到 (2^31-1)2^31  = 2147483647
  • int64_t占用8个byte,数据范围 -2^63 到 (2^63-1)    2^63 = 9223372036854775807ll

2、无符号整型

  • u8  占用1个byte, 数据范围 0 - 2^8
  • u16 占用2个byte, 数据范围 0 - 2^16
  • u32 占用4个byte, 数据范围 0 - 2^32 2^32  = 4294967295
  • uint64_t 占用8个byte, 数据范围 0 - 2^64 2^64  = 18446744073709551615
3、浮点型

  •     float ——4个byte,有符号型,可以表达负数/小数; Float 类型至少要能精确表示到小数点后6位。
  •    double——8个byte,有符号型,可以表达负数/小数;Double 类型至少要能精确到小数点后 10 位。

二、不同数据类型混合运算

在C语言中,不同类型的数据间是可以混合运算的。在进行运算时,不同类型的数据要先转换成同一类型,然后进行运算。转换的规则如下:



注意:箭头的方向只表示数据类型级别的高低,由低向高转换,这个转换过程是一步到位的。

(三)数据类型转换规则

各类数据类型的转换,分为两种方式:隐式(编译软件自动完成),显式(程序强制转换)

隐式转换规则:

字符必须先转换为整数(C语言规定字符类型数据和整型数据之间可以通用) 
   short型转换为int型(同属于整型) 
   float型数据在运算时一律转换为双精度(double)型,以提高运算精度(同属于实型) 
   赋值时,一律是右部值转换为左部类型 
[注] 
     当整型数据和双精度数据进行运算时,C先将整型数据转换成双精度型数据,再进行运算,结果为双精度类型数据 
     当字符型数据和实型数据进行运算时,C先将字符型数据转换成实型数据,然后进行计算,结果为实型数据

显式转换规则:

例:(int)(x+y);

注:强制类型转换时,得到一个所需要的中间变量,原来变量的类型未发生变化。



声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。