EXTRA: February 2007

Subject 01: DICE : My First Steps
Subject 02: DICE : Complete Amiga C
Subject 03: DICE : The Editor
Subject 04: DICE : Danger, Will Robinson! K&R syntax alert!
Subject 05: DICE : Failed ReturnCode 20
Subject 06: DICE : Analysing a Simple Program
Subject 07: DICE : Fun with Numbers
Subject 08: DICE : More Fun with those Numbers
Subject 09: DICE : Even More Fun with those Numbers
Subject 10: DICE : Floating Point
Subject 11: DICE : Decision Making
Subject 12: DICE : IF

Entry 0730: Blogs: 12


DICE RETROSPECTIVE: February 2007

Entry 0730: Blog: 1


DICE: My First Steps

Walked into the postal collection depot and even before I
got to the glass counter Mr Postie was searching the 
usual shelf for my stuff. He suggested I might get a 
rubber stamp to save me having to keep signing for
parcels.... 

Anyway in the parcel was my VERY LARGE book and
disks for the DICE software from Obvious Implementations
Corporation.. for the Amiga Personal Computer. Version 3
no less... For technical support Compuserve: Go Amigav
DICE user manual for March 13th 1994. 

Here are some names for you..

Dave Baker, Matt Dillon, Andy Finkel, Bryce Nesbitt,
John Mainwaring , Mark Schnell and John Toebes.
... reads like a who's who Amiga side early 90's

Also in the batch was AmiTCP/IP by good old Village Tronic
plus disks. ...

Sana-II network device driver specification released by
Commodore Amiga Networking Group.

DICE RETROSPECTIVE: February 2007

Entry 0730: Blog: 2


Complete Amiga C
Back to DICE tonight and a happy few hours
with my Complete Amiga C book, which comes
with a registered version of DICE. The four
DD disks ( that's double density ) are copied
as XDCC1-4 on the hard drive and an assign
written to the user-startup. You are then
ready to rock and roll. This book is great
cus it assumes you know nothing. And so tonight
I have been through:

What Complete Amiga C will teach you
How to use the book
What is DICE
Amiga libraries and includes
Processors and memory
Programming language
Functions and sub-routines

We then move on to the installation and
preliminary usage... Tomorrow me thinks.

Amazing to think this is all on a few 
DD disks. That's the great thing about 
the Amiga... it tends to do what's 
written on the tin... and no messing.
Straight in at the deep end, and you
pretty well get the whole package. 

DICE RETROSPECTIVE: February 2007

Entry 0730: Blog: 3


The Editor
Lesson 1 THE EDITOR

With DICE set up on the computer you can begin
by calling up the DICE editor from the SHELL
with the line 

dme 

Took me a while to realise where the file
example "hello" was and realised that everything
I did was going into RAM:T so I copied the
hello.c into RAM:T and called it up from
there...

cd RAM:T

Using the F9 key saves the work in the editor.

It is standard practice to add .c to the end
of C source code files.

To run the file enter

run hello.c

What is very useful is that you can run the
editor and compiler together at the same time
so that you can check then fix errors as you
go along.

To compile a program with DICE simply type 

dcc hello.c

This will cause DICE to go off and compile 
and link your program, producing an executable.
i.e. a runable program with the same name
minus the customary .c. What happens when you
fire this up from say DOPUS is that you get
the ENTER ARGUMENTS for "hello" which on
return executes the file.

You can then make DICE produce an executable
with a different name by using the -o option.

dcc hello.c -o brian

This will create an executable called brian

If you just want to compile and not link this
to an executable you just add -c option to
the command line 

dcc -c hello.c

This produces an object file called hello.o

and if you want to compile a program and
link it to a library you can type...

dcc program.c hello.o -o program

which will compile program and link it
to library hello and produce and 
executable file - program....

Clear as mud. I now have a RAM T drawer
full of hello.c, hello.o and hello files
all doing different things. When you try
to edit the execute it is filled with
gibberish...

The file hello.c had the lines


#include 

main(ac, av)
char *av[];
{
    puts("HELLO WORLD!");
}

Which when executed stated simply

HELLO WORLD!


I think I am getting to understand matters..

We move on next to Basic C programming...

Whoo hooooo.c or is it whoo hooo.o

DICE RETROSPECTIVE: February 2007

Entry 0730: Blog: 4


Danger, Will Robinson! K&R syntax alert!
Paul writes

> #include 
>
> main(ac, av)
> char *av[];
> {
>     puts("HELLO WORLD!");
> }

Danger, Will Robinson!  K&R syntax alert!
I guess this book is a little old...  Or maybe I should 
say retro :-)

