APPENDIX 6. Essentials of C – Designing Embedded Systems with PIC Microcontrollers, 2nd Edition

APPENDIX 6. Essentials of C
This appendix provides summary information on key aspects of the C programming language as a set of tables. Example usage and further explanations of most of these features appear in Chapter 14, Chapter 15, Chapter 16, Chapter 17, Chapter 18 and Chapter 19 of this book.
TABLE A6.1. C keywords associated with data type and structure definition

Word.Summary meaning.Word.Summary meaning.
charA single character, usually 8-bit.signedA qualifier applied to char or int (default for char and int is signed).
constData that will not be modified.sizeofReturns the size in bytes of a specified item, which may be variable, expression or array.
doubleA ‘double precision’ floating-point number.structAllows definition of a data structure.
enumDefines variables that can only take certain integer values.typedefCreates new name for existing data type.
floatA ‘single precision’ floating-point number.unionA memory block shared by two or more variables, of any data type.
intAn integer value.unsignedA qualifier applied to char or int (default for char and int is signed).
longAn extended integer value; if used alone, integer is implied.voidNo value or type.
shortA short integer value; if used alone, integer is implied.volatileA variable which can be changed by factors other than the program code.
TABLE A6.2. C keywords associated with program flow

Word.Summary meaning.Word.Summary meaning.
breakCauses exit from a loop.forDefines a repeated loop – loop is executed as long as condition associated with for remains true.
caseIdentifies options for selection within a switch expression.gotoProgram execution moves to labelled statement.
continueAllows a program to skip to the end of a for, while or do statement.ifStarts conditional statement; if condition is true, associated statement is executed.
defaultIdentifies default option in a switch expression, if no matches found.returnReturns program execution to calling routine, also causing return of any data value specified by function.
doUsed with while to create loop in which statement following do is repeated as long as while condition is true.switchUsed with case to allow selection of a number of alternatives; switch has an associated expression which is tested against a number of case options.
elseUsed with if and precedes alternative statement used if if condition is not true.whileDefines a repeated loop – loop is executed as long as condition associated with while remains true.
TABLE A6.3. C keywords associated with data storage class

Word.Summary meaning.Word.Summary meaning.
autoVariable exists only within block within which it is defined. This is the default class.registerVariable to be stored in a CPU register; thus, address operator (&) has no effect.
externDeclares data defined elsewhere.staticDeclares variable which exists throughout program execution; the location of its declaration affects in what part of the program it can be referenced.
TABLE A6.4. C data types, as implemented by the MPLAB C18 C compiler

Data typeDescriptionLength (bytes)Range
charCharacter1−128 to +127
signed charCharacter1−128 to +127
unsigned charCharacter10 to 255
intInteger2−32 768 to +32 767
unsigned intInteger20 to 65 535
shortInteger2−32 768 to +32 767
unsigned shortInteger20 to 65 535
short longInteger3−8 388 608 to 8 388 607
unsigned short longInteger30 to 16 777 215
longInteger4−2 147 483 648 to 2147 483 647
unsigned longInteger40 to 4 294 967 295
floatFloating point4From 1.17549 × 10−38 to 6.80565 × 10+38
doubleFloating point, double precision4From 1.17549 × 10−38 to 6.80565 × 10+38
TABLE A6.5. C operators

Prec. and orderOperationSymbolPrec. and orderOperationSymbol
Parethenses and array access operators
1, L to RFunction calls( )1, L to RPoint at memberX–>Y
1, L to RSubscript[ ]1, L to RSelect memberX.Y
Arithmetic operators
4, L to RAddX+Y3, L to RMultiplyXY
4, L to RSubtractX–Y3, L to RDivideX/Y
2, R to LUnary plus+X3, L to RModulus%
2, R to LUnary minus–X
Relational operators
6, L to RGreater thanX>Y6, L to RLess than or equal toX<=Y
6, L to RGreater than or equal toX>=Y7, L to REqual toX= =Y
6, L to RLess thanX<Y7, L to RNot equal toX!=Y
Logical operators
11, L to RAND (1 if both X and Y are not 0)X&&Y2, R to LNOT (1 if X=0)!X
12, L to ROR (1 if either X or Y are not 0)X||Y
Bitwise operators
8, L to RBitwise ANDX&Y2, L to ROnes complement (bitwise NOT)≈X
10, L to RBitwise ORX|Y5, L to RRight shift; X is shifted right Y timesX »Y
9, L to RBitwise XORXY5, L to RLeft shift; X is shifted left Y timesX «Y
Assignment operators
14, R to LAssignmentX=Y14, R to LBitwise AND assignX&=Y
14, R to LAdd assignX+=Y14, R to LBitwise inclusive OR assignX|=Y
14, R to LSubtract assignX–=Y14, R to LBitwise exclusive OR assignX=Y
14, R to LMultiply assignX = Y14, R to LRight shift assignX »=Y
14, R to LDivide assignX/=Y14, R to LLeft shift assignX «=Y
14, R to LRemainder assignX%=Y
Increment and decrement operators
2, R to LPreincrement++X2, R to LPostincrementX++
2, R to LPredecrement– –X2, R to LPostdecrementX– –
Conditional operators
13, R to LEvaluate either X (if Z≠0) or Y (if Z=0)Z?X:Y15, L to REvaluate X first, followed by YX,Y
‘Data interpretation’ operators
2, R to LThe object or function pointed to by XX2, R to LThe address of X&X
2, R to LCast – the value of X with (scalar) type specified(type) X2, R to LThe size of X, in bytesSizeof X
Key: Prec. = precedence; L = left; R = right.
TABLE A6.6. Example preprocessor directives

DirectiveSummary descriptionDirectiveSummary description
#ifUsed for conditionally compiling code, based on evaluation of associated expression. Must be terminated by #endif.#defineDefines string constants which are used in source code and are substituted before code line is compiled.
#ifdefSimilar to #if, but tests if specified symbol has been defined. Terminated by #endif.#errorGenerates user-defined error message.
#ifndefIdentical to #ifdef, but tests if specified symbol has not been defined.#includeInclude at this point the full text from file specified, which may contain unlimited C code, and is then compiled with the source program.
#elseUsed with #if to provide alternative section for compilation.#lineAllows user to specify line number within code.
#elifUsed within an #if section to test a new expression.#pragmaAllows further directive-like information to be sent to the preprocessor, generally compiler-specific.
#endifTerminates an #if section.#undefReverses the action of #define, on string constant specified.
TABLE A6.7. Some applications of punctuation marks

:Terminates a labelloop:
:Used in bit-field format (seen also in Table A6.5)unsigned RB0:1;
;Terminates a statement or declarationunsigned RB0:1;
.Denotes member of a structure or union (seen also in Table A6.5)PORTAbits.RA2 = 1;
\Next line is continuation of this one
{}Defines block of code
TABLE A6.8. MPLAB C18 additional storage qualifiers

farVariable can be anywhere in program memory.Variable can be anywhere in data memory; bank switching instruction needed to access; far and ram is the default combination.
nearVariable is in program memory, with address <64K.Variable is in Access RAM.
TABLE A6.9. MPLAB C18 memory models

Memory modelPointer sizeDefault ROM qualifierMemory size
small (default)16-bitnearProgram memory ≤ 64K
large24-bitfarProgram memory > 64K