June 3

Building Blocks of Programming Languages

  • Four basic building blocks of programming languages:
  1. Expressions
  2. Statements
  3. Statement Blocks
  4. Function Blocks

1. Expressions

  • Expressions in computer programming have the same definition as expressions in math: they are a combination of an operator and its operand(s). In keeping with the mathematical definition of an expression, they are well-defined, meaning that an expression must ultimately resolve to a value.
    • An operator tells the computer to perform some kind of mathematical or logical manipulation and is performed on one or more operands
      • Examples:
        • a + b
          • + is the operator; a and b are operands
        • x – 2
          • – is the operator; a and b are the operands
        • a < b
          • < is the operator; a and b are the operands
      • With the above examples, two operands are in play, which is why you’ll hear them referred to as binary expressions
        • As a result, binary expressions use binary operators; binary operators operate on two operands
      • Operator Classification:
        • Operators can be classified based on the number of operands they perform their operation on:
          • Unary Operators
            • Take one operand
            • Example: & (address-of operator); see Pointer tutorial
          • Binary Operators
            • Operate on two operands and are by far the most common
            • Examples: +, -, <, =, etc.
          • Ternary Operators
            • Operate on three operands
        • Operators can also be classified based on the kind of function they perform:
          • Arithmetic (math) Operators:
            • i.e. Operators that perform math
            • Examples: +, -, /
          • Relational Operators:
            • Compare the values of two operands
            • Examples: >, <, ==
            • Return/resolve to a boolean: true (1) or false(0)
          • Logical Operators:
            • Combine logical expressions
            • Examples: && (AND), || (OR)
            • Return/resolve to a boolean: true; false

2. Statements:

  • Statements are syntactically complete instructions
    • In C, syntax dictates that all statements end with a semicolon (this semicolon is known as a statement terminator)
  • Example:
    • Variable assignment:
      • a = 4;
        • This is a syntactically complete instruction; note how it is simply an expression (a = 4) consisting of the assignment operator (=) with the operands a and 4, and terminating with a semicolon as required by C. By syntactically correct, we simply mean that the instruction complies with the rules of the language (i.e. syntax).

3. Statement Blocks:

  • Statement blocks group statements together so they act like a single statement (i.e. the statements act together as a block)
  • In C, statement blocks start with { and end with }. In Python, statement blocks are controlled by indentation. This is why whitespace matters in Python but not in C.
    • The code inside the statement block is known as the statement block body
  • Example:
Example of statement block

