Welcome, Guest. Please login or register.

Author Topic: How to program: In one easy step... Learn?  (Read 11160 times)

Description:

0 Members and 1 Guest are viewing this topic.

Offline Karlos

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16879
  • Country: gb
  • Thanked: 5 times
    • Show all replies
Re: How to program: In one easy step... Learn?
« on: February 19, 2003, 12:57:13 PM »
Quote

Atheist wrote:

So if I put "int argc"  as  "int 3"
It would accept up to 3  words or numbers, in addition to the name of the cammand, from the shell prompt? Or from being called from another program.



That, my good man would be a syntax error. When you define any function C (just as many other languages) the definition says only what types of variables are handed to the function, not what the allowed values are. It's up to you, when you write the guts of the function how it deals with unwanted values etc.

Quote


char*argv

This looks like you are multiplying the terms.
Or, are variable names supposed to be placed between the square brackets? I don't get it.


Unfortuantely thats C pointer syntax for you. What 'char* argv[]' actually says is 'argv is a pointer to an array of characters'.
Arrays and pointers in C are also pretty much interchangable. This means that you could also declare it as 'char** argv', which means 'argv is a pointer to pointers to characters'.
Don't worry about it too much yet though...

Here's a simple program (off the top of my head so iut may have bugs) you could try to compile...

/*
   A simple C program to show arguments passed
   to main()
*/

#include

int main(int argc, char* argv[])
{
   int i;
   puts("Here's what was passed to main()");
   for (i=0; i   {
      printf ("Argument %d was '%s\'n", i, argv);
   }
   puts ("All done!");
   return 0;
}

Assuming it works and you compiled it as testprog, try running it in a shell as follows

testprog bollocks whatever

Hopefully you'd get something like

Here's what was passed to main()
Argument 0 was 'testprog'
Argument 1 was 'bollocks'
Argument 2 was 'whatever'
All done!

as your output.

Quote

Well, at any rate, which C. People say C, C++, then there's ANSI C (and maybe ANSI C++). Is ANSI the name of a company, like Storm is?

I've always considered taking a chance at using it.


Well, ANSI is a standard (Amierican National Standards Institute). Pretty much all modern C compilers conform to this standard so don't worry. It defines a few ground rules that make sure a simple program (such as the one I did earlier) should work everywhere.
C++ is a little more of a mixed bag when it comes to standardisation.

Hope this was useful ;-)
int p; // A
 

Offline Karlos

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16879
  • Country: gb
  • Thanked: 5 times
    • Show all replies
Re: How to program: In one easy step... Learn?
« Reply #1 on: February 19, 2003, 02:01:54 PM »
Quote

Eniodis wrote:
...
But... You must work on a Pal Screen :-(

Eniodis


...and nothing you ever write will work the moment you shove an accelerator, extra memory, hard disk, change your workbench pattern prefs (joke) etc.

Seriously though, if you want to go along a BASIC route, there are much better implementations available than any incarnation of AMOS I ever saw.
True the editor may be user friendly but there are plenty of source editors available that are just as, if not more  powerful.

Ok, I promise not to keep flaming AMOS ;-)
int p; // A
 

Offline Karlos

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16879
  • Country: gb
  • Thanked: 5 times
    • Show all replies
Re: How to program: In one easy step... Learn?
« Reply #2 on: February 20, 2003, 09:46:50 AM »
Quote

Atheist wrote:

I don't know what you mean by the extra memory. Amos worked when I only had chip ram, and it worked when I got both. I don't have the compiler thogh, but am getting it soon. I know it's free, but I found a store that had it on the original disks, and am waiting to see if it's still available. (Professional Comp)

Are you saying that when Amos Professional, compiles a program, it won't work on what 020, 030...?



Basically, my experience of AMOS has not been a good one. Any Amos program I've ran, be it old custom-chip hacky game style or intuition friendly has fell over at the slightest provocation.
This has been the case on at least three accelerated systems I used (030, 040 and 040/PPC). And yes, one program that used to work in 16Mb 70ns ram crashed continually with 64Mb 60ns ram. Maybe the speed was too much for it, but I don't know. When the machine just restarts on you, you don't get much debug info ;-)

-edit-

Anyhow, me old mate, back to C (neary forgot to answer your other questions)

First off, the first string argument passed to the C main() function is indeed the program name. You didn't misread that.

As for argv, you could indeed have changed the name to 'word' or whatever you wish, just as long as you use the same name in the code, eg

int main(int numberOfArguments, char* argumentString[])
{
/* use numberOfArguments and argumentString
somewhere in here*/
}

