单片机之从C语言基础到专家编程 - 4 C语言基础 - 4.6 基本数据类型转换

 

符号属性

长度属性

基本型

所占位数

取值范围

输入符

输出符

--

--

char

8

-2^7~2^7-1

%c

%c,%d,%u

signed

--

char

8

-2^7~2^7-1

%c

%c,%d,%u

unsigned

--

char

8

0~2^8-1

%c

%c,%d,%u

signed

short

int

16

-2^15~2^15-1

%hd

unsigned

short

int

16

0~2^16-1

%hu,%ho,%hx

signed

--

int

32

-2^31~2^31-1

%hd

unsigned

--

int

32

0~2^32-1

%u,%o,%x

signed

long

int

32

-2^31~2^31-1

%ld

unsigned

long

int

32

0~2^32-1

%lu,%lo,%lx

signed

long long

int

64

-2^63~2^63-1

%l64d

unsigned

long long

int

64

0~2^64-1

%l64u,%l64o,%l64x

--

--

float

32

+/-3.40282e+038

%f,%e,%g

--

--

double

64

+/-1.79769e+308

%lf,%le,%lg,%f,%e,%g

--

long

double

64(Keil MDK),96

+/-1.79769e+308

%Lf,%Le,%Lg

类型转换,一共有两种形式,一种是自动转换,也叫作隐式转换;另一种是强制转换,也叫作显式转换。

自动转换

是编译根据代码上下文环境自行判断的结果。

强制转换

是程序员明确提出要进行的类型转换,用特定的代码格式去指定某一种类型的转换。

类型转换的规则,一般是由低级到高级转换,如下图所示。

基本数据转换代码实例:


#include <stdio.h>

#include "stm32f10x.h"

 

 

void printDataConvert(void)

{

    unsigned char a = 0xff;

   

    signed char b;

   

    float c;

   

    double d;

   

    int e;

    printf("unsigned char a is %c",a);  //unsigned char a is 0xFF

   

    //隐式将内存值为0xFF的unsigned char a转换为signed char b,其结果仍然为0xFF

    b = a;

    printf("signed char a is %c",b);      //signed char a is 0xFF

   

    //隐式将内存值为0xFF的signed char b转换为float c,其结果为-1,这是因为b的值虽然为0xFF,但由于它是有符号的char,经过补码反向运算表示成了-1,将有符号的char值-1转为浮点数仍为-1

    c = b;

    printf("float signed char b is %f",c);     //float signed char b is -1

     

    //隐式将内存值为0xFF的unsigned char a转换为float c,其结果为255,

    c = a;

    printf("float unsigned char a is %f",c); //float unsigned signed char a is 255

     

   

   

    d = c;

    printf("double unsigned char a is %lf",d); //double unsigned signed char a is 255

   

    d = -1;

     

   

    printf("double d is %lf",d);     //double d is -1

   

   

    c = d;

    printf("float double d is %f",c);     //float double d is -1 

   

   

    b = d;

    printf("signed char double d is %c",b);//signed char double d is 0xFF

   

    a = d;

    printf("unsigned char double d is %c",a);//unsigned char double d is 0x00

   

    e = d;

    printf("int double d is %d",e);//int double d is 0xFFFFFFFF

}

 





 

void printDataManualConvert(void)

{

     

      int a;

      int b;

      float c;

     

      a = 5;

      b = 2;

      c = a / b;

      printf("c is %f",c);            //c is 2;

     

     

      c = (float)a / b;

      printf("c is %f",c);            //c is 2.5;

}







int main(void)

{

    uint32_t syscount;

   

    SystemInit();

   

    printf("Hello World");

   

    syscount = 0;

      

    while (1)

    {

           printf("This is %d times of the cricle",syscount);

           printDataAutoConvert();        

           printDataManualConvert();

           syscount++;

    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值