Data Types

int variable = value;

int A = 61;

int B = 62;

int C = 63;

int A=61, B=62, C=63;

Data Types and bytes allocated for each are listed below:

char (1), int (2), short (2), long (4), float (4), double (4), long double (4), * pointer (1. 2 or 4)





Use #include <header>  or  #include "myfile".




const type constant (value);

#define PI 3.14

const float PI = 3.14;


Prevents a value from becoming a variable.


type identifier = value;


int number = 1000;

Variable types have to be declared so memory can be allocated appropriately.

You can declare several variables of the same type at once: int num1, num2, num3;

Program Organization



Global variables at the top of the program.

Setup (Arduino) contains the drivers and device assignments.

Main or Loop (Arduino) next.

Functions or Procedures follow. "Some" compilers require them above the Loop or Main function.

Parameter passing

void main(void)

int main()  

All functions assume they pass and return a value: the function is called and a parameter is passed to it from the calling function, and then the result of the program is passed back to the calling function.

If the function takes no parameters, then we usually use main(void). The data type for the function must be declared so commonly we see either int main() or void main().

Arithmetic Operators

+   addition

-   subtraction

*   multiply

/   divide

%  modulus





11 % 5



Modulus is the remainder after integer division.  11 / 5 = 2 with a remainder of 1.  11 % 5 = 1.

Compound Assignment Operators






product = product * 1.5

quotient = quotient / number

pass = pass % 4

count = count + 2

reduce = reduce - 5

product *= 1.5

quotient /= number

pass  %= 4

count  += 2

reduce  -= 5

Shift Operators

<<    Shift left


>>    Shift right

x << 1   is the same as x*2

x << 2   is the same as x*4

y >> 1   is the same as y/2

y >>2    is the same as y/4

  << = shift left. Shifting a bit to the left one position will double its value.

>> = shift right. Shifting a bit to the right one position will halve its value.

Logical Operators

&&   AND

||    OR

!      NOT


Relational Operators

>     greater than

>=   greater than or equal to

<     less than

<=   less than or equal to

==   equal

!=    not equal




Bitwise Operators

|    bitwise OR

^    bitwise XOR (exclusive OR)

&   bitwise AND

~    NOT (1's complement)

PORTB = PORTB | (1<<2);

PORTB = PORTB ^ (1 <<2);

PORTB = PORTB & ~(1<<2);

LED_Port = ~(1 << 2);

PORTB |= (1<<2);

PORTB ^= (1<<2);

PORTB &= ~(1<<2);

This operation sets bit PB2 to a 1 without disturbing the other bits in PORTB.

This operation toggles just bit PB2.

This operation clears just bit PB2.

This operation inverts the state of just bit PB2.

IF/ELSE Expressions

if (expression1) statement;

   if (expression2)



      if (expression3);




Nested IFs are supported up to 15 deep. A switch/case expression might be a better choice though.

SWITCH/CASE Expressions

switch (expression)  {

   case constant1:

   statement; break;

case constant2:

   statement; break; 


   statement; break;   }



If a matching Case is found, that process is executed, otherwise the Default process is executed.

Break forces the switch structure to terminate and exit to the next C program structure.

FOR Loop

for (init; conditional test; increment) statement;

for (i = 1; i <= 10; i++)

{ Serial.println( i ); }


Print the value for "i" ten times to the Serial Monitor.

WHILE Loop while (expression) statement; while(1)   Endless loop: while it is true, do it. It only executes if true. This is different from the DO loop below.

DO Loop

do {



while (expression);



The DO statement executes at least once before the "while" expression.


type variable [size] = {value-list};





char table [10];



char grid [2] [5];

char table [10] = {a,b,c,d,e,f,g,h,i,j};


char table [2][5] = {0,1,2,3,4},


Declares an array called table that will house ten elements of 8-bit data. In the example, they are letters, of course.



Declares a two-dimensional array of 2 rows by 5 columns. First row is 0, last is 1. First column element is 0, last is 4.

table [0][2] = 2

table [1][2] = c


type *pointer_name;

char *pAlpha;  // Pointer.

int C = 66;  // A variable value.

pAlpha = &C; // Address of C.

Serial.print("%d", *pAlpha);

// Output is 66: the contents of variable C pointed to by pointer pAlpha.


Pointers are special variables that are used to store addresses rather than values, and start with *.  Often we will see the letter p used with the * asterisk symbol: *p.

*pAlpha would be a typical example of a pointer for variable Alpha.

Example: int *pAlpha;

We refer to the location of the pointer by using the & ampersand symbol.