K&R (Kernighan & Ritchie) invented the glorious C language.  
However, it's gone through a few revisions over the years.  
One of which is changing the way functions (like your 'main') 
is defined.  The more proper way would be:

int main (int ac, char **av)
{
   puts("HELLO WORLD!");
}

> Which when executed stated simply
>
> HELLO WORLD!
>
>
> I think I am getting to understand matters..
>
> We move on next to Basic C programming...
>
> Whoo hooooo.c or is it whoo hooo.o

Well done!  I suggest you work your way through the 
basic console stuff.  It'll pretty much work on any 
platform, including Windows.

If this book deals with Amiga GUI stuff and it's from 
the 1.3 era I'd avoid it and look for some kickstart 2.x 
tutorials, particular the gadtools.library.  1.3 was 
a real pain.

DICE RETROSPECTIVE: February 2007

Entry 0730: Blog: 5


Failed ReturnCode 20
> This is the very very small program I
> refer to in my next post...
>
> #include 
>
> /* program to print a message on the screen */
> void main()
> {
>
>     printf("Hello from planet C\n");
>
> }
>
> This one saved to Workbench for some reason.
> When I execute it displays this ...
> er... I saved the program as planet.c
> then compiled it to become planet
>
> Hello from planet C
> planet failed returncode 20
>
> Why failed returncode 20

main() is a special function in C.  It's where the 
program starts, and ends.  On the Amiga main() should 
return a value to the caller
(normally the CLI/Shell).

So, if we change your program a little:

#include 
/* program to print a message on the screen */
int main()
{
   printf("Hello from planet C\n");
   return 30;
}

Notice I changed the 'void' to 'int'.  That's because 
'void' means 'this function doesn't return anything' 
and 'int' means 'this function returns a number'..

It should now say planet failed returncode 30.

You can use this return code.  For example, suppose 
you wrote a program to detect if a mouse button was 
pressed or not.  Pressed would return 5, not pressed 0.  
Stick this in your startup-sequence and the shell can 
take a special action, like run a command or something.

DICE RETROSPECTIVE: February 2007

Entry 0730: Blog: 6


Analysing a Simple Program
Tonight... Analysing a simple program

You are prompted to run the editor and
enter the following...

#include 

/* program to print a message on the screen */
void main()
{

    printf("Hello from planet C\n");

}

The first line tells the compiler that we want
to use some code that has already been written.
This is collected in the ' header file '

The file is held within the angles brackets stdio.h
Included for printing information on the screen

The next line has to be left blank.. So far as
white space this is all treated the same by C.
It does not distinguish between a space character,
a tab or return character.

The next line is a comment. /* */

Anything between is ignored by the compiler

After the comment comes the function definition
The function is called main. Every C program
has a function with this name. The computer
must be told which function is first the top
level or main. The word void before the 
word main indicates that the function does
not produce a result. The parenthesis after
the function name lets the compiler know
that it is a function that is being defined.
And the fact that there is no space between
them indicates that the function does not
require an input.

The open and closed curly brackets bound the
contents of the function. Reported speech must
be enclosed in inverted commas.

The statement that actually does something is
printf.  This is a pre written function that
writes information to the screen. The f stands
for formatted. Its definition is provided
by the inclusion of stdio.h

\n are instructions that tell printf to print
a new line character

Please note the semi colon at the end of printf
This marks the end of the statement for the
compiler. ... This can be the end of a block
denoted by the closing curly brace.

That's the end of my first program... Complicated
or what.

DICE RETROSPECTIVE: February 2007

Entry 0730: Blog: 7


Fun with Numbers
Its fun with numbers tonight and variables.
I got real stuck... cus the compiler kept
throwing up errors on every line until I
noticed I had put a full stop in the wrong
place... duh !



#include 

