Variables

Variables store data while a program is running. They follow a strict syntax for declaration and initialization, at least requiring type information and a name.

Declaration vs Initialization

If you just want to declare a variable you can do the following:

int number;

This essentially tells the compiler that a variable called number of type int exists. You might declare a variable without initializing it if you plan to assign a value later on in the program.

Initializing a variable means assigning it a value as it is declared. This value can also be the value of another variable or the return value of a function, as we saw in our temperature converter project.

int number = 10;
int number2 = number;
String text = function_that_returns_string();

Note

In C3, if you declare a local variable without initializing it, it will automatically be zero initialized. This means that if we don’t initialize our variable, the compiler will give it a default value of 0, "", false, … respective of type.

If you have already initialized a variable and give it a new value, you are assigning it that new value.

int number = 10; // Declaration and Initialization
number = 22; // Assignment

Naming Rules

In C3 there are certain rules that apply when naming variables. A variable must only contain letters and numbers, no special characters, like ? or !. There are plenty of other naming rules in the documentation. These naming rules may seem like a lot, but don’t worry, in most cases the compiler will tell you if you mistakenly didn’t follow one. Essentially all you should know is that variable names must start with a lowercase letter.

It is also convention to write variable names in snake case, which means that you write the name in all lowercase and split up longer names with underscores, like so: very_long_variable_name.

Basic Types

In C3 every variable has to have a type. The type defines what kind of data a variable can hold. A variable of type int can for example hold Integer (whole number) values. There are a lot of types in C3, but for the sake of simplicity we will just be looking at the most fundamental types.

Type Full Name Usage
int Integer The Integer type is used to store whole numbers, like 5, -1 or 0.
float Floating-point number A floating-point number can essentially store non-whole numbers, such as 1.43, 5.123 or 0.5.
bool Boolean A Boolean can store one of two values: true or false.
char Character This type stores any single character, like ‘C’, ‘3’ or ‘$’.
String String A collection of characters, like “Hello!”, “C3” or “Programming 😀”

Practice

Congrats! You now know how to declare and initialize variables in C3. What better way to solidify your knowledge than to put it to the test.

Task 1

Difficulty:

Create a variable that can hold the following value: Hello, World!.

Solution

As “Hello, World!” is a collection of characters, the only type suitable to store it is a String. In code it would like this:

String text = "Hello, World!";

Task 2

Difficulty:

Why is this code causing an error?

int number = 1.5;
Solution
The code causes an error, because int can only store whole numbers. Since 1.5 is a decimal, this does not work.
Last updated on