Welcome, Guest. Please login or register.

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

Description:

0 Members and 1 Guest are viewing this topic.

Offline mel_zoomTopic starter

  • Full Member
  • ***
  • Join Date: Jan 2007
  • Posts: 231
    • Show all replies
Learning C with the Amiga
« on: January 25, 2007, 12:45:52 PM »
Hi!

Ive decided to have a go at learning C on the amiga. I do know a little bit about the language and programming in general but Ive never really written anything.

In my introduction, someone pointed me at AmiDevCPP but this is clearly windows software and if possible Id prefer to use an amiga native environment.

What options are there given a relatively bare minimum of experience?
I love my MX5!
Please pay a visit
 

Offline mel_zoomTopic starter

  • Full Member
  • ***
  • Join Date: Jan 2007
  • Posts: 231
    • Show all replies
Re: Learning C with the Amiga
« Reply #1 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 all replies
Re: Learning C with the Amiga
« Reply #2 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 mel_zoomTopic starter

  • Full Member
  • ***
  • Join Date: Jan 2007
  • Posts: 231
    • Show all replies
Re: Learning C with the Amiga
« Reply #3 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 mel_zoomTopic starter

  • Full Member
  • ***
  • Join Date: Jan 2007
  • Posts: 231
    • Show all replies
Re: Learning C with the Amiga
« Reply #4 on: January 29, 2007, 02:23:01 PM »
Hi!

Wow so many ways to start a program in C! I think Ill stick with the basic "int main(int argc, char** argv)" one for now.
I love my MX5!
Please pay a visit
 

Offline mel_zoomTopic starter

  • Full Member
  • ***
  • Join Date: Jan 2007
  • Posts: 231
    • Show all replies
Re: Learning C with the Amiga
« Reply #5 on: February 03, 2007, 12:49:17 AM »
Hi!

I think Im actually making progress in the theory and I have just finished installing from the CD Karlos gave me. Fingers crossed I can actually compile something soon :-)
I love my MX5!
Please pay a visit
 

Offline mel_zoomTopic starter

  • Full Member
  • ***
  • Join Date: Jan 2007
  • Posts: 231
    • Show all replies
Re: Learning C with the Amiga
« Reply #6 on: February 03, 2007, 06:44:32 PM »
lou_dias:

"See what you get..."

Well I could guess but I thought it might be interesting to see how it liked your definition of "main":

gcc -Wall test.c -o test
test.c:5: warning: return type of `main' is not `int'

Cannon Fodder is not funny.
Cannon Fodder is a 0

Im told that all good c programs are also c++ programs so I also tried in c++ mode and then it didnt compile at all.

g++ -Wall test.c -o test2
test.c:5: error: `main' must return `int'
test.c:5: error: return type for `main' changed to `int'

So according to that advice, this is not a good c program.

If you are planning to ridicule someone elses abilities shouldnt you do it in an "upwards compatible" way?
I love my MX5!
Please pay a visit
 

Offline mel_zoomTopic starter

  • Full Member
  • ***
  • Join Date: Jan 2007
  • Posts: 231
    • Show all replies
Re: Learning C with the Amiga
« Reply #7 on: February 03, 2007, 07:47:53 PM »
lou_dias:

I never quit once I set my mind on something. I dont think the language looks that bad just yet!

This one worked fine....

#include

int main(void)
{
  const char *result;
  int lou_dias_joke;
  int backfired;

  lou_dias_joke = 1;
  backfired = 1;
  result = "was funny";
  if (lou_dias_joke == backfired) {
    result = "backfired";
  }
  printf("lou_dias joke %s.\n", result);
  return 0;
}

I love my MX5!
Please pay a visit
 

Offline mel_zoomTopic starter

  • Full Member
  • ***
  • Join Date: Jan 2007
  • Posts: 231
    • Show all replies
Re: Learning C with the Amiga
« Reply #8 on: February 03, 2007, 07:48:52 PM »
Mmm. Is there some way to keep the indentation when posting?
I love my MX5!
Please pay a visit
 

Offline mel_zoomTopic starter

  • Full Member
  • ***
  • Join Date: Jan 2007
  • Posts: 231
    • Show all replies
Re: Learning C with the Amiga
« Reply #9 on: February 03, 2007, 08:06:29 PM »
Jupp3:

"You can do in C everything, you can do in C++"

I plan to learn c++ once I have a better understanding of c. At least I was told this was a good idea :)
I love my MX5!
Please pay a visit
 

