Blog

Relearning MSX #26: Variables and arithmetic operations in MSX-C (part 1)

Posted by in How-to, MSX, Retro, Technology | September 29, 2015

In the previous post we saw how to print text and numbers on the screen. This time we’ll learn how to use variables and how to operate with them.

You probably know already what a variable is. It’s just some place in the computer memory that we use to store a piece of data. In C, a variable can contain data of one of several basic types. Let’s take a look at them.

Character variables – the char type

We have already seen this type in some of the examples in previous posts. The program below assigns the character ‘X’ to the character variable c and prints it to the screen using putchar():

26-1.C (Click to enlarge)

26-1.C (Click to enlarge)

If you compile and run this program you’ll see that it prints the character X on the screen and exits:

Running 26-1.COM (Click to enlarge)

Running 26-1.COM (Click to enlarge)

We already saw in chapter 18 that in C we have to declare variables before using them. In the program above, the variable declaration is the first line in the main() function:

char    c;

The variable declaration is very important because the compiler needs to reserve space in memory, and it can’t do that unless it knows what kind of data the variable will hold. In this case, we’re telling the MSX-C compiler that the c variable is of type char, which take only one byte in the computer’s memory. This also affects what range of numbers the variable will be able to hold. We’ll see this in more detail later.

If you have experience programming in MSX-BASIC you may remember that string variables have a $ sign after the name. This makes it easy to see what kind of that a variable contains. There isn’t such thing in C. The only way to know what kind of variable we’re dealing with is to look at the variable declaration.

Numeric variables – the int type

To store numeric values we normally use variables of type int (which is just short for integer). See this example:

26-2.C (Click to enlarge)

26-2.C (Click to enlarge)

Compiling and running this program prints the number 1024 on the screen, as expected:

26-2.COM (Click to enlarge)

26-2.COM (Click to enlarge)

The int type in C works exactly as integer variables in BASIC: an int variable can store values between -32768 and 32767. Assigning values outside this range to an int variable will cause it to overflow, as in the example below (in a future post we will see with more detail what overflow is and why it happens):

Integer variable overflowing. (Click to enlarge)

Integer variable overflowing. (Click to enlarge)

MSX-C also supports an unsigned integer type. It is similar to int, but it holds values between 0 and 65535. We’ll see it in another post, probably when we look at all the parameters for printf().

Standard C also support the types float and double, for floating-point and double precision values respectively. MSX-C doesn’t natively support these types, but the MSX-C Library package does include a lot of functions to work with these. We’ll see these types after we’ve learn about C structs.

Arrays

Arrays are structures used to group together several pieces of data of the same type. They have to be declared before they can be used, but the syntax is very similar to declaring any other variable. The only difference is that they take a numeric value in brackets after the array name to indicate how many elements the array can hold. See the following array declaration:

int     a[3];

The line of code above declares an array called a. This array holds three values (the number in brackets after the array name), of type int.

Let’s see a simple example program using the same array declaration:

26-3.C (Click to enlarge)

26-3.C (Click to enlarge)

This small program defines an array called a that holds three elements of type int. First, we assign the number 39 to the first element, a[0], then we assign the number 42 to the second element of aa[1], and finally we assign the result of multiplying these two to the last element, a[2]. The program then prints the value of a[2] and finishes. This is the result of compiling and running this program:

26-3.COM (Click to enlarge)

26-3.COM (Click to enlarge)

By the way, a very common mistake when working with arrays is to refer to elements outside the range declared for the array. A diagram will make this point easier to understand:

three_element_array

The declaration above (a[3]) declares an array called a that contains three elements of type int. Since the first element always has index 0, the elements in the array are a[0], a[1] and a[2]. Remember that there is no element a[3]. MSX-C will happily allow us to access array elements outside the range defined, but doing so will corrupt the program memory or even crash the computer. Don’t do it.

As in BASIC, we can also define multidimensional arrays. To do this just add additional sets of brackets to the array name. For example, the following declaration creates a 2-dimensional array that contains three rows of three int elements each:

int     a[3][3];

Look at the following example to see how this works:

26-4.C (Click to enlarge)

26-4.C (Click to enlarge)

Running this program gives us the expected value:

26-4.COM (Click to enlarge)

26-4.COM (Click to enlarge)

You can add as many dimensions to your arrays as you want. The limit is the available computer memory.

Arithmetic operations

C supports several basic arithmetic operations, very similar to those in BASIC:

c_arithmetic_operators

Arithmetic operators in C

As you can see, these operators are almost exactly the same as in BASIC, with the exception of the modulo operator which is % in C and MOD in BASIC.

Let’s see a very simple example program and the result of running it:

26-5.C / 26-5.COM (Click to enlarge)

26-5.C / 26-5.COM (Click to enlarge)

By the way, we saw earlier in this post that the range of int variables is -32768 to 32676. What would happen if the result of an arithmetical operation, such as a product or an addition, didn’t fill inside that range? Let’s find out:

26-6.C / 26-6.COM (Click to enlarge)

26-6.C / 26-6.COM (Click to enlarge)

The variable i in the program above is an integer, so its range is -32,768 to 32,767. The result of the operation 1,000 * 1,000 is 1,000,000, which won’t fit in the range for an int. This is why printing the variable’s value shows the wrong value. In BASIC this would return an overflow error, but C is much more forgiving. Like in this case, we are allowed to assign numbers that are too big to fit in a variable. This will often result in programs that don’t work as expected, though from time to time we can also use this effect to our advantage (often in games).

Summary

In this post we saw the int and char types. We learnt how to declare and access arrays. We saw the six arithmetic operators and saw a variable overflow when it is assigned a value too big for it.

In the next post…

In the next post we’ll see the arithmetic and logical operators used to work with binary values. We’ll also see some interesting properties of char variables.

 


This series of articles is supported by your donations. If you’re willing and able to donate, please visit the link below to register a small pledge. Every little amount helps.

Javi Lavandeira’s Patreon page

Leave a Reply

Your email address will not be published. Required fields are marked *


Warning: Illegal string offset 'share_counts' in /www/javi_lavandeira/lavandeira.net/ftproot/htdocs/wp-content/plugins/simple-social-buttons/simple-social-buttons.php on line 477