Welcome, Guest. Please login or register.

Author Topic: Learning C with the Amiga  (Read 12351 times)

Description:

0 Members and 1 Guest are viewing this topic.

Offline costabunny

  • Jr. Member
  • **
  • Join Date: Dec 2006
  • Posts: 65
    • Show only replies by costabunny
    • http://www.xiblings.co.uk
Re: Learning C with the Amiga
« Reply #14 on: January 25, 2007, 06:23:33 PM »
I agree - GCC is great, even on a basic amy (tho its been a few years for me and my c compilers).  I regularly use it on other architectures and have to admit that I am now feeling the urge to get down with C on the amiga (now where did I put my amy- oh yes still trying to get one)

:lotsacoffee:
Mac, PC, WinUAE....
 

Offline mel_zoomTopic starter

  • Full Member
  • ***
  • Join Date: Jan 2007
  • Posts: 231
    • Show only replies by mel_zoom
Re: Learning C with the Amiga
« Reply #15 on: January 25, 2007, 06:46:20 PM »
It seems a recordable CD containing GCC and the NDK was left on my desk, along with a copy of the K&R C Language reference and another called "Mastering Standard C".

I wonder who left those.... ?


;-)
I love my MX5!
Please pay a visit
 

Offline mel_zoomTopic starter

  • Full Member
  • ***
  • Join Date: Jan 2007
  • Posts: 231
    • Show only replies by mel_zoom
Re: Learning C with the Amiga
« Reply #16 on: January 25, 2007, 07:02:04 PM »
Starting with the basics, Ive been looking at variable "types". Lets see if I got this right

A "char" is 1 byte in size
A "short int" is at least as big as a "char"
An "int" is at least as big as a "short int"
A "long int" is at least as big as an "int"

From what Ive read so far, different systems agree on the "char" but differ on the rest, but typical sizes for current systems are

"char" 1 byte
"short int" 2 bytes
"int" 4 bytes
"long int" 4 bytes

Also all of the above can only represent whole number values  and may be specified as "unsigned" or "signed" too but with "signed" being the default for anything other than a char?

Is this correct so far?
I love my MX5!
Please pay a visit
 

Offline EDanaII

  • Hero Member
  • *****
  • Join Date: Dec 2006
  • Posts: 579
    • Show only replies by EDanaII
    • http://www.EdwardGDanaII.info
Re: Learning C with the Amiga
« Reply #17 on: January 25, 2007, 07:29:45 PM »
Quote
I wonder who left those.... ?


Ooo! Ooo! Let me guess! Was it... Santa Karlos?
Ed.
 

Offline SamuraiCrow

  • Hero Member
  • *****
  • Join Date: Feb 2002
  • Posts: 2280
  • Country: us
  • Gender: Male
    • Show only replies by SamuraiCrow
Re: Learning C with the Amiga
« Reply #18 on: January 25, 2007, 08:20:25 PM »
Quote

mel_zoom wrote:
Starting with the basics, Ive been looking at variable "types". Lets see if I got this right

A "char" is 1 byte in size
A "short int" is at least as big as a "char"
An "int" is at least as big as a "short int"
A "long int" is at least as big as an "int"

From what Ive read so far, different systems agree on the "char" but differ on the rest, but typical sizes for current systems are

"char" 1 byte
"short int" 2 bytes
"int" 4 bytes
"long int" 4 bytes

Also all of the above can only represent whole number values  and may be specified as "unsigned" or "signed" too but with "signed" being the default for anything other than a char?

Is this correct so far?


Correct so far.

Old source codes made for SAS C and DICE C compilers treat int as a 16-bit word since the original 68000 processor was 16-bit.  For reasons like this you might want to include exec/types.h at the beginning of your Amiga-specific source codes and use BYTE, WORD, and LONG for the respective 8, 16, and 32-bit signed types and UBYTE, UWORD, and ULONG for the respective unsigned types.


One more thing you might want to look into before you get too involved are the SDI Headers which will let you generate Amiga-specific code in such a way that it can be recompiled on AmigaOS 3.x, 4.0, and MorphOS and their respective different versions of the compilers with few (if any) additional changes.
 

Offline whose

  • Newbie
  • *
  • Join Date: Dec 2006
  • Posts: 13
    • Show only replies by whose
Re: Learning C with the Amiga
« Reply #19 on: January 25, 2007, 08:38:06 PM »
@lou_dias:

Yeah, I see, for programs of such class GCC is a MUST and Storm is absolutely unsuitable ;)

I think we gave her enough tips, she should have a look herself, what´s suiting her needs best.

But one thing: Compared to Storm, SAS, vbcc et al GCC IS slow. Awfully slow. More than 5 times slower than Storm on a 030 miggy without min. 16 MB ram. On other (bigger) AmigaOS machines vbcc outperforms all other compilers.

Be fair and test it yourself instead of overtaking other people´s meaning without critics. GCC is a so-to-speak standard, but it´s definetly not "the best" compiler out there. This depends on the project´s needs, I would say.

Greetz
 

Offline CannonFodder

  • Hero Member
  • *****
  • Join Date: Sep 2003
  • Posts: 1115
    • Show only replies by CannonFodder
Re: Learning C with the Amiga
« Reply #20 on: January 25, 2007, 09:43:45 PM »
@Mel

I have the complete set of RKRM's, each book released in each edition.

I don't use them at all these days, I could lend them to you if you promise to take good care of them.
People are hostile to what they do not understand - Imam Ali ibn Abi Talib(AS)
 

Offline mel_zoomTopic starter

  • Full Member
  • ***
  • Join Date: Jan 2007
  • Posts: 231
    • Show only replies by mel_zoom
Re: Learning C with the Amiga
« Reply #21 on: January 26, 2007, 12:51:13 PM »
Hi!

