You are on page 1of 3

Atılım University

Department of Mechatronics Engineering


MECE 308

CCS Pic-C: Variables, Constants and Operators


In a typical C program written for a PIC MCU, it is quite common to encounter various constants
and variables of different types, sizes and scopes (i.e. local or global), several arithmetic and logical
operations, and various expressions. Therefore, data types and operators supported by the CCS Pic-
C compiler, and compiler specific properties related to these must be well studied. CCS Pic-C
Compiler Reference Manual, CCS Pic-C IDE help, http://www.ccsinfo.com/forum/ web site, and
example source files included with the compiler, provides good information about these and on
many other topics.

CCS Pic-C compiler provides the following data type specifications:

Size Examples
Data Type Specifier Signed/Unsigned Range
(bits)
short short pire;
short int 1 Unsigned 0 to 1 short int pire;
int1 pire;
int1
unsigned unsigned zat;
unsigned int unsigned int zat;
8 Unsigned 0 to 255 int zat;
int
char zat;
char
int8 zat;
int8
long long tatil;
long int 16 Unsigned 0 to 65535 long int tatil;
int16 tatil;
int16
signed signed hava;
signed int 8 Signed -128 to 127 signed int hava;
signed int8 hava;
signed int8
signed long Signed signed long yol;
16 -32768 to 32767
signed int16 signed int16 yol;
int32 32 Unsigned 0 to 4294967296 int32 deniz;
signed int32 32 Signed -2147483648 to 2147483648 singned int32 gol;
float 32 Signed ±1.175494351x10-38 to ±3.402823466x1038 float okyanus;

Furthermore, to aid program development and to improve readability of source codes, the following
definitions are given in devices.h files:
#define FALSE 0 // FALSE is defined to be equal to zero
#define TRUE 1 // TRUE is defined to be equal to one
#define BYTE int // BYTE is defined to be equal to variable-type int
#define BOOLEAN short int // BOOLEAN is defined to be equal to variable-type short
int
Hence with the above definitions, the following variable declerations may be utilized:
BYTE zat; // zat is a variable of type BYTE which is in fact an 8-bit size int type
BOOLEAN pire; // pire is a variable of type BOOLEAN which is in fact a 1-bit size short int
Atılım University
Department of Mechatronics Engineering
MECE 308

Keyword CONST can be used before a variable name (identifier) to treat it as a constant.
Constants must be initialized and may not be changed at run-time. Pointers to constants are not
permitted. The compiler has support for placing any data structure into the device ROM (Program
Memory) as a constant read-only element. For example, to place a 10 element BYTE array in RAM
or ROM use:
byte table[10]= {9,8,7,6,5,4,3,2,1,0}; // table is an array of 10 byte elements placed in RAM
byte const table[10]= {9,8,7,6,5,4,3,2,1,0}; // table is an array of 10 byte elements placed in ROM
Numeric, character and string (array of characters) type constants with various formats are
summarized in the following table. In the examples of this table sayi, harf and kelime are variables
that are equated to various constants.

Constant Type Format Examples


123 Numeric Decimal sayi=123;
0123 Numeric Octal sayi=0123;
0x123 Numeric Hexadecimal sayi =0x123;
0b010010 Numeric Binary sayi =0b010010;
'x' Character Character harf=’x’
'\010' Character Octal harf=’\010’
'\xA5’ Character Hexadecimal harf=’\xA5’
'\c' Character Character harf=’\c’
"abcdef" String String kelime="abcdef
"

CCS Pic-C provides several arithmetic and logical operators that include the following:
Operator Examples a=0x03 a=0b00000011
b=0x11 b=0b00010001

Symbol Name Result


+ Addition a+b 0x14 0b00010100
- Subtraction b-a 0x0E 0b00001110
* Multiplication a*b 0x33 0b00110011
/ Division b/a 0x05 0b00000101
(2 operands)

% Mod b%a 0x02 0b00000010


& Bitwise AND b&a 0x01 0b00000001
| Bitwise OR b|a 0x13 0b00010011
^ Bitwise XOR b^a 0x12 0b00010010
Binary

>> Shift right b>>a 0x02 0b00000010


<< Shift left b<<a 0x88 0b10001000
++ Increment ++a 0x04 0b00000100
-- Decrement --a 0x02 0b00000010
(1 operand)

- Negation -0b00000011
Unary

-a -0x03 0b11111101
~ One's complement ~a 0xFC 0b11111100
Atılım University
Department of Mechatronics Engineering
MECE 308

Furthermore, there are operators such as +=, -=, *=, /=, %=, &=, |=, ^=, >>=, <<= that are called
compound operators.

CCS Pic-C provides the following relational and logical operators:

Operator Example Result


Symbo Name a=0x03; b=0x11;
l
== Equality a == b FALSE
!= Not equivalent a != b TRUE
> Greater than a>b FALSE
>= Greater than or equal to a >= b FALSE
< Less than a<b TRUE
<= Less than or equal to a <= b TRUE
&& Logical AND a && b TRUE
|| Logical OR a || b TRUE
! Logical NOT !a FALSE
TRUE and FALSE are defined in devices.h to be equal to 1 and 0, respectively.

You might also like