Offline mel_zoomTopic starter

  • Full Member
  • ***
  • Join Date: Jan 2007
  • Posts: 231
    • Show all replies
Re: Learning C with the Amiga
« Reply #10 on: February 03, 2007, 08:21:18 PM »
lou_dias:

"ah yes, another thing about C syntax that makes no sense"

I dont follow. Ive not looked much at pointers yet but the idea seems self-explanatory so far. Unless I got this very wrong, you have the pointer itself and the thing it points at and either of these can be constant. Thats four possibilities as far as I can see:

pointer to something
pointer to something constant
constant pointer to something
constant pointer to something constant

So far we only saw the pointer to something constant?
I love my MX5!
Please pay a visit
 

Offline mel_zoomTopic starter

  • Full Member
  • ***
  • Join Date: Jan 2007
  • Posts: 231
    • Show all replies
Re: Learning C with the Amiga
« Reply #11 on: February 04, 2007, 07:35:20 PM »
Guys! Can we save this for later? Im only just looking at functions :lol:
I love my MX5!
Please pay a visit
 

Offline mel_zoomTopic starter

  • Full Member
  • ***
  • Join Date: Jan 2007
  • Posts: 231
    • Show all replies
Re: Learning C with the Amiga
« Reply #12 on: February 07, 2007, 08:08:58 PM »
Hi!

So Ive spent time looking at functions in C since I last posted here. So far so good in that I understand the purpose of functions and Im not particularly struggling with the syntax.

I do have some basic questions and one not so basic question though (which is getting a bit ahead of myself too).

Question 1

Is there a way that a function can return more than one value simultaneously? Its obvious from the function definition syntax so far that you return a single value or nothing at all (void).

However if I can imagine times when returning multiple values together might be useful Im sure other people can and have found ways.

So far I can only think of returning an array or structure or modifying the parameters if you "pass by reference". An array is presumably only any good if you have to return multiple values that are the same "type", right?

Question 2

Can you actually return an array in this way or is it only by reference too? If that is the case wouldnt the array that the reference represents be destroyed when the function returns?

Ive not really looked at structures yet but I understand that they are basically made by putting together more existing types so you could make one that represents the multiple values you want to return. So, would that be a valid solution?

I looked at the other idea using the "pass by reference" idea and I know that this involves pointers and is therefore a likely place to make errors if Im not careful. I dont really want to do that yet even though I am looking at them but I noticed something weird.

The book I have says that normal variables are "passed by value" so you get a copy of them that you are free to alter. It also says that arrays are always "passed by reference" so you get the actual array and not a copy.

I am aware that the array syntax is basically another way of representing a pointer so that kind of makes sense really. I can imagine that this makes things faster compared to copying an array which might be very large.

What I found odd was that structures can be passed by value as well as reference. Why is that? A structure can contain a lot of variables and I imagine theres no reason you couldnt even have an array inside one so a structure could be as big as any array. So...

Question 3

Is there some important reason that structures can be passed by value to a function? When might you do something like that?
I love my MX5!
Please pay a visit
 

Offline mel_zoomTopic starter

  • Full Member
  • ***
  • Join Date: Jan 2007
  • Posts: 231
    • Show all replies
Re: Learning C with the Amiga
« Reply #13 on: February 07, 2007, 08:58:24 PM »
samuraicrow:

"AFAIK structures cannot be passed by value and must be passed by reference"

Well, I was looking at this already and have written a test program that works. Did I compile this wrong?

gcc -Wall testbyval.c -o testbyval


The code is

/* test passing structures by value - why does this work? */

#include

struct coord {
  int x;
  int y;
};

struct coord testbyvalue(struct coord o)
{
  o.x++;
  o.y++;
  return o;
}

int main(void)
{
  struct coord c1 = {1,2};
  struct coord c2 = testbyvalue(c1);
  printf("c1 = {%d,%d}\nc2 = {%d,%d}\n}", c1.x, c1.y, c2.x, c2.y);
  return 0;
}

I love my MX5!
Please pay a visit
 

Offline mel_zoomTopic starter

  • Full Member
  • ***
  • Join Date: Jan 2007
  • Posts: 231
    • Show all replies
Re: Learning C with the Amiga
« Reply #14 on: February 07, 2007, 09:01:30 PM »
hans:

I understand that and I can see why arrays are "by reference" but why are structures allowed to be by value? Especially as they can contain an array even?
I love my MX5!
Please pay a visit