I actually compiled some code last night. One thing I have noticed is that the definition of "main" varies a lot in examples. So far Ive seen the following

main()
{

}

main(void)
{

}

int main()
{
  return 0;
}

int main(void)
{
   return 0;
}

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

My question is, which one should I be using? The last one seems pretty explicit but Im told that its the best one to use even though the first one is in the K&R book as the principal definition :-?
I love my MX5!
Please pay a visit
 

Offline Steady

Re: Learning C with the Amiga
« Reply #22 on: January 26, 2007, 01:25:07 PM »
The last definition is the most 'correct' one because when your program starts, the main() function will receive a pointer to all the arguments on the command line in argv as an array. argn contains the number of arguments that were supplied (including the name of the command).

For example, if you were to type the following at the Shell prompt (not including the quotes):

'dir ram: all'

the main() function arguments would contain the following:

argn = 3

argv[0] = "dir"
argv[1] = "ram:"
argv[2] = "all"

You can think of the array as a list of the words on the command line that can be read separately by counting from left to right. Counting starts at 0.

Also, traditionally argn tends to be called argc, but it doesn't really matter. It's just a name.

Hope that helps.
 

Offline Cymric

  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 1031
    • Show only replies by Cymric
Re: Learning C with the Amiga
« Reply #23 on: January 26, 2007, 01:43:27 PM »
Quote
mel_zoom wrote:
My question is, which one should I be using? The last one seems pretty explicit but Im told that its the best one to use even though the first one is in the K&R book as the principal definition :-?

For this sort of question I refer you to the excellent and witty C-faq, which in this case states that the correct declaration is at the least

int main() { }

or

int main(void) { }

to tell the C-compiler (though not a C++-compiler!) that you're not expecting any arguments to main(). (The C++-compiler is a bit more literal and sees the above as two different functions: the first doesn't expect any parameter, the second expects a parameter of type 'void'.) If you do want arguments, then the correct declaration is

int main(int argc, char *argv[]) { }

The upshot is that you should end main() not with a plain 'return;' but with a 'return ;'. The integer is usually 0 to indicate that the program completed succesfully, if you set it to 5 or 10 AmigaDOS interprets this as a warning, and with 20 or higher it creates an error message. (I think, it's been a while :).)

To make things a bit more complex: the above mechanism works only for programs you start from the CLI. For programs which start from the Workbench, you will need to use a different and much more Amiga-like mechanism. For now you can ignore its existence, but remember that you need to perform a bit of special processing if you want your program to be double-clickable.
Some people say that cats are sneaky, evil and cruel. True, and they have many other fine qualities as well.
 

Offline gertsy

  • Lifetime Member
  • Hero Member
  • *****
  • Join Date: May 2006
  • Posts: 2317
  • Country: au
    • Show only replies by gertsy
    • http://www.members.optusnet.com.au/~gbakker64/
Re: Learning C with the Amiga
« Reply #24 on: January 26, 2007, 01:59:29 PM »
Go SAS. I used it 16 years ago so its gotta be good by now.
They still exist Mainframe and Unix(Solaris/HP-UX/AIX) stuff now. I'm sure no one there would know they had once supported the Amiga.
 

Offline Mad_Dog

  • Newbie
  • *
  • Join Date: Jan 2007
  • Posts: 1
    • Show only replies by Mad_Dog
    • http://www.norman-interactive.com
Re: Learning C with the Amiga
« Reply #25 on: January 26, 2007, 02:43:16 PM »
Hi,

If you understand German, my C workshop may be the right place for you to start:

http://www.norman-interactive.com/C-Kurs.html
 

Offline Karlos

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16867
  • Country: gb
  • Thanked: 4 times
    • Show only replies by Karlos
Re: Learning C with the Amiga
« Reply #26 on: January 28, 2007, 12:38:22 PM »
You should always use the last form when writing portable commandline applications. Amiga specific stuff can use ReadArgs(), but that's further down the line.

At the very least, never use plain "main()" without int return type (and remembering to actually return an int from it, zero for success). That's just a legacy anachronism.
int p; // A
 

Offline Karlos

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16867
  • Country: gb
  • Thanked: 4 times
    • Show only replies by Karlos
Re: Learning C with the Amiga
« Reply #27 on: January 28, 2007, 12:41:00 PM »
Quote

Steady wrote:

Also, traditionally argn tends to be called argc, but it doesn't really matter. It's just a name.


That would be my fault, I put some simple examples on a CD ;-)

Personally I generally tend to use "int argNum, char** argVal".
int p; // A
 

Offline Dietmar

  • Full Member
  • ***
  • Join Date: Nov 2002
  • Posts: 220
    • Show only replies by Dietmar
    • http://devplex.awardspace.biz
Re: Learning C with the Amiga
« Reply #28 on: January 28, 2007, 05:10:01 PM »
Quote
Im told that its the best one to use even though the first one is in the K&R book as the principal definition

If you read the K&R, take into account that it describes an obsolete version of C and that it will expose you to what is no considered bad style or obsolete.

There were several revisions of C (e.g. C90, C99 around 1990 and 1999), parallel to the invention of C++. The book of your choice should at least cover C90. If you download the Cubic IDE development environment (free for beginners) and its C++ package, you will find it includes an electronic book on C90 and some C99:

http://www.developers.2go.cc/cubic/download.html
 

Offline lionstorm

Re: Learning C with the Amiga
« Reply #29 from previous page: January 28, 2007, 05:38:14 PM »
Quote

Mad_Dog wrote:
Hi,

If you understand German, my C workshop may be the right place for you to start:

http://www.norman-interactive.com/C-Kurs.html


how about translating it to English ? That way it will profit to a much bigger andience (including myself).

Thanks