
-----------------------------------
DtY
Sun Apr 26, 2009 10:03 pm

How is my programming?
-----------------------------------
This weekend I decided I'd learn C, I played with c++ a bit (and mos6502 asm), but other than that this is my first experience with a low level language.
I'm just wondering how the code looks, like if it should be cleaner (likely), if there's a better way to do something, etc.
Also, I'd like to know if it compiles and runs for everyone, I've only tested it on my own computer using gcc on fedora core 10, Linux.

-----------------------------------
wtd
Mon Apr 27, 2009 12:26 am

RE:How is my programming?
-----------------------------------
Short list.


No conditional include guards on game.h
The functions defined in game.c asude from game() are not declard in game.h.


-----------------------------------
DtY
Mon Apr 27, 2009 6:04 am

RE:How is my programming?
-----------------------------------
What are guards? And don't I only need to define the functions that need to be public in the header, don't I?

-----------------------------------
wtd
Mon Apr 27, 2009 1:08 pm

RE:How is my programming?
-----------------------------------
See http://wiki.compsci.ca/index.php?title=Whirlwind_Tour_of_C#Let.27s_compile_something.2C_for_crying_out_loud.21

-----------------------------------
DtY
Mon Apr 27, 2009 6:40 pm

Re: How is my programming?
-----------------------------------
Okay, I added the include guard, and made all the functions other than game() static, so I don't need to add these to the header do I? (Since they can't be accessed from outside that file)

-----------------------------------
wtd
Tue Apr 28, 2009 2:13 am

RE:How is my programming?
-----------------------------------
It may be useful to have those helper functions in your header, if only to ensure that you have an interface for them that can be checked when game.c is compiled.

-----------------------------------
DtY
Tue Apr 28, 2009 3:43 pm

RE:How is my programming?
-----------------------------------
Okay, should they still remain static though?

#ifndef __GAME_H
#define __GAME_H

static char getRepr(int i);
static void drawBoard(int board

Also, I had an idea, instead of putting all the function definitions at the top (without the block), if I just #included game.h, since the definitions are there, and if I changed one of the function outlines, I would only need to change it in two places, rather than three. I tried it, and it compiled fine on my compiler, do you know if it will on most compilers?

-----------------------------------
wtd
Tue Apr 28, 2009 4:13 pm

RE:How is my programming?
-----------------------------------
I'm not sure I follow you.  

Normally you have your header with function declarations and your implementation file containing definitions of those functions.

-----------------------------------
wtd
Tue Apr 28, 2009 4:18 pm

RE:How is my programming?
-----------------------------------
Also, in your makefile, you should create CC and LN (compiler and linker) aliases.  Allows you to easily change compilers and linkers with only one edit.

Oh, and you need a "clean" rule in your makefile.  :-)

-----------------------------------
DtY
Tue Apr 28, 2009 5:09 pm

RE:How is my programming?
-----------------------------------
Okay, here's my updated makefile:
static char getRepr(int i); (I think it's called prototyping), so instead of putting that at the top of the file, I can just #include it's header file.

-----------------------------------
OneOffDriveByPoster
Tue Apr 28, 2009 6:23 pm

Re: RE:How is my programming?
-----------------------------------
If you have static functions, do not put them into a header file (at least the one that is supposed to be for the interface).
Use a separate header file that can only be included into the correct file (#error inside #if with appropriate check).

The CC variable should not have the "-c" part.  Make it plain "gcc".  The LN variable is a confusing name for the linker; maybe use "LD".  Again, the "-o" part probably does not belong with the command variable.

Your use of touch and then rm can be replaced with "rm -f".  FYI, GNU make has $(RM).

-----------------------------------
DtY
Tue Apr 28, 2009 9:04 pm

RE:How is my programming?
-----------------------------------
Okay, changed all that. What is $(RM)? Is that just a shortcut to rm -f or something?

-----------------------------------
OneOffDriveByPoster
Tue Apr 28, 2009 10:09 pm

Re: RE:How is my programming?
-----------------------------------
Is that just a shortcut to rm -f or something?The default setting is that it is "rm -f", yes.
