Computer Science Canada How is my programming? |
Author: | DtY [ Sun Apr 26, 2009 10:03 pm ] |
Post subject: | 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. |
Author: | wtd [ Mon Apr 27, 2009 12:26 am ] |
Post subject: | RE:How is my programming? |
Short list.
|
Author: | DtY [ Mon Apr 27, 2009 6:04 am ] |
Post subject: | 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? |
Author: | wtd [ Mon Apr 27, 2009 1:08 pm ] |
Post subject: | 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 |
Author: | DtY [ Mon Apr 27, 2009 6:40 pm ] |
Post subject: | 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) |
Author: | wtd [ Tue Apr 28, 2009 2:13 am ] |
Post subject: | 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. |
Author: | DtY [ Tue Apr 28, 2009 3:43 pm ] | ||
Post subject: | RE:How is my programming? | ||
Okay, should they still remain static though? [edit] Here is game.h with the definitions:
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? |
Author: | wtd [ Tue Apr 28, 2009 4:13 pm ] |
Post subject: | 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. |
Author: | wtd [ Tue Apr 28, 2009 4:18 pm ] |
Post subject: | 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. ![]() |
Author: | DtY [ Tue Apr 28, 2009 5:09 pm ] | ||
Post subject: | RE:How is my programming? | ||
Okay, here's my updated makefile:
And on the subject of makefiles, any idea why make requires tabs, and doesn't recognize whitespace as whitespace like all other *nix tools? [edit] Missed your first post, I mean like at the beginning of the files, you have the declaration of the function, like 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. |
Author: | OneOffDriveByPoster [ Tue Apr 28, 2009 6:23 pm ] |
Post subject: | 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). |
Author: | DtY [ Tue Apr 28, 2009 9:04 pm ] |
Post subject: | RE:How is my programming? |
Okay, changed all that. What is $(RM)? Is that just a shortcut to rm -f or something? |
Author: | OneOffDriveByPoster [ Tue Apr 28, 2009 10:09 pm ] |
Post subject: | Re: RE:How is my programming? |
DtY @ Tue Apr 28, 2009 9:04 pm wrote: Is that just a shortcut to rm -f or something? The default setting is that it is "rm -f", yes. |