Variable types

From Curuxa

Contents

This is the entire list of variable types you can use in your programs written in C-language using the SDCC compiler.


Default variable types

Type Size (bits) Values
char 8 -128 to 127
unsigned char 8 0 to 255
int 16 -32768 to 32767
unsigned int 16 0 to 65535
long 32 -2147483648 to 2147483647
unsigned long 32 0 to 4294967295
float 32

C-language doesn't support booleans natively (single-bit variables that can only take values 0/1, true/false, yes/no...), but you can use 8-bit variables (char) to store them.


Usage example

void main() {
    unsigned char a;
    char b;
    int c;
    float d;
    a=240; //if this were a (signed) char instead of an unsigned char, you couldn't store values higher than 127
    b=-7;
    c=a+b;
    d=c/9;
    //...
}


Recommended defines

It's recommended to put the following defines at the beginning of each program. After putting them you'll be able to declare variables and see much more easily which type and size of variable you are using:

#define int8 char
#define int16 int
#define int32 long
 
#define bool unsigned int8
#define true 1
#define TRUE 1
#define false 0
#define FALSE 0


After putting these defines, the table of available types would be:

Type Size (bits) Values
bool 8 0 or 1
int8 8 -128 to 127
unsigned int8 8 0 to 255
int16 16 -32768 to 32767
unsigned int16 16 0 to 65535
int32 32 -2147483648 to 2147483647
unsigned int32 32 0 to 4294967295
float 32


Usage example

#include <pic16fxxx.h>
 
#define int8 char
#define int16 int
#define int32 long
 
#define bool unsigned int8
#define true 1
#define TRUE 1
#define false 0
#define FALSE 0
 
void main() {
    unsigned int8 a;
    int8 b;
    int16 c;
    float d;
    a=240; //if this were a (signed) int8 instead of an unsigned int8, you couldn't store values higher than 127
    b=-7;
    c=a+b;
    d=c/9;
    //...
}


Boolean variables

Often you will want to store a single-bit (boolean) variable that can take two possible values: 0/1, true/false, yes/no.

Boolean variables take 8 bits of RAM but are supposed to store a single 0 or 1. It wastes some memory but this way is much more efficient.


Usage example

// (after writing the previous recommended defines)
 
bool myVar;
bool anotherVar;
int8 f = 67;
myVar = 0;
myVar = 1;
myVar = true;
myVar = false;
myVar = TRUE;
myVar = FALSE;
if(myVar == true) anotherVar=false;
f = f/5;
if( f < 15 ) myVar = true;
else myVar = false;