would be perfectly OK. It's just that 'argn' and 'argv' are kinda conventional.

As for output, I did use puts() and printf(). There is a reason for this.
Basically puts() is your no-frills console output command that writes a string, complete with a newline ('\n') character on the end. Thats all it can do.
The other function, printf() is a complex beastie that can print formatted strings (hence the 'f' in printf).
Going into the detail of formatted output would take ages. But, just to give an idea:
The string we pass to printf() can embed special codes, prefixed with a percent sign. When the printf() function finds one of these, it substitutes it with the corresponding argument. For example

printf("An integer : %i\nA float : %f\n", 10, 5.5);

should produce the following

An integer : 10
A float : 5.5

as output. There are options that pad, justify, enforce sign, notation conventions etc.

As a final point, printf() doesn't stick a newline on the end of a string by default like puts() does, so you'll see I had to stick the '\n' character in the string myself.
int p; // A
 

Offline Karlos

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16879
  • Country: gb
  • Thanked: 5 times
    • Show all replies
Re: How to program: In one easy step... Learn?
« Reply #3 on: February 20, 2003, 10:48:48 AM »
Hi again,

Quote

Atheist wrote:
@ Karlos

1) I find it surprising that the first string is the name of the program itself. Was that correct?

2) I don't get this stuff; I see
For____without a___next
Return___without a____gosub
Return 0____0 what?

You see, it's too cryptic, I didnt' know "argc" was a name for a number, and that I could have put in its place "word_number". I did realize it meant integer though.

3)i++____increment by 1
i++2_____increment by 2?

4) So, is there an ANSI C++?


1) Yep its true - I think I detailed that in the last post. The name of the executable you ran in the shell is passed as the first string. This implies that there is always at least one string passed to main, so the value of 'argc' should always be at least 1.

2) For/Next
In C/C++/Java, your basic For/Next loop construct has the following syntax

for (; ; )
{

}

sets up the inital values etc in the loop. We can initalise counters and stuff here.

is the criteria that must be true to continue looping.

is an event to do in each loop (usually increase your counter etc).

is the code to execute in each loop.

There is no 'Next', basically everything between '{' and '}' is inside the loop.

Return without a gosub / return 0 what?

Fair point. What you have to think in C is that code exists only inside of functions. So our main program is inside of a function main(), as you saw already. That function is ultimately called by the operating system when you run the program. The return you see goes back to whatever ran the program.
We used 'return 0' because our definition of main() stated that it returns an integer number :

int main(int argn, char* argv[])
{
return 0;
}

Zero is just a nice safe number to return from main. You could return any integer number you wanted. The meaning of the value is operating system dependent. If you returned the value 5, for example, AmigaDOS would regard that a warning.

3) i++ etc..

Welcome to the world of laziness that is C. C was designed to compile into efficient machine code. It so happens that many CPUs can quickly increment and decrement variables and the above operator syntax reflects this ability.

C/C++/Java have a wealth of operate-assign operations. Basically, any dyadic (takes two arguments) arithmetic/logic operation can be written in an operate-assign shorthand for operations like i = i x;

So, to elaborate

i = i + 2 becomes i += 2
i = i * 3 becomes i *= 3
i = i / x becomes i /= x
i = i & 15 becomes i &= 15

etc.

Which you prefer is up to you. Modern compilers almost always optimise the former syntax to the latter during compilation if it can give faster code.

When you see i++, i--, this is a special case syntax for increment/decrement by 1. I should point out that prefix and postfix versions exist:

i++, ++i
i--, --i

What's the difference? The prefix versions (++i) change the value of i first and the new value becomes the result of the whole expression. The postfix versions (i--) do the same job, but the previous value of i is the result of the whole expression

Well an example is best

a)

int i = 5
int j;

j = ++i;

Here, i == 6 and j == 6

b)

int i = 5;
int j;

j = i++;

Here, i == 6 and j == 5;

c)

int i = 5;
int j;

j = --i;

Here, i == 4 and j == 4

You get the idea.

4) ANSI C++

Yes, indeed there is. Unfortunately, C++ is a rather more complex beast and has many features C does not. An inevitable upshot of this is that the defined ANSI C++ standard is not uniformly supported everywhere. If you do get into this, things to watch out for when developing code are

1) STL implementation
2) namespaces
3) templates
4) abstract base references

int p; // A
 

Offline Karlos

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16879
  • Country: gb
  • Thanked: 5 times
    • Show all replies
