单片机中各个数据类型在内存中占的字节数:
char size = 1
short int size = 2
int size = 2
long int size = 4
float size = 4
源代码
/********************************************
main.c文件
*******************************************/
#include
#include
#include
#include "uart.h"
//#include "fun.h"
//#define myprintf PC_select_serial();printf
/*********************************************
ADC模数转换
*********************************************/
#define TEMPCH 0x00 //P1.0
#define DOCH 0x01 //P1.1
#define pHCH 0x02 //P1.2
typedef struct B
{
float a;
float b;
}A;
void main()
{
unsigned char b[8]=0;
float a = 2.9999;
float h = -3.4521;
A HH;
int c = sizeof(char);
init_uart();
// putstr_printf(b);
// bb = b;
printf("char size = %d
",c);
c = sizeof(short int);
printf("short int size = %d
",c);
c = sizeof(int);
printf("int size = %d
",c);
c = sizeof(long int);
printf("long int size = %d
",c);
c = sizeof(float);
printf("float size = %d
",c);
memcpy(b,&a,4);
memcpy(&b[4],&h,4);
HH = *(A *)b;
printf("HH.a = %f
",HH.a);
printf("HH.b = %f
",(10-HH.b));
while(1);
}
/******************************************************
uart.c文件
******************************************************/
#include
#include "define.h"
/*****************************************
******************************************/
void init_uart()
{
SCON = 0x50;
TMOD |= 0x20;
PCON = 0x80; // pcon =0x80 波特率翻倍
TH1 = 0xEE;
TL1 = 0xEE;
TI = 1;
TR1 = 1;
}
void putstr_printf(char *pStr)
{
TI = 0;
while (*pStr != '