C language for MSX – lesson 1

da | Nov 3, 2023 | C language | 0 comments

C language for MSX

Column by Nicola Brogelli (Part 1)

Primitive types

   The C language provides developers with a series of data types called primitives and a series of operations that can be performed on them. Therefore each "object" is identified by two entities: a set of values that it can take on and a set of operations defined on it. In the following table we find the list of primitive types that the language makes available to us.
char, Character, can have the modifier signed / unsigned
int, Integer, can have the modifier short / long / unsigned
float, Decimal number
double, Double precision decimal number, can have the modifier long

Whole type

   The primitive type int represents integers in the C language. If N is the number of bits used to represent this type of data, the values that can be assumed will be represented by the interval from -2N-1 to 2N-1-1.
To know exactly how our system implements this type of data, simply use the operator sizeof (type), which will return a number representing the occupancy in bytes of the specified type.
   Observation: The short, long modifiers can be added to the int type which allow integers to be represented on a smaller or larger number of bits. Generally speaking we can state that an int can never be less than a short int and consequently a long int can never be less than an int. In particular implementations we may find that the three types will have the same memory occupation. There is another modifier called unsigned this allows you to define unsigned integer types. In this case, if N is the number of bits used to represent this type of data, the values that can be assumed will be represented by the range from 0 to 2N-1 (by default all types are defined as signed).
   Note: In this case I used the word modifier to mean that the prefix short, long and unsigned alter the representation of our integer in memory. Note that in much documentation the word modifier is not used but the term type is used directly.
In the brief introduction I specified that each "object" is identified by two entities: a set of values that the object can take on (this has already been analyzed previously) and a set of operations defined on it.
Those shown in the table are some of the operations that the C language makes available on integers:

       Mathematical operators Logical operators

Symbol Description Symbol Description
+ Sum | Bitwise OR
– Subtraction & Bitwise AND
* Bitwise multiplication ^ EXCLUSIVE OR
/ Division ~ Complement
% Rest of the division << Shift left
>> Shift right

   Let's now take a concrete example of how to define an integer type variable together with its modifiers and its contextual initialization. It is worth remembering that in C the use of a variable declared but not initialized will lead to unexpected behavior of the program, even if in modern compilers the problem is intercepted and reported during the compilation phase.
short s = 0; int i = 0; long l = 0; unsigned int = 0;

Real guy

   For real numbers there are two types of data float And double, which can offer different precision. The accuracy offered depends on the implementation of each type. Consequently, there may be the possibility that the float and double types have the same memory occupancy, but it cannot happen that the float type is more precise than the double type.
To know the actual implementation of the real types, simply use the sizeof (type) operator, which will return a number representing the occupation in bytes of the specified type.
   Observation: It is possible to add the long modifier to the double type which allows it to be further extended, thus modifying its memory occupancy.
The set of operations defined on real types offered by the C language are shown in the following table

   Mathematical operators                                                                               

Symbol Description                                                                                 
+ Sum
– Subtraction
* Multiplication
/ Division
% Rest of the division

Let's now give a concrete example of how a real type variable is defined together with its modifiers and its contextual initialization. It is worth remembering that in C the use of a variable declared but not initialized will lead to unexpected behavior of the program, even if in modern compilers the problem is intercepted and reported during the compilation phase.
float f = 2.2; double d = 2.0; long double ld =1.5;

Char type

   The guy char has as a set of values a set of appropriately encoded characters. The set of values associated with the char type are the letters of the alphabet, decimal digits, punctuation symbols and other miscellaneous symbols. Each char value is represented by its code.
To know the actual implementation of the char type, simply use the operator sizeof (type), which will return a number representing the occupancy in bytes of the specified type (normally a byte).
   Observation: The char type can be considered to all intents and purposes an integer type, whose values are generally represented in 8bit and to which modifiers can be applied signed and unsigned.
Therefore, being able to treat the char type to all intents and purposes as an int type, we could therefore deduce that the set of operations defined on int types can also be applied to the char type.

       Mathematical operators                                                                                       

Symbol Description                                                                                 
+ Sum
– Subtraction
* Multiplication
/ Division
% Rest of the division

   Let's now give a concrete example of how to define a variable of type char together with its modifiers and its contextual initialization.
char a = 'A'; char c = 65;

Constants

   To specify that an "object" is constant we must precede the type declaration with the keyword const . In this case it is mandatory to carry out the initialization at the same time as the declaration. Their value cannot be changed within the program. This check will be carried out at compile time.
const int a = 100; const double pi = 3.14159;

If the content satisfied you, here you can find it second dispensation.

   For those interested theMSX Italy Association created a R&D working group on C programming for MSX. For more info:associazioneMSXitalia@gmail.com

0 Comments

Submit a Comment

Your email address will not be published. I campi obbligatori sono contrassegnati *

Linguaggio C per MSX – lezione 6

C language for MSX – lesson 6

C language for MSX Column by Nicola Brogelli (Part 6) Arrays and Pointers Multidimensional Arrays Until now we have considered one-dimensional arrays, which required only one index to identify an element. C allows you to...

Linguaggio C per MSX – lezione 6

C language for MSX – lesson 5

C language for MSX Column by Nicola Brogelli (Part 5) Arrays and Array Pointers Arrays are sequences of variables of the same type that are stored consecutively in memory and which can be accessed using the same name...

Linguaggio C per MSX – lezione 3

C language for MSX – lesson 3

C language for MSX Column by Nicola Brogelli (Part 3) Enumerations Enums or enumerations are particular types of data that contain a list of constants, each of which is associated with an integer value. Specifically, the constant values contained...

MSXgl una libreria di giochi scritta in C

MSXgl a game library written in C

MSXgl is a game library written in C and intended for MSX computers. The goal of this library is to give C programmers the full feature set to create a high-performance game. Even though a C program can never be efficient...