2009-11-10 175 views

回答

5

这是很难不知道你想编译哪个CPU的代码。假设例如Microchip的C18编译器为PIC18,该User Guide状态下基本型尺寸:

TYPE    SIZE  RANGE 
char(1,2)   8 bits -128 127 
signed char   8 bits -128 127 
unsigned char  8 bits 0 255 
int     16 bits -32,768 32,767 
unsigned int  16 bits 0 65,535 
short    16 bits -32,768 32,767 
unsigned short  16 bits 0 65,535 
short long   24 bits -8,388,608 8,388,607 
unsigned short long 24 bits 0 16,777,215 
long    32 bits -2,147,483,648 2,147,483,647 
unsigned long  32 bits 0 4,294,967,295 

请注意,这包括一些类型(short long)不在标准C.

+0

我可以建议您声明这些值适用于PIC18系列的Microchip C18编译器吗?我知道链接的用户指南提供了这些信息,但它会使这个答案更加完整,以便将其包含在此处。此外,考虑到该问题没有指定编译器和目标微控制器,应该强调的是,其他目标的类型可能是不同的大小。 – 2009-11-10 20:40:38

1

我会警惕这样的概括。 MPLAB只是一个IDE - 它适用于不同的芯片。 Microchip有8位控制器,如PIC18F,16位和32位控制器。每种数据类型可能会有所不同,并对性能产生严重影响。即对于8位芯片,16位和32位数据类型可以用软件模拟,这并不总是你想要的。

1

int,long等的值从未在所有编译器中标准定义(reference)。出于这个原因,建议尽量使用库:

#include <stdint.h> 

要使用这个库为自己的目的,请尝试使用如下代码:

typedef uint8_t BYTE 
typedef uint16_t WORD 
typedef uint32_t LONG 

然后你只需要使用这些来定义你的变量。这个方法通常使用一个integer.h文件来存储这些定义,并且包含在需要的地方。

-2
#include<stdint.h> 
long x; 

这两件事情让我打通;) 而其余的信息。已被其他人共享。

0

以下是在不同MPLAB XC编译器上实现的整数数据类型。

  1. 为8位器件的数据类型(实施上XC8编译器): enter image description here

  2. 为16位器件的数据类型(上XC16编译器实现): enter image description here

  3. 数据32位器件的类型(在XC32编译器上实现):enter image description here