/* program to total four numbers */
void main()

{
    /* first declare variables */
    int first, second, third, fourth, total;

    /* set the total value to zero */
    total=0;
    /* now assign arbitrary values to the other variables, 
in a real-world example these values would be entered by 
the user running the program */
    first=23;
    second=-5;
    third=42;
    fourth=1;
    /* now perform the addition to find the total */
    total=total+first;
    total=total+second;
    total=total+third;
    total=total+fourth;
    printf ("The total is ");
    printf ("%d\n",total);

}

The line defining the variables could have
been written:

int first;
int second;
int third;
int fourth;

and in contrast the total could have been
achieved by writing:

total=total+first+second+third+fourth

also the printf statement could have been
written..

printf ("The total is %d in total",total;

I am now getting my head around operator precedence
in so much that the maths works from left to right
unless multiplication is involved which takes
priority.

Struggling with 42%23 giving 19 at the moment, the
remainder of 42 divided by 23. I appreciate why
42/23 gives 1 and not 1.826 but the 42%23 that's
confusing.

Operators are your times by, divided by, added etc
This % modulus which finds the remainder... As I
say I can't see how 42%23 makes 19...

Next up floating point.


DICE RETROSPECTIVE: February 2007

Entry 0730: Blog: 8


More Fun with those Numbers
From the book...

If you try mixing several different operators
on the same line, you might notice some odd
results. For one thing, there is the 
consequence of the integer division as
mentioned earlier, meaning that the expression
third/first (ie 42/23) would give the result 1,
rather than 1.826. To compensate for this 
C provides an operator called the "modulus"
that will find the remainder. It is written
as a percentage sign and used just like the
operators already discussed. For example
the expression 42%23 would give the result 19,
the remainder of 42 divided by 23. 

     That last bit was the confusing bit.
I think however that you are right. If the
number was 88 and 88%23 applied I think the
result would be still 19... And yes if you
divide 88 by 23 and times the sum beyond 
the decimal point by 23 you get 19. Its
a simple case of transposing equations.
Thank goodness for math...

DICE RETROSPECTIVE: February 2007

Entry 0730: Blog: 9


Even More Fun with those Numbers
> Struggling with 42%23 giving 19 at the moment, the
> remainder of 42 divided by 23. I appreciate why
> 42/23 gives 1 and not 1.826 but the 42%23 that's
> confusing.
> 
> Operators are your times by, divided by, added etc
> This % modulus which finds the remainder... As I
> say I can't see how 42%23 makes 19...
> 

Idiots... Its not the remainder of 42 divided by
23... Its the remainder times 23. Cus of whole
numbers 42 divided by 23 gives 1 and not 1.826.
To compensate C creates the modulus which enables
a figure to be achieved to represent this figure
42%23 which establishes the difference between
the whole number and the fraction thereof, in this
instance .826 and multiplies it by the 23 to
give a value of 19. I get it now.

I just logged that into the program and ran it
and I do infact get 1 for 42/23 and 19 for 42%23.

I am using Cygnus ED for writing my programs
and then compiling with the SHELL with the
command dcc i.e. dcc total.c  I then check this
by going into Dopus and executing the program.

I am also using the Right-Amiga shortcuts a lot...

RA-c copy
RA-x cut
RA-v paste

dme in the SHELL gives the DICE editor. I prefer CED.

DICE RETROSPECTIVE: February 2007

Entry 0730: Blog: 10


Floating Point
Moving on with Amiga C I am now into 
floating point and also linking the 
maths library.

Floating point allows C to handle real
numbers with as you would appreciate a
floating point. This is defined by the
keyword 'float' 

So now when we declare our variables we
can add float to the statement. Note that
any number that is assigned needs the
decimal point. So 0 becomes 0.0

In the example below we also introduce
the scanf which unlike the printf requires
an input from the user... So you enter a
number on request. To tell the scanf that
it is dealing with a floating point
variable we use "%f" and the second 
parameter to be stored must include the '&'

The additional complication here is that
when compiling you need to link the 
maths library. Now I mistook -lm to be
-1m ( minus one m ) and I was an age
sorting that out....

To compile you enter 

dcc float.c -lm -o float

Where float is the name of the file you
created... As below. You can call it anything.
Then when you run the program you are
asked to enter the values, and like magic
the total is the sum there of...

#include 

/* program to total four non-integer numbers */
void main()
{

    /* first declare variables */
    float first, second, third, fourth, total;

    /* set the total value to zero */
    total=0.0;
    /* now get the numbers to be added from the user */
    printf("Enter the numbers to be added\n");
    printf("Enter the first number ");
    scanf("%f",&first);
    printf("\nEnter the second ");
    scanf("%f",&second);
    printf("\nEnter the third ");
    scanf("%f",&third);
    printf("\nEnter the fourth ");
    scanf("%f",&fourth);
    
    /* Now perform the addition to find the total */
    total=total+first+second+third+fourth;
    printf("\nThe total is %f\n",total);
}
    
Well I am now at the end of that chapter...

Next up ' Decision Making ' and reducing
statements to true or false.

DICE RETROSPECTIVE: February 2007

Entry 0730: Blog: 11


Decision Making
Time for decision making.... and more
important the concept of true or false

This time by example of a program for
a simple calculator...

#include 

/* simple calculator */
void main()

{
    /* declare the variables */
    float first, second, result;
    int reply;

    /* get the user to enter the numbers */
    printf("Enter the two numbers to be opened on \n");
    scanf("%f",&first);
    scanf("%f",&second);
    /* print up the menu and get the users choice */
    printf("Which operation do you require?\n");
    printf("1 - addition\n2 - subtraction\n3 - multiplication\n4 - 
division\n");
    scanf("%d",&reply);

    /* now to make the decision */
    switch (reply) {
        case 1: result=first+second;
            break;
        case 2: result=first-second;
            break;
        case 3: result=first*second;
            break;
        case 4: result=first/second;
            break;
        default:
            break;
    }
    printf("\nThe result is %f\n",result);
}

A statement can be true or false. The computer
has no concept of being partially true. Therefore
decision making is asking the computer if
something is true. To establish this we enter
a variable, which could be assumed as a reply.
This is a variable under which the users input
is stored. Imagine a simple calculator program
as above where the user is asked to input
two variables and then asked what to do with
those variables... 

The 'switch ' statement means switch execution
to one of the following groups of statements,
depending on the result of the expression
inside the parenthesis. The expression in this
case is simply the variable 'reply'. Note that
the body of the switch statement is in curly
braces... this means that the body constitutes
a statement block.

Each of the lines beginning with case is an expression
And so if the number following 'case' is the same
as the variable inputted by the user the the
statement following case is executed. 

We introduce here the concept of 'break' cus it
tells the computer to break out of the switch
statement and ignore all other statements in
the block.... then to continue the next
statement, in this case printf.

Evidently you must include a break after default
in the statement. Default is used to catch any
unexpected result. 

In essence the switch is a good way of responding
to a menu selection.

As ever I created the above in CED then saved as
calculator.c then I opened the SHELL and compiled

dcc calculator.c -lm -o calculator

And then I run from DOpus by simply clicking on the
compiled file calculator. The name can be anything
you want to call it.

DICE RETROSPECTIVE: February 2007

Entry 0730: Blog: 12


IF


Today I was looking at the IF statement and particularly 
the two == next to each other.

#include 

/* Quick demo of if */
void main()
{
    int reply;
    printf("Enter the code number\n");
    scanf("%d",&reply);
    if (reply==999)
        printf("Correct\n");

}

In this example the program tests whether the 
entered number is the same as the reply variable 
within the program. Simple enough. An equals sign 
on its own is used to store the result of an 
expression. Evidently confusing = and == is a
common error. 

The comparison as a whole, made up as it is of two 
expressions and the test for equality ( known as a
'relationship operator ', since it examines the 
relationship between two expressions ) can also 
be viewed as an expression. This expression can
have only one of two logical values. Either true 
or false. Falsity is represented by the number 0, 
any other number is equivalent to truth. .... Now 
the brain tickling statement....

In other words, you could substitute a simple 
expression for a logical one involving a relationship 
operator...



Well that's your lot for February. Next DICE update will
be March. 


scuzz site

If you can only see this CONTENT window
then click the image above for the full site

Last updated 4th February 2007

Chandraise Kingdom