4. Function Blocks:

  • Function blocks are blocks of code that accomplish a single task
  • Functions allow you to reuse code so that if you need to do the same thing multiple times, you simply call the function tag wherever you need it; you separately define the function (with its function block) elsewhere in your code
    • This makes it easier to maintain your code since, if you need to update your function, you only have to update the code once in the function itself and not multiple times wherever the function was called
  • Functions also help when working on big projects with multiple developers by acting as a black box
    • Good functions act like a black box in the sense that you don’t have to waste your time, brainpower, or memory knowing exactly how the code inside the function (it’s function block) works; you just have to know that for a given input, you get a given output
    • This is also the basis of the idea behind libraries- you don’t have to know exactly how something is done. You just call the function (written by someone else) from the library; this forms the very basis of abstraction which allows for collaboration
  • Functions also help for code readability; instead of mentally having to parse out multiple lines of code you can look at the function name (like verifyPhoneNumber() ) and know that it verifies the phone number.
    • Example:
      • int addNum(int a, int b){//Function block here }
April 25

Variables, Pointers, and Indirection in Arduino C

Before we continue on with learning about the I2C protocol and our EEPROM project, we need to discuss variables: what they are and what goes on behind the scenes. Knowledge of how variables work and the use of pointers and indirection with arrays will serve us well when it comes time to read from our EEPROM. Let’s begin.

Anatomy of a Variable:

1. What is a variable?

Simply put, variables hold data. More specifically, a variable holds data of a specific data type. For example, an int holds an integer, a string contains a collection of chars, etc.

2. What goes on behind the scenes when a variable is defined and when it is assigned?

When you define a variable, the compiler goes and checks the symbol table (basically a list of variables that have previously been declared) to see if that variable already exists. If it doesn’t, the compiler goes ahead and adds the new variable to the list.

Say, for example, you add the following statement:

int myVar;

Since our variable has not already been declared (it doesn’t already exist in the table), the compiler updates the symbol table so it now looks like this:

Symbol table with myVar declared (but not yet defined) since it lacks a location in memory (lvalue).
Symbol table after myVar declared- note the lack of an lvalue. This is because myVar is not yet defined. rvalue is also unknown because we haven’t assigned a value to myVar yet.

Now, technically, the variable has only been declared at this point- it’s missing an actual location in memory. To get this location in memory, the compiler requests a place to put this variable from the system’s memory manager. The memory manager then responds with a memory address which the compiler then adds to the symbol table for that variable. This memory address is known as an lvalue (lvalue = location value) and it merely represents where the variable can be found in memory. With this addition of the lvalue to the symbol table, our variable is now defined:

myVar now defined in the symbol table (myVar now has an lvalue).
Symbol table with myVar defined- this means that the variable now has a location in memory (lvalue).

With our new variable defined, we can now move on to storing a value in it. Fortunately, assigning a value to a variable is rather straightforward. When we assign a value to a variable, we directly navigate to the variable’s location in memory (the lvalue) and update the memory at that address with the new value. The data that’s actually stored in memory is known as the rvalue (rvalue = register value).

Continuing our example with the following assignment statement:

myVar = 10;

With this assignment, our symbol table now looks like this:

myVar after rvalue assignment
Symbol table after assignment- note the updated rvalue which holds our data value.

Another way to visualize what we have just gone over is with an lvalue-rvalue diagram:

lvalue-rvalue diagram for a value type variable
lvalue-rvalue diagram

This diagram is why you will see some people refer to the memory address as the “left value” and the actual data value as the “right value”.

  • There’s also an important caveat here: in Arduino, and C in general, there is no duty to clear that rvalue at our variable’s lvalue when we define it. Therefore you should always assume that a variable’s value contains whatever garbage was originally in that memory location unless we’ve explicitly assigned a value to the variable. (i.e., Don’t assume it’s 0 or null). Therefore it’s probably best to go ahead and initialize your variable with a value when you define it.
    Let’s summarize: Whenever your program needs to use the value stored in a variable, it uses the variable’s lvalue to go to that memory address and retrieves the data (rvalue) from that memory location.


Now that we’ve covered what variables are and how they really work, we’re ready to understand pointers. Simply put, a pointer is nothing more than a variable that references the memory address of another variable. Using the terminology that we’ve just learned, a pointer is a variable whose rvalue is the lvalue of another variable.

To visualize this, let’s take a look at two lvalue-rvalue diagrams representing the value type variable myVar and the reference type variable myPointer:

myPointer referencing myVar - Notice how the rvalue of myPointer is the memory address of myVar.
myPointer referencing myVar – Notice how the rvalue of myPointer is the memory address of myVar.

Declaring a Pointer:

Declaring a pointer variable is rather straightforward:

int *myPointer;

The type specifier (int in this case) must match the data type of the variable the pointer is to be used with. The asterisk indicates to the compiler that myPointer is a pointer. Since whitespace doesn’t really matter in C, the asterisk can be placed anywhere between the type specifier and the pointer variable name so you will sometimes also see: int* myPointer, int * myPointer, etc.

The Address-Of Operator:

By itself, a pointer that is defined but does not actually point to anything is a pretty pointless pointer (ha!). To point it to the memory address of another variable we simply need to assign the pointer the memory address of that variable. But where do we get the memory address from? That is, where do we get the lvalue of myVar from? Enter the address-of operator (&).

The address-of operator is a unary operator that returns the lvalue of a variable.

Pointer Assignment:

To point our new pointer at the memory location of our value type variable, myVar, we simply call the following statement:

myPointer = &myVar;

This completes the link shown in the previous diagram and is known as referencing. It is for this same reason that the address-of operator (&) is also known as the “referencing operator“.

Whenever you are learning a new concept, it’s a good idea to try it out yourself to prove to yourself what you’ve read. Let’s mock up an example of what we’ve learned so far in the Arduino IDE:

void setup() {
  int myVar = 10;  // Initialize a variable.
  Serial.print("myVar's lvalue: ");
  Serial.println((long) &myVar, DEC);  // Grab myVar's lvalue
  Serial.print("myVar's rvalue: ");
  Serial.println(myVar, DEC);
  int *myPointer;   // Declare your pointer.
  myPointer = &myVar; //Assign myVar's memory address to pointer.
  Serial.print("myPointer's lvalue: ");
  Serial.println((long) &myPointer, DEC);  //myPointer's lvalue
  Serial.print("myPointer's rvalue: ");
  Serial.println((long) myPointer, DEC);  //myPointer's rvalue

void loop() {

Watching the serial monitor, what you should see is something like this:

Serial log showing that the rvalue of a pointer is the memory address of the value type variable it references.
Note that the rvalue of myPointer is the same as myVar’s lvalue.

Notice that myPointer’s rvalue is the memory address of myVar (i.e. myVar’s lvalue), just like it shows in the diagram.

Indirection (Dereferencing):

We just saw that a pointer can reference a location in memory by assigning that pointer a variable’s memory address using the reference operator (&). We can take this a step further and obtain the actual value stored at that memory address by dereferencing the pointer. This is also known as indirection and is accomplished via the indirection operator (*) with your pointer. Example:

*myPointer = 5; // Go to memory addressed stored in myPointer's rvalue (myVar's lvalue) and place the value 5 in that memory address.

Continuing off our previous Arduino code example:

void setup() {
  int myVar = 10;
  Serial.print("myVar's lvalue: ");
  Serial.println((long) &myVar, DEC);
  Serial.print("myVar's rvalue: ");
  Serial.println(myVar, DEC);
  int *myPointer;
  myPointer = &myVar;
  Serial.print("myPointer's lvalue: ");
  Serial.println((long) &myPointer, DEC);
  Serial.print("myPointer's rvalue: ");
  Serial.println((long) myPointer, DEC);

  Serial.println("Updating *myPointer = 5");

  Serial.print("myPointer's lvalue: ");
  Serial.println((long) &myPointer, DEC);
  Serial.print("myPointer's rvalue: ");
  Serial.println((long) myPointer, DEC);

  Serial.print("myVar's lvalue: ");
  Serial.println((long) &myVar, DEC);
  Serial.print("myVar's rvalue: ");
  Serial.println(myVar, DEC);


void loop() {
dereferencing the pointer and assigning a value; we are able to manipulate the data stored in myVar
Notice that by dereferencing the pointer and assigning a value, we are able to manipulate the data stored in myVar.

Notice that nothing changed to myPointer at all (blue). Neither its lvalue nor its rvalue changed. Contrast that with myVar (red) which had it’s rvalue changed to 5 by the indirection operator we applied to our pointer.

That is the power of pointers and indirection. In my next journal entry, I will discuss pointers and arrays which will then allow us to finally move on to the last part of our EEPROM I2C project!

February 2

How To Update/Install FileZilla on Ubuntu

FileZilla is an incredibly useful FTP client for transferring files between your workstation and servers. In this tutorial, I will walk you through updating/installing FileZilla on Ubuntu without using the repository. In general, if you want the latest and greatest features, try to avoid repositories- the apps in repositories are often outdated. I also feel like a repository is a crutch in that it obfuscates how your software is actually installed on your Linux system.

I can already hear the outrage now; I’m not saying that repositories are worthless. They greatly reduce maintenance when it comes to keeping your system (relatively) up-to-date. Downloading, extracting, and compiling every application from source would be hugely impractical. I use repositories for things that either I don’t use very often or that I don’t care about having the latest version of. For apps that I use often, where I care about having the latest, I handle those manually. Now, on to the tutorial.

1. Obtain your update files.

Obtain your update files. If you already have FileZilla installed, FileZilla checks automatically for updates at launch and downloads them to your home downloads folder. If you don’t have FileZilla already, download it here.

2. Navigate to your downloads folder.

Navigate to your Downloads folder and find your FileZilla tar file. I’ll admit I use the Files GUI app that comes with Ubuntu most of the time. Right click and select “Open in Terminal” (or just open a terminal with Ctrl + Alt + T and just type cd ~/Downloads/).

3. Extract your tar file.

Extract your tar file using the following command:

tar -vxjf FileZilla_3.40.0_x86_64-linux-gnu.tar.bz2

This will extract the file to a directory in your Downloads directory called FileZilla3. You should now have the following:

Extracted FileZilla3 files in ~/Downloads/ directory.
Extracted FileZilla3 files in ~/Downloads/ directory.

Notice this extraction contains a bin directory, implying that it’s ready to run (no compilation necessary).

4. Move your extracted files to their final location.

Let’s move this folder to our /opt/ directory with:

sudo mv ./FileZilla3/ /opt/

But wait! If you’ve already, installed, you’ll get the following error:

mv: cannot move './FileZilla3/' to '/opt/FileZilla3': Directory not empty

Even with sudo, mv will refuse to merge a directory. It’s a nice guardrail. In our case though, we do want to merge. For that, we’ll use rsync:

sudo rsync -a ./FileZilla3/ /opt/FileZilla3/

Warning: DO NOT FORGET to add the /FileZilla3/ directory to /opt/ like it shows above. If you simply did /opt/ you’d wipe out your entire /opt/ folder and be left with only FileZilla3. 

And with that, we’re done!

Category: Linux | LEAVE A COMMENT