Arduino: Understanding the Structure of C

Arduino uses a variant of the programming language called “C”.

This post discusses the very basics of understanding the rules of C in order to get started understanding an Arduino program and being able to modify it.

Comments

Comments are included in a program to help people understand the code. The computer completely ignores the comments.

There are two types of comments, block comments and in-line comments.

Block comments are set apart by /* and */.

In-line comments are indicated by //.

/*
This is a block comment.
Everything in this block will be ignored
by the computer.
*/

//This is an in-line comment.
//The computer ignores everything after the //.

In-line comments can be used on the same line as computer code. The computer uses the code before the // and ignores everything after the //.

White Space

Instructions need to be separated by a space in order for the computer to understand that you are talking about two different words. For instance,

void setup

and

voidsetup

are not the same. This would be very confusing to the computer.

However, the C program ignores all of the other spaces between characters in the code. It does not care if the code is continued onto the next line or if the code is indented.

For instance,

void setup() {
    pinMode(LED_BUILTIN, OUTPUT);
}

could also be written like this:

void setup(){pinMode(LED_BUILTIN,OUTPUT);}

A space is needed between void and setup, but otherwise, the other punctuation is enough to let the computer know where the separations are. Extra white space makes the code easier for people to read, but the computer doesn’t care about it.

A Line of Code

    pinMode(LED_BUILTIN, OUTPUT);

In C, a line of code ends with a semicolon. This is helpful when the line of code is very long because it can be continued on the next line, like this:

int myData[] = {1,2,3,4,5,
                6,7,8,9,10,
                11,12,13,14};

However, leaving out a semicolon is a common source of errors for new programmers. Remember this and look out for a missing semicolon whenever you have an error in your code.

A Block of Code

void setup() {
    pinMode(LED_BUILTIN, OUTPUT);
}

A C program is organized into blocks of code. The start of a block is indicated by a { and the end of the block is indicated by a }.

The end of a block is not followed by a semicolon.

A common source of error is having too many or too few }. This will give you an error when you compile your code.

A worse type of error is when you have the same number of { and }, but the } is in the wrong place. Then the code will compile but it will run incorrectly.

Variables

int n = 567;
float pi = 3.1;
String name = "Bob";

Variables have four properties: a type, a name, a value, and a scope.

The code above defines three variables with the following properties:

NameTypeValue
nint567
pifloat3.1
nameString“Bob”

Variable Names

A variable can be named just about anything as long as it is follows a few rules:

  • The name must start with a letter
  • The name cannot contain a space
  • The name should not be a predefined word that is used in C

Sometimes a variable can have a simple name, like n. More descriptive names are helpful in understanding the code. The computer never understands what the variable name means. The computer just uses the name to label a section of memory.

Variable Types

Different types of variables require different amounts of memory to be reserved.

TypeExample
intinteger916
floatdecimal number3.14159
Stringtext“New Year’s Day”

Variable Scope

The scope of a variable is very important in long programs. It is much less important for the short programs that you will be starting with. You should be aware of scope but not worry about it too much early on.

Basically, the scope of a variable is the part of the code where the variable is recognized by the program. Some variables have a global scope that covers the entire program. Some variables have a scope that is limited to a block of code.

int i = 1;  //the scope of i is the entire program
int sum = 0; //the scope of sum is the entire program

void myFunction() {
    int j = 2;  //the scope of j is limited to this function
    sum = i + j;  //sum and i can be used in this block
}

j = 3;  //this is an error because j is used outside its scope

In the above example, i and sum will be recognized throughout the program, while j will only be recognized within myFunction.

Void

The keyword “void” often causes a lot of confusion to new C programmers. It would require more explanation than it is worth to truly understand why this is used.

For now, just know that functions can also have types. The function type void simply indicates that the function will not have a value and cannot be set equal to anything.

Comments

Leave a comment