Re: How to program: In one easy step... Learn?
« Reply #4 on: February 21, 2003, 09:57:05 AM »
Hi again,

Quote

Atheist wrote:
@ Karlos
Well, this is interesting. I understood all the things you said. I've yet to see a book that actually spells it out like that. It seems to me it was deliberately made that way to keep people away from programming.


I'm glad you found it useful. C isn't the nicest language for beginners. The syntax looks weird at first but it really is worth the time to get to know it. It's a structured language and you eventually come to understand and even appreciate the layout. Then BASIC ends up looking weird ;-)

Quote

Like, they could have structured it
For x=1 to 6 step 1
Print X
Next x

instead, and the compiler would just work it out. I can read the above code.


Well, after a bit, you come to realise that the C style  looping mechanisms are unsurpassed in flexibility.
The basic 'use a counter' style looping is just the start. You can create much fancier loops that can iterate through lists of objects without needing a coutner or even knowing the number of objects.
But, that's something for a much later tutorial ;-)

Quote

The answer to 1 is really neat, it means you could check to see if someone renamed your program. Sometimes you need to know everthing.


I suppose you could. Here's an example that might work

/* get io functions */
#include

/* get string functions */
#include

/* a global string that holds the program name */
char realName[] = "atheistsprog";

int main(int argn, char* argv[])
{
/*
* the stricmp() function compares two strings
* (case insensitive) and returns 0 if they are
* equivalent.
*/
  if (stricmp(realName, argv[0])!=0)
  {
    puts("You changed the program name!\nRename it back");
    return 5; /* 5 is an AmigaDOS warning */
  }
  puts("OK");
  return 0; /* okey dokey */
}

Quote

Thanks for the output. Obviously I'm not going to learn C on a message board, but one last important thing.

1) Have every type of C (C, C++, ANSI C and ANSI C ++) been made for Amiga?


Pretty much. If you just want to learn (ANSI) C for starters, get hold of the amiga developer cd v2.1. It comes bundled with StormC v3. Its even good to get the basic fundamentals of C++ too, but one thing at a time.

I know I gripe endlessly about StormC's C++ support being crap, but for an easy working environment its lovely. Just what you don't want to be doing is diving straight into CLI based compilers and makescripts etc. StormC's interface hides all that away nicely.

Once youre exprerienced with the language(s) and want the full works, go GCC.

Quote

2) No RKM's (books) are available for 4.0. Now no custom chips exist. So, the libs: directory will be completely rebuilt for the G4. Do you need 2 different compilers, one for G3, and 1 for G4?


Don't worry on that front. The G3 and G4 are code compatible. G3 code will run on G4, your only difference is that Altivec stuff will only run on G4 (and above?). Anhow, you can expect that your PPC compiler will support multiple target architectures.

The language itself doesn't change, thats the beauty.

Quote

3) I suppose I would need to buy an SDK to get that library information, before I could program in (any) C?


No. If you just want to write simple, portable programs to learn the language, you dont need the full OS related stuff.
Any ANSI C compiler and some tutorials will suffice.

Quote

4) Does one need RKMs to program in ADos 1.3, 2.04 etc.? Can you make 1 program, that will take advantage of a CPU, whether it's just a 68000, or  a 68030 etc.? Or are separate versions always needed?


The RKMs are very handy when you need to write Amiga specific stuff. They detail how the OS works internally and gives you all the required info to take advantage of stuff like gfx/sound/threads etc.
The developer cd comes with those too ;-)

As for code generation, if you need to optimise a program as well as possible then you will need to target a given CPU. Sad fact of life. Unless you have used some assembly code in your project, you won't have to change any code though ;-)

With experience, you can write several versions of your critical functions (usually in assembly) and detect which CPU is used at runtime, then use that information to call the 'optimised' code.

Quote

We REALLY need to stay with 1 CPU.

For instance, now a C is needed to access the altivec component of CPU's.

Amiga!


Well, that's what compilers and libraries are for. The language itself doesn't need to change to take advantage of these developments. That's one of the real beauties of the language and one reason why it's stayed around for so long!
For example, suppose a code did something like

a = b*c+d;

where a, b, c and d are floats. Compiled for 680x0, we'd expect a 'fmul', 'fadd'and a few 'fmove' instructions.
On a PPC 60x, a good compiler would recognise the construct and use the single multiply-add instruction that the PPC supports.
With Altivec, a good compiler will be able to optimise code that operates on arrays of data etc. to make use of the altivec unit.

Tune in next week for the next thrilling installment!

:-